Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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

In reply to Re: Dear Moose docs... by tobyink
in thread Dear Moose docs... by 7stud

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2024-03-28 13:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found