Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor the way we run the coderefs with an eye towards finally
- Loading branch information
Showing
5 changed files
with
107 additions
and
55 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,89 @@ | ||
package TryCatch::Exception; | ||
|
||
use strict; | ||
use warnings; | ||
use Moose; | ||
|
||
use MooseX::Types::Moose qw/CodeRef ArrayRef/; | ||
|
||
use Scope::Upper qw/unwind want_at :words/; | ||
use namespace::clean; | ||
use namespace::clean -except => 'meta'; | ||
|
||
sub catch { | ||
my ($self, @conds) = @_; | ||
my $sub = pop @conds; | ||
die "no code to catch!" unless ref $sub && ref $sub eq 'CODE'; | ||
|
||
local $@; | ||
for my $cond (@conds) { | ||
if (ref $cond) { | ||
local *_ = \$self->{error}; | ||
return $self unless $cond->(); | ||
} | ||
else { | ||
my $tc = TryCatch->get_tc($cond); | ||
return $self unless $tc->check($self->{error}); | ||
} | ||
|
||
has try => ( | ||
is => 'ro', | ||
isa => CodeRef, | ||
required => 1 | ||
); | ||
|
||
has catches => ( | ||
is => 'ro', | ||
isa => ArrayRef[ArrayRef[CodeRef]], | ||
default => sub { [] } | ||
); | ||
|
||
has ctx => ( | ||
is => 'ro', | ||
required => 1 | ||
); | ||
|
||
our $CTX; | ||
|
||
sub _run_block { | ||
my ($self, $code) = @_; | ||
|
||
my $wa = want_at $CTX; | ||
if ($wa) { | ||
my @ret = $code->(); | ||
} elsif (defined $wa) { | ||
my $ret = $code->(); | ||
} else { | ||
$code->(); | ||
} | ||
} | ||
|
||
# If we get here then the conditions match | ||
sub run { | ||
my ($self) = @_; | ||
local $CTX = $CTX; | ||
my $ctx = $CTX; | ||
|
||
my $ctx = want_at SUB(CALLER(1)); | ||
unless (defined $CTX) { | ||
$CTX = $ctx = $self->ctx; | ||
} | ||
|
||
local $@; | ||
eval { | ||
$@ = $self->{error}; | ||
if ($ctx) { | ||
my @ret = $sub->(); | ||
} elsif (defined $ctx) { | ||
my $ret = $sub->(); | ||
} else { | ||
$sub->(); | ||
} | ||
$self->_run_block($self->try); | ||
}; | ||
|
||
return "TryCatch::Exception::Handled"; | ||
# If we get here there was either no explicit return or an error | ||
return unless defined($@); | ||
my $err = $@; | ||
|
||
CATCH: for my $catch ( @{$self->catches} ) { | ||
my $sub = pop @$catch; | ||
for my $cond (@$catch) { | ||
if (ref $cond) { | ||
local *_ = \$err; | ||
next CATCH unless $cond->(); | ||
} | ||
else { | ||
my $tc = TryCatch->get_tc($cond); | ||
next CATCH unless $tc->check($err); | ||
} | ||
|
||
} | ||
|
||
$self->_run_block($sub); | ||
} | ||
|
||
return; | ||
} | ||
|
||
package TryCatch::Exception::Handled; | ||
sub catch { | ||
my ($self, @conds) = @_; | ||
push @{$self->catches}, [@conds]; | ||
return $self; | ||
|
||
} | ||
|
||
sub catch {} | ||
__PACKAGE__->meta->make_immutable; | ||
|
||
1; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use strict; | ||
use warnings; | ||
use Test::More tests => 2; | ||
|
||
BEGIN { use_ok "TryCatch" or BAIL_OUT("Cannot load TryCatch") }; | ||
|
||
sub nested_1 { | ||
try { | ||
try { | ||
return "from nested_1"; | ||
} | ||
catch ($e) { | ||
} | ||
} | ||
} | ||
|
||
is( nested_1(), "from nested_1", "nested return"); |