Commit 3e22da84 by PotatoGim

MooseX::NonMoose를 메타 클래스에 적용하는 예

parent 0d8f3267
#!/usr/bin/env perl
#=============================================================================
# FILE: moosex_nonmoose_with_meta.pl
# USAGE: ./moosex_nonmoose_with_meta.pl
# OPTIONS:
# BRIEF: 메타 클래스에 MooseX::NonMoose를 적용하는 예
# AUTHOR: Ji-Hyeon Gim <potatogim@potatogim.net>
# DATE: 01/21/2015 09:16:18 PM
# VERSION: 1.0
# LICENSE: Artistic License
# NOTE:
#=============================================================================
package Fruits;
sub new
{
print "new: ", __PACKAGE__, "\n";
my $class = shift;
my $self = shift;
$self = {} unless $self && ref($self) eq "HASH";
bless ($self, $class);
return $self;
}
sub grow
{
print "growing...\n";
}
sub DESTROY
{
print "DESTROY: ", __PACKAGE__, "\n";
}
package main;
use Moose;
use Moose::Meta::Class;
use Data::Dumper;
sub new_fruit_instance
{
my $name = shift;
my $metaclass = Moose::Meta::Class->create($name);
$metaclass = Moose::Util::MetaRole::apply_metaroles (
for => $name,
class_metaroles => {
class => ["MooseX::NonMoose::Meta::Role::Class"],
constructor => ["MooseX::NonMoose::Meta::Role::Constructor"],
},
);
$metaclass->superclasses ("Fruits");
$metaclass->add_attribute ("name" => (is => "ro"));
$metaclass->add_method (
"be_split" => sub {
printf "%s says 'I was split in half!'.\n", shift->meta->name;
}
);
$metaclass->add_method (
"BUILD" => sub {
print "BUILD: ", shift->meta->name, "\n";
}
);
$metaclass->add_method (
"DEMOLISH" => sub {
print "DEMOLISH: ", shift->meta->name, "\n";
}
);
$metaclass->add_method (
"FOREIGNBUILDARGS" => sub {
my $class = shift;
my $name = shift;
print "FOREIGNBUILDARGS: ", $class, "\n";
return $name;
}
);
my $instance = $metaclass->name->new();
return $instance;
}
my $apple = new_fruit_instance("Apple");
$apple->be_split();
my $banana = new_fruit_instance("Banana");
$banana->be_split();
my $cherry = new_fruit_instance("Cherry");
$cherry->be_split();
my $durian = new_fruit_instance("Durian");
$durian->be_split();
1;
__END__
=encoding utf8
=head1 NAME
moosex_nonmoose_with_meta.pl
=head1 SYNOPSIS
메타 클래스에 MooseX::NonMoose를 적용하는 예
=head1 DESCRIPTION
=over
=item B<Item 1>
=item B<Item 2>
=item B<Item 3>
=back
=head1 AUTHOR
Ji-Hyeon Gim <potatogim@potatogim.net>
=head1 CONTRIBUTORS
=head1 COPYRIGHT AND LICENSE
Copyright(c) 2015 by Ji-Hyeon Gim <potatogim@potatogim.net>
This is free software; you can redistribute it and/or modify it
under the same terms as Perl 5 itself at:
L<http://www.perlfoundation.org/artistic_license_2_0>
You may obtain a copy of the full license at:
L<http://www.perl.com/perl/misc/Artistic.html>
=head1 SEE ALSO
=cut
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment