T
Tim McDaniel
Miscellaneous neepery on the subject of eval and Try::Tiny.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#! /usr/bin/perl
use strict;
use warnings;
use Try::Tiny;
use Data:umper;
my $eval = eval {die 'In eval'};
print Data:umper->Dump([$eval, $@], [qw[eval error]]), "\n=====\n";
my $error = '';
my $try = try { die 'In try with naive catch' } catch { $error = $_; };
print Data:umper->Dump([$try, $error], [qw[try error]]), "\n=====\n";
$try = try { die 'In try with larger catch' } catch { $error = $_; return };
print Data:umper->Dump([$try, $error], [qw[try error]]), "\n";
exit 0;
Output:
$ perl local/test/052.pl
$eval = undef;
$error = 'In eval at local/test/052.pl line 7.
';
=====
$try = 'In try with naive catch at local/test/052.pl line 11.
';
$error = 'In try with naive catch at local/test/052.pl line 11.
';
=====
$try = undef;
$error = 'In try with larger catch at local/test/052.pl line 14.
';
Indeed, checking the code as of 5.8.8, and the most recent source
under http://search.cpan.org/~doy/Try-Tiny-0.11/lib/Try/Tiny.pm, catch
is invoked with
for ($error) {
return $catch->($error);
}
(I think it's stupid as all hell, but whatever.) So I think I can
replace
some_lhs = eval { body };
# uses of $@
with
my $error;
some_lhs = try { body } catch { $error = $_; return };
# uses of $error
Is that right?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I've run across cases of
eval string_value
that I want to replace. Since the code for Try::Tiny localizes $@,
I think I can get away with
try { eval string_value; die $@ if $@ } catch { $error = $_; return }
Anyone see a problem with that?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#! /usr/bin/perl
use strict;
use warnings;
use Try::Tiny;
use Data:umper;
my $eval = eval {die 'In eval'};
print Data:umper->Dump([$eval, $@], [qw[eval error]]), "\n=====\n";
my $error = '';
my $try = try { die 'In try with naive catch' } catch { $error = $_; };
print Data:umper->Dump([$try, $error], [qw[try error]]), "\n=====\n";
$try = try { die 'In try with larger catch' } catch { $error = $_; return };
print Data:umper->Dump([$try, $error], [qw[try error]]), "\n";
exit 0;
Output:
$ perl local/test/052.pl
$eval = undef;
$error = 'In eval at local/test/052.pl line 7.
';
=====
$try = 'In try with naive catch at local/test/052.pl line 11.
';
$error = 'In try with naive catch at local/test/052.pl line 11.
';
=====
$try = undef;
$error = 'In try with larger catch at local/test/052.pl line 14.
';
Indeed, checking the code as of 5.8.8, and the most recent source
under http://search.cpan.org/~doy/Try-Tiny-0.11/lib/Try/Tiny.pm, catch
is invoked with
for ($error) {
return $catch->($error);
}
(I think it's stupid as all hell, but whatever.) So I think I can
replace
some_lhs = eval { body };
# uses of $@
with
my $error;
some_lhs = try { body } catch { $error = $_; return };
# uses of $error
Is that right?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I've run across cases of
eval string_value
that I want to replace. Since the code for Try::Tiny localizes $@,
I think I can get away with
try { eval string_value; die $@ if $@ } catch { $error = $_; return }
Anyone see a problem with that?