Created
August 29, 2012 20:22
-
-
Save tempire/3518374 to your computer and use it in GitHub Desktop.
perl - return error objects or successful response using overload bool
This file contains 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 Modern::Perl; | |
use Data::Dumper; | |
my $hello = Hello->new->hello($ARGV[0]); | |
print $hello || warn Dumper $hello->{messages}; | |
package Hello; | |
use Mojo::Base -base; | |
sub hello { | |
return (pop) ? "successful" : Error->new->error('hello error'); | |
}; | |
package Error; | |
use Mojo::Base -base; | |
use overload 'bool' => sub {()}; | |
has messages => sub{[]}; | |
sub error { | |
my $self = shift; | |
push @{$self->{messages}} => pop; | |
return $self; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment