On Fri Mar 09 15:20:13 2018, haarg wrote:
Show quoted text> How is the inside_eval function different from testing the $^S variable?
They differ in various subtle ways (all examples with 5.12.4):
Compliing main program (__DIE__):
$ perl -Ilib -MEval::Util=inside_eval -le 'BEGIN { $SIG{__DIE__} = sub { print "\$^S: ", $^S // "undef"; print "inside_eval: ", inside_eval } } $a+'
$^S: undef
inside_eval: 0
syntax error at -e line 1, at EOF
Execution of -e aborted due to compilation errors.
Compiling module (__DIE__):
$ echo '$a+'>SyntaxError.pm
$ perl -I. -Ilib -MEval::Util=inside_eval -le 'BEGIN { $SIG{__DIE__} = sub { print "\$^S: ", $^S // "undef"; print "inside_eval: ", inside_eval } } require SyntaxError'
$^S: 0
inside_eval: 0
syntax error at SyntaxError.pm line 1, at EOF
Compilation failed in require at -e line 1.
Dying in module’s main CV:
$ echo 'die'>Die.pm
bash-3.2$ perl -Ilib -MEval::Util=inside_eval -le 'BEGIN { $SIG{__DIE__} = sub { print "\$^S: ", $^S // "undef"; print "inside_eval: ", inside_eval } } require Die'
$^S: 0
inside_eval: 1
$^S: 0
inside_eval: 0
Died at Die.pm line 1.
Compilation failed in require at -e line 1.
(The second invocation is after require() re-throws the error.)
Inside string eval:
$ perl -Ilib -MEval::Util=inside_eval -le 'eval q{ print "\$^S: ", $^S // "undef"; print "inside_eval: ", inside_eval }'
$^S: 1
inside_eval: 1
Inside blocky eval:
$ perl -Ilib -MEval::Util=inside_eval -le 'eval { print "\$^S: ", $^S // "undef"; print "inside_eval: ", inside_eval }'
$^S: 1
inside_eval: 1
Die inside eval:
$ perl -Ilib -MEval::Util=inside_eval -le 'BEGIN { $SIG{__DIE__} = sub { print "\$^S: ", $^S // "undef"; print "inside_eval: ", inside_eval } } eval "die"'
$^S: 1
inside_eval: 1
BEGIN block in main program:
$ perl -Ilib -MEval::Util=inside_eval -le 'BEGIN { print "\$^S: ", $^S // "undef"; print "inside_eval: ", inside_eval }'
$^S: undef
inside_eval: 1
BEGIN block in module:
$ echo 'BEGIN { print "\$^S: ", $^S // "undef"; print "inside_eval: ", inside_eval }'>Begin.pm
$ perl -Ilib -MEval::Util=inside_eval -le 'require Begin'
$^S: undef
inside_eval: 1
Begin.pm did not return a true value at -e line 1.
BEGIN block in string eval:
$ perl -Ilib -MEval::Util=inside_eval -le 'eval q|BEGIN { print "\$^S: ", $^S // "undef"; print "inside_eval: ", inside_eval }|'
$^S: undef
inside_eval: 1
This means that for the various cases where $^S returns undef, you can use inside_eval to tell whether errors will be caught.