Created
March 18, 2013 14:55
-
-
Save belden/5187723 to your computer and use it in GitHub Desktop.
Whenever I look at Patterns::UndefObject I want it to act more like this instead.
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
#!/usr/bin/env perl | |
{ | |
package maybe; | |
use overload 'bool' => sub { 0 }; | |
sub new { bless \$_[0] } | |
sub DESTROY {} | |
sub AUTOLOAD { shift } | |
} | |
sub maybe (&) { | |
my ($code) = @_; | |
no dying; | |
return hook_run( | |
before => sub { }, | |
run => $code, | |
after => sub { return maybe->new if died() } | |
); | |
} | |
use Maybe qw(maybe); | |
my $user = maybe { User->find(100) }; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment