Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

MooX::Press is a quick way of building a bunch of Moo roles and classes in one use statement.

The most basic example would be:

package MyApp { use MooX::Press class => ['Foo', 'Bar']; } my $thing1 = MyApp::Foo->new(); my $thing2 = MyApp->new_foo(); # alternative constructor

But do-nothing classes with a constructor and nothing else aren't very exciting. Let's define a class with some subclasses which have attributes and roles and methods and stuff.

package MyApp::Zoo; use MooX::Press ( role => [ 'Aquatic' => { can => [ swim => sub { print "swimming\n" }, ], }, 'Flight', ], class => [ 'Animal' => { has => [qw( $name $colour $age )], subclass => [ 'Fish' => { with => 'Aquatic', subclass => [qw( Shark Ray )], }, 'Bird' => { with => 'Flight' }, 'Mammal' => { subclass => [ qw( Panda Goat ), 'Kangaroo' => { can => [ jump => sub { ... } ] }, 'Dolphin' => { with => 'Aquatic' }, 'Bat' => { with => 'Flight' }, ], }, ], }, ], );

The above code just defined the following roles:

  • MyApp::Zoo::Aquatic
  • MyApp::Zoo::Flight

And the following classes:

  • MyApp::Zoo::Animal
  • MyApp::Zoo::Fish
  • MyApp::Zoo::Shark
  • MyApp::Zoo::Ray
  • MyApp::Zoo::Bird
  • MyApp::Zoo::Mammal
  • MyApp::Zoo::Panda
  • MyApp::Zoo::Goat
  • MyApp::Zoo::Kangaroo
  • MyApp::Zoo::Dolphin
  • MyApp::Zoo::Bat

All with the appropriate attributes and roles applied to them.

Also, it defined a package called MyApp::Zoo::Types with class and role type constraints already set up.

So you can do:

use Moo; use MyApp::Zoo::Types qw(Kangaroo); has mascot => (is => 'ro', isa => Kangaroo);

Or:

use MyApp::Zoo::Types qw(is_Kangaroo); $thing->jump if is_Kangaroo($thing);

Here's some more code using our zoo classes...

use MyApp::Zoo (); my $lenny = MyApp::Zoo->new_shark(name => 'Lenny'); $lenny->isa('MyApp::Zoo::Shark'); # true $lenny->isa('MyApp::Zoo::Fish'); # true $lenny->isa('MyApp::Zoo::Animal'); # true $lenny->does('MyApp::Zoo::Aquatic'); # true $lenny->can('swim'); # true package MyApp::Zoo::Enclosure::Tank { use Moo; use Types::Standard qw(ArrayRef); use MyApp::Zoo::Types qw(Aquatic); has animals => ( is => 'rw', isa => ArrayRef[Aquatic], ); } my $tank = MyApp::Zoo::Enclosure::Tank->new( animals => [ $lenny ], );

MooX::Press is on CPAN.


In reply to Announcing MooX::Press by tobyink

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (8)
    As of 2020-07-12 20:44 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?