åãã¦ã®Moose - Mooseã®ããã
ã·ã³ãã«ãªPerl 5ã§ã¯ã©ã¹ãä½ã
æ®éã®Perl5 OOã§ã¯ã©ã¹ãæ¸ãã¨
{ package Dog; use strict; use warnings; sub new { my ( $class, %args ) = @_; my $obj = bless { name => $args{name}, gender => $args{gender}, }, $class; return $obj; } sub name { my $self = shift; $self->{name} = shift if @_; return $self->{name}; } sub gender { my $self = shift; $self->{gender} = shift if @_; return $self->{gender}; } sub bark { my $self = shift; print "Bow Wow!\n"; } } use strict; use warnings; my $dog = Dog->new( gender => 'male' ); $dog->name('Pochi'); print 'gender: ' . $dog->gender . "\n"; print 'name: ' . $dog->name . "\n"; $dog->bark;
è¯ããããã¡ãªDog(ç¬)ã¯ã©ã¹ã§ããã
åå(name)ã¨æ§å¥(gender)ã¨ããã¯ã©ã¹å±æ§(ã¢ããªãã¥ã¼ã)ã¸ã®ã¢ã¯ã»ãµãæã£ã¦ãã¦ãå¼ãã(bark)ã£ã¦ããã¡ã½ãããæã£ã¦ãã¾ãã
ã³ã³ã¹ãã©ã¯ã¿å¼æ°ã«ã¯nameã¨genderãæå®ã§ãã¾ãã
ã§ãå®è¡ããã¨
gender: male name: Pochi Bow Wow!
ã¨è¡¨ç¤ºããã¾ãã
Perl 5ã®OOã¯æå¤ã¨ã·ã³ãã«ãªã®ã§ãããã¡ãã£ã¨åé·ã ãæ°ããã¢ããªãã¥ã¼ãã追å ããããã³ã«ã¢ã¯ã»ãµãä½ãã®ã¯é¢åã§ãã
Class::Accessorã使ã£ã¦ã¯ã©ã¹ãä½ã
ã§ãCPANãæ¼ã£ã¦ã¿ãã¨Class::Accessorã¨ãã便å©ãªã¢ã¸ã¥ã¼ã«ãããã®ã§æ¸ãç´ãã¦ã¿ã¾ãã
{ package Dog; use strict; use warnings; use base qw(Class::Accessor); __PACKAGE__->mk_accessors(qw(name gender age)); sub bark { my $self = shift; print "Bow Wow!\n"; } } use strict; use warnings; my $dog = Dog->new( gender => 'male' ); $dog->name('Pochi'); $dog->age('three'); print 'gender: ' . $dog->gender . "\n"; print 'name: ' . $dog->name . "\n"; print 'age: ' . $dog->age . "\n"; $dog->bark;
ããã¶ããã£ããããä¸ã«å¹´é½¢(age)ã¸ã®ã¢ã¯ã»ãµã追å ã§ãã¾ããã
Class::Accessorã§ã¯æ°ããã¢ã¯ã»ãµã追å ããããªã£ããmk_accessorã§è¿½å ããã ãã§ãã
ã³ã³ã¹ãã©ã¯ã¿ã®å¼æ°ãããã·ã¥ããããã·ã¥ãªãã¡ã¬ã³ã¹ã«å¤ãã£ããã¨ãé¤ãã°ãããã»ã©ã®ã«å ãã¦å¹´é½¢ã表示ãããããã«ãªãã¾ããã
Class::Accessorã¯è²ã ãªã¢ã¸ã¥ã¼ã«ã§ä½¿ããã¦ãããã¡ãã£ã¨ããã¯ã©ã¹ãä½ãæã«ã¯é常ã«ä¾¿å©ã§ãã
ãããã®ã¯ã©ã¹ã®è¦ä»¶ã«æ°ãããageã¢ã¯ã»ãµã¯æ´æ°ã®ã¿åãã¤ãããã¨è¨ããã®ã追å ããããã©ãã§ãããï¼
ãããªãã¨Class::Accessorã®æ©æµãåãããã¨ã¯ã§ããªããªããã³ã³ã¹ãã©ã¯ã¿ã¨ageã¡ã½ãããèªåã§å®è£
ããå¿
è¦ãã§ã¦ãã¾ãã
Mooseã使ã£ã¦ã¯ã©ã¹ãä½ã
ããã§ãMooseã使ã£ã¦ãã®è¦ä»¶ãå«ãã¦å®è£ ãã¦ã¿ã¾ãã
{ package Dog; use Moose; has name => ( is => 'rw', isa => 'Str' ); has gender => ( is => 'rw', isa => 'Str' ); has age => ( is => 'rw', isa => 'Int' ); sub bark { my $self = shift; print "Bow Wow!\n"; } } use strict; use warnings; my $dog = Dog->new( gender => 'male' ); $dog->name('Pochi'); $dog->age('three'); print 'gender: ' . $dog->gender . "\n"; print 'name: ' . $dog->name . "\n"; print 'age: ' . $dog->age . "\n"; $dog->bark;
ãããå®è¡ããã¨
Attribute (age) does not pass the type constraint because: Validation failed for 'Int' failed with value three at (eval 72) line 7 Dog::age('Dog=HASH(0x9df9fe8)', 'three') called at moose.pl line 23
ã¨ãã¨ã©ã¼ã表示ããå®è¡ã§ããªããã¨ããããã¾ãã
ããã¯ageã¢ããªãã¥ã¼ã(has age)ã¯èªã¿æ¸ãå¯è½(is => 'rw')ã ãã©ãæ´æ°ã®ã¿å¤ã¨ãã¦ä¿æã§ãã(is => 'Int')ããã«è¨å®ããã¦ããããã§ãã
$dog->age(3);
ã®ããã«ç´ãã¨ãå ã»ã©ã¨åãçµæãå¾ããã¨ãã§ãã¾ãã
ãã®ããã«Mooseã¯åãªãPerl 5 OOã®ç¼ãç´ãã ãã§ã¯ãªããããç°¡åã«ããå¼·åãªOOããã°ã©ãã³ã°ãæä¾ãã¦ãããã¢ã¸ã¥ã¼ã«ã§ãã
å ã»ã©ã®åå¶ç´ã ãã§ã¯ãªããåå¼·å¶å¤æ(coerce)ãã¤ã³ã¿ã¼ãã§ã¼ã¹(role)ãã¡ã¿ã¯ã©ã¹ã¸ã®ã¢ã¯ã»ã¹ãªã©ã®æ©è½ãæä¾ãã¦ããã¾ãã