Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Dear Moose docs...

by tobyink (Abbot)
on Feb 10, 2013 at 08:51 UTC ( #1018035=note: print w/replies, xml ) Need Help??


in reply to Dear Moose docs...

You're right that the docs are a little sparse in that area. There are some examples though - Moose::Manual::Attributes, Moose::Cookbook::Basics::Point_AttributesAndSubclassing, Moose::Cookbook::Basics::BankAccount_MethodModifiersAndSubclassing, etc. If you don't think this is enough, please submit a bug report.

Out of the box, Moose supports two styles of object construction:

# Key-value pairs my $point1 = Point->new(x => 10, y => 20); # Hashref my $point2 = Point->new({ x => 10, y => 20 });

In your class it's possible to override BUILDARGS to add your own argument parsing...

use v5.12; { package Point; use Moose; has 'x' => (is => 'rw', isa => 'Int'); has 'y' => (is => 'rw', isa => 'Int'); around BUILDARGS => sub { my $orig = shift; my $self = shift; # Allow object to be built by passing two integers... if (@_ == 2 and $_[0] =~ /^[0-9]+$/ and $_[1] =~ /^[0-9]+$/) { return { x => $_[0], y => $_[1] }; } # If not two integers, then fall back to standard Moose BUILDA +RGS... return $self->$orig(@_); }; __PACKAGE__->meta->make_immutable; } # Use our overridden BUILDARGS... print Point->new(10, 20)->dump; # Standard Moose BUILDARGS is fallback... print Point->new(x => 10, y => 20)->dump; print Point->new({ x => 10, y => 20 })->dump;
package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1018035]
help
Chatterbox?
[shmem]: maybe that supports ctags, check the documentation
[Lady_Aleena]: I'd have to talk to a geany person to see.
[Lady_Aleena]: Right now, I'm trying to decide whether I want to rewrite how variables are entered in the sub I'm working on.
[shmem]: wait, what? you want that person to read the documentation for you?
[Lady_Aleena]: I might have to go with options instead of a straight list.
[Discipulus]: if more than 3 go for named variables LA
[Lady_Aleena]: Discipulus, it is at 2 now, but with what I am thinking about, it could go to 3. However, only 1 is needed. The second and third are optional.
[shmem]: straight list or named parameters - that depends on whether (and how many) optional arguments you have
[Discipulus]: if so a plain list is ok, imho
[Lady_Aleena]: shmem, I already have the plugin installed, just not active.

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (12)
As of 2017-04-27 12:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (506 votes). Check out past polls.