Skip to content

Instantly share code, notes, and snippets.

@tempire
Created August 29, 2012 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tempire/3518374 to your computer and use it in GitHub Desktop.
Save tempire/3518374 to your computer and use it in GitHub Desktop.
perl - return error objects or successful response using overload bool
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