Moose programing

Páginas: 12 (2856 palabras) Publicado: 26 de julio de 2010
Programming with Moose/Syntax/has
From Wikibooks, the open-content textbooks collection The most important Moose keyword is arguably "has", which is essentially the most beefed up accessor-generator ever devised. Many of these features can be found in the accompanying docs of Moose (http://search.cpan.org/author /DROLSKY/Moose/lib/Moose.pm) , and Class::MOP::Accessor(http://search.cpan.org/~drolsky/Class-MOP/lib/Class/MOP/Attribute.pm) .

Attributes of has
Below you can find all of the attributes of has.[1] Astrisk represents the default, and should not ever explicitly show up when you're using Moose. [+]has => ( isa => , is => , coerce => , weak_ref => , does => , required => , lazy => , auto_deref => , init_arg => , default => , metaclass => , trigger => , initializer=> , handles=> , lazy_build => , clearer => , predicate => , builder => );

$TypeConstraint "ro|rw" 0*|1 0*|1 $RoleName 0*|1 0*|1 0*|1 $Str|undef $DefaultValue $Voodoo $SubRef $SubRef Array|Hash|Regex|Role|Code 0*|1 $ClearerName $PredicateName $BuilderName

isa
The second most essential attribute of has is isa. isa is pronounced "Is a", and

essentially does little more than tell what the attribute canbe set with. These are the default choices for isa. In addition to them isa can be set to any class name, for instance isa => "URI", or isa => "HTML::Treebuilder".
Bool Undef Defined Value Num Int Str ClassName Ref ScalarRef ArrayRef HashRef CodeRef RegexpRef GlobRef FileHandle Object Role

Any Item

has 'foobar' => ( isa => 'Str' , is => 'ro' ); has 'foobar' => ( isa => 'MooseObject' , is=> 'ro' );

Moose v0.26 introduces parameterized types[1] -- no one knows what they really do, but they gave the ability to do such things as: has 'foobar' => ( isa => ArrayRef[URI] ); Now when you try to store to ->foobar, you had better only add URI elements to the ArrayRef, or you will croak.

is
The heart of the attribute generating has is the attribute is. If something is is => 'ro' theuser has a getter (read-only accessor) by the specified name. Conversely, if the value supplied is is => 'rw' the user has a getter/setter hybrid (read-write accessor). The default value is to not generate an accessor of either kind at all.

coerce
The coerce attribute to has is significantly more complex than is and isa. It tells Moose, "Hey dude, it would be really l33t if you would get thisvalue into the datatype I want it to be without being a pain in the ass all the time."

Moose generally listens when you talk to it as such. package MyMoose; use Moose; use Moose::Util::TypeConstraints; use URI; subtype 'URI' => as 'Object' => where { $_->isa('URI') } ; coerce 'URI' => from 'Str' => via { URI->new($_) } ; has 'myuri' => ( is => 'rw', isa => 'URI', coerce => 1 ); package main; my$m = MyMoose->new; $m->myuri( 'foobar' ); print ref $m->myuri; ## print URI::_generic

To use coerce you must have a supplied a subtype which can coerce.

weak_ref
You cannot use coerce => 1, if you set weak_ref => 1 Weak_ref stops the refcount held in the attribute from being incremented by the object. This means if you have one copy of something, and a hundred objects use it with weak_ref=> 1, you can successfully destroy their copies by undef'ing your one copy. Perl's garbage collector will destroy the item when its reference count reaches zero. With weak_ref your object will never increase the reference count. This prevents circular recursion, but leaves you susceptible to having the rug pulled out from under you, so to speak.

$| = 1; ## turn off buffering package MyMoose;use Moose; has 'foo' => ( isa , is , weak_ref ); package main; my $foo = [qw/ foo bar baz/]; my $m = MyMoose->new({ foo => $foo }); print @{ $m->foo }; undef $foo; ## Can't use an undefined value as an ARRAY reference at test.pl line 21. print @{ $m->foo }; ## prints foobarbaz => 'ArrayRef' => 'ro' => '1'

does
This will accept the name of a role which the value stored in this attribute is...
Leer documento completo

Regístrate para leer el documento completo.

Estos documentos también te pueden resultar útiles

  • Programing
  • Programing C++
  • c programing
  • programing c.
  • Dvr programing
  • Xp programing
  • Programing
  • Programing

Conviértase en miembro formal de Buenas Tareas

INSCRÍBETE - ES GRATIS