Skip to content

Instantly share code, notes, and snippets.

@tobyink
Last active September 14, 2020 21:12
Show Gist options
  • Save tobyink/155c4c43712339ecdbcfc7d77335af88 to your computer and use it in GitHub Desktop.
Save tobyink/155c4c43712339ecdbcfc7d77335af88 to your computer and use it in GitHub Desktop.
Example from the Zydeco pod, rewritten for MooX::Press
use 5.008008;
use strict;
use warnings;
use Types::Standard qw( Str );
use MooX::Press (
prefix => 'MyApp',
class => [
'Person' => {
has => [
'name' => [ type => Str, required => 1 ],
'gender' => [ type => Str ],
],
factory => [
'new_man' => {
signature => [ Str ],
code => sub {
my ( $factory, $class, $name ) = ( shift, shift, @_ );
return $class->new( name => $name, gender => 'male' );
},
},
'new_woman' => {
signature => [ Str ],
code => sub {
my ( $factory, $class, $name ) = ( shift, shift, @_ );
return $class->new( name => $name, gender => 'female' );
},
},
],
can => [
'greet' => {
signature => [ 'friend' => 'Person', 'greeting' => Str, { default => 'Hello'} ],
named => 1,
code => sub {
my ( $self, $arg ) = ( shift, @_ );
printf( "%s, %s!\n", $arg->greeting, $arg->friend->name );
},
},
],
coerce => [
Str, 'from_string' => sub {
my ( $class, $string ) = ( shift, @_ );
return $class->new( name => $string );
},
],
},
],
);
'MyApp'->new_woman( 'Alice' )->greet( friend => 'Bob' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment