Last active
September 14, 2020 22:38
-
-
Save tobyink/183a1581c4f8f40a4e672f29bc0ade9e to your computer and use it in GitHub Desktop.
Example from the Zydeco pod, rewritten for Zydeco::Lite
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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