Skip to content

Instantly share code, notes, and snippets.

@amarnus
Last active December 21, 2015 09:16
Show Gist options
  • Save amarnus/161c30fe282871d544d3 to your computer and use it in GitHub Desktop.
Save amarnus/161c30fe282871d544d3 to your computer and use it in GitHub Desktop.
Returning from inside an eval expression in a subroutine
#!/usr/bin/env perl
use Carp::Assert;
sub extractOne {
return 3 - 2;
}
sub returnOne {
my $one = 0;
eval {
$one = extractOne;
};
### Remove this section and the assertion below will pass ###
### start of section ###
if ( $@ ) {
print STDERR "Error extracting 1...";
}
### end of section ###
}
assert( returnOne == 1, 'returnOne() should return 1' );
### Fix is to move the return statement outside the eval {} block or to
### return the value of the eval {} expression.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment