develooper Front page | perl.perl5.porters | Postings from May 2021

$^S value for try/catch blocks

From:
=?UTF-8?B?0JzQuNGF0LDQuNC7INCa0L7Qt9Cw0YfQutC+0LI=?=
Date:
May 18, 2021 20:34
Subject:
$^S value for try/catch blocks
Message ID:
8a504f46-1b10-3062-a94b-33e495632ab6@gmail.com
use strict;
use warnings;

use feature 'try';
use feature 'say';

no warnings 'experimental';

$SIG{__DIE__} = sub {
     say "\$^S: $^S";
};

try {
     die 'I have to go';
}
catch ($e) { }

exit;

Output:
$^S: 1

Whereas pervar says:

> 
>        $^S     Current state of the interpreter.
> 
>                        $^S         State
>                        ---------   -------------------------------------
>                        undef       Parsing module, eval, or main program
>                        true (1)    Executing an eval
>                        false (0)   Otherwise
> 
>                The first state may happen in $SIG{__DIE__} and $SIG{__WARN__} handlers.
> 
>                The English name $EXCEPTIONS_BEING_CAUGHT is slightly misleading, because the "undef" value does not indicate whether exceptions are being caught, since
>                compilation of the main program does not catch exceptions.
> 
>                This variable was added in Perl 5.004.



It means that "die" call occured in a eval, but try/catch blocks aren't 
eval blocks (in the usual sense).

May be it make sense to add some explanations to pod about it and also 
fix the $^S value when die happens in try/catch blocks (especially try)?



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About