Skip to content

Instantly share code, notes, and snippets.

@tobyink
Last active September 14, 2020 22:38
Show Gist options
  • Select an option

  • Save tobyink/183a1581c4f8f40a4e672f29bc0ade9e to your computer and use it in GitHub Desktop.

Select an option

Save tobyink/183a1581c4f8f40a4e672f29bc0ade9e to your computer and use it in GitHub Desktop.
Example from the Zydeco pod, rewritten for Zydeco::Lite
use 5.008008;
use strict;
use warnings;
use Zydeco::Lite;
use Types::Standard qw( Str );
app 'MyApp' => sub {
class 'Person' => sub {
has 'name' => ( type => Str, required => true );
has 'gender' => ( type => Str );
factory 'new_man'
=> [ Str ]
=> sub {
my ( $factory, $class, $name ) = ( shift, shift, @_ );
return $class->new( name => $name, gender => 'male' );
};
factory 'new_woman'
=> [ Str ]
=> sub {
my ( $factory, $class, $name ) = ( shift, shift, @_ );
return $class->new( name => $name, gender => 'female' );
};
method 'greet'
=> [ 'friend' => 'Person', 'greeting' => Str, { default => 'Hello' } ]
=> ( named => true )
=> 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