Skip to content

Instantly share code, notes, and snippets.

@belden
Created March 18, 2013 14:55
Show Gist options
  • Save belden/5187723 to your computer and use it in GitHub Desktop.
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.
#!/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