Seitenanfang

Moose documentation quick reference

Dieser Post wurde aus meiner alten WordPress-Installation importiert. Sollte es Darstellungsprobleme, falsche Links oder fehlende Bilder geben, bitte einfach hier einen Kommentar hinterlassen. Danke.


I don't like Moose. It has a lot of disadvantages and tries to create a new language based on Perl - without any need. But I'm forced to use it within a project and working myself into all that stuff, the documentation turned out to be as bad as the one of DBIx::Class. This post is my attempt to merge the most important documentation parts.

Many important information is spread all over different modules POD. It's ok, because all these modules add their little part to the big Moose thing and every documentation bit is part of the module adding the feature but - as a user in terms of developer-using-Moose - I don't care which module added which feature (at least for 98% of the time I need to look up this and that).

Package and sub declaration

from Moose, MooseX::Declare and Class::MOP

Package/Class

use MooseX::Declare;class Foo::Bar {   use Some::Thing;   has baz => ( ... );   method sample () { ... }}

Inherit

extends (@superclasses);extends 'My::Parent';extends 'My::Parent'      => { -version => 0.01 },        'My::OtherParent' => { -version => 0.03 };

Properties

has $name => (   is            => 'ro'|'rw',        # property is read-only or read-write, see notes below   isa           => $data_type,       # See "Data Types" below   required      => 0|1,              # see notes below   weak_ref      => 0|1,              # weaken the value, read weaken in Scalar::Util   lazy          => 0|1,              # create value using default/builder as late as possible   trigger       => sub { },          # run this code after a new value has been set   default       => $value | sub { }, # default value (scalar or return value of subroutine)   documentation => $text,            # an arbitrary string that can be retrieved later# from Class::MOP, rarely used. All values are method names, not CODE refs!   builder       => $method_name,     # return value is used as default value   accessor      => $method_name,     # used as $self->reader() and $self->writer($new_value)    predicate     => $method_name,     # returns true if the attribute has been explicitly set   reader        => $method_name,     # name of the method for reading this $name, see notes   writer        => $method_name,     # name of the method for writing this $name, see notes   clearer       => $method_name,     # used for uninitializing the property);has @names => ( same options as above );has 'foo' => ( options );has ['foo', 'bar', 'baz'] => ( options );

is

Properties marked as ro may be written by arguments passed to ->newdefault or builder.

required

Property must either be passed to ->new or have lazy set and a default value (using default or builder).

trigger

The trigger option is a CODE reference which will be called after the value of the attribute is set. The CODE ref is passed the instance itself, the updated value, and the original value if the attribute was already set. You can have a trigger on a read-only attribute.

reader/writer

These attributes do not define the sub/method used for reading or writing the property, like builder does for the default value. The default universal read/write accessor is named like the property (read-only accessor if is => 'ro'). The reader and writer attributes change the name of the read and write accessor methods for this property.

Methods

method foo () { ... }     # Same as sub foo { my $self = shift; ... }method foo ($foo) { ... } # Same as sub foo { my ($self, $foo) = @_; ... }

# from Moose::Manual::MethodModifiersbefore $name|@names|\@names|qr/.../ => sub { ... }after $name|@names|\@names|qr/.../ => sub { ... }around $name|@names|\@names|qr/.../ => sub { ... }

before 'foo' => sub { print "Before entering foo"; };after 'foo' => sub { print "After leaving foo"; };before 'foo' => sub { my $orig = shift; my $self = shift; print "before"; $self->$orig(@_); print "after";};

Prototypes/Signatures

from MooseX::Method::Signatures
method foo ( $affe)             # no type checkingmethod bar (Animal $affe)       # $affe->isa('Animal')method baz (Animal|Human $affe) # $affe->isa('Animal') || $affe->isa('Human')
method foo ( $a, $b, $c) # positionalmethod bar (:$a, :$b, :$c) # namedmethod baz ( $a, $b, :$c) # combined
method foo ($a , $b!, :$c!, :$d!) # requiredmethod bar ($a?, $b?, :$c , :$d?) # optional
method foo ($a = 42) # defaults to 42
method foo ($foo where { $_ % 2 == 0 }) # only even

Data types

from Moose::Util::TypeConstraints
  Any      Item          Bool          Maybe[`a]          Undef          Defined              Value                  Str                      Num                          Int                      ClassName                      RoleName              Ref                  ScalarRef[`a]                  ArrayRef[`a]                  HashRef[`a]                  CodeRef                  RegexpRef                  GlobRef                  FileHandle                  Object  ArrayRef[Int]    # an array of integers  HashRef[CodeRef] # a hash of str to CODE ref mappings  ScalarRef[Int]   # a reference to an integer  Maybe[Str]       # value may be a string, may be undefined
 

PS2: This post heavily uses the WordPress CPAN plugin.

 

2 Kommentare. Schreib was dazu

  1. [Imported from blog]

  2. [Imported from blog]

Schreib was dazu

Die folgenden HTML-Tags sind erlaubt:<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>