Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Localizing %SIG

by shmem (Chancellor)
on Feb 17, 2016 at 12:33 UTC ( [id://1155461]=note: print w/replies, xml ) Need Help??


in reply to Localizing %SIG

I expected this to just turn off all handlers. Why do i need to write local $SIG{__DIE__};? Can anybody explain this?

Localizing a hash just creates an empty alias, with no key whatsoever. With local $SIG{__DIE__} you explicitly set the __DIE__ handler to undef, so for this context, you get back the default behavior of die, and die is done, signal processed. If you just localize %SIG, the __DIE__ handler of the surrounding context is still in place, since the __DIE__ signal isn't handled by the current localized %SIG (there is no __DIE__ key in %SIG) - so it is passed up and hits the handler installed before the localization. Does that make sense to you?

Update: the question from the previous paragraph just indicates that the explanation is poor, i.e. uncomplete. Further then:

Localizing a hash just creates an empty alias, with no key whatsoever. The processing of a signal via a custom handler is dependent on the presence of the signal name as key in the hash %SIG.

If you say local $SIG{__DIE__}, you just alias the value of the slot __DIE__ of the %SIG currently in place. If you localize the entire %SIG without further ado, this has no effect whatsoever, since it has no keys. Aliasing doesn't create keys! So, if you write something like

$SIG{__DIE__}= sub {print "fubar!\n"}; eval { local %SIG; local $SIG{__DIE__}; $a/$b }; print $@;

you will see

fubar! Illegal division by zero at - line 5.

since the subsequent aliasing of an undefined value in the already localized hash %SIG doesn't create the key __DIE__ in %SIG.

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

Replies are listed 'Best First'.
Re^2: Localizing %SIG
by Anonymous Monk on Feb 17, 2016 at 13:25 UTC

    That makes perfect sense - but where is that behavior documented? It doesn't seem to be mentioned in %SIG or the Camel at least...

      "... where is that behavior documented?"

      The "Illegal division by zero" example is documented in eval. This has additional information about $SIG{__DIE__}.

      die also has information about $SIG{__DIE__}.

      More general information related to this topic can be found in local, perlsub, perlipc and, as already noted, perlvar.

      — Ken

        I meant specifically that local %SIG; does not appear to affect the signal handlers, even though it creates a local, empty %SIG, does not appear to be documented.

        use warnings; use strict; use Data::Dumper; local $SIG{__WARN__} = sub { warn "outer: $_[0]" }; warn "one\n"; { local $SIG{__WARN__} = sub { warn "inner 1: $_[0]" }; warn "two\n"; { local %SIG; warn "three\n"; print Dumper(\%SIG); { local $SIG{__WARN__} = sub { warn "inner 2: $_[0]" }; warn "four\n"; } } warn "five\n"; } warn "six\n"; __END__ outer: one inner 1: two inner 1: three $VAR1 = {}; inner 2: four five outer: six

        Okay, so the result for "five" isn't correct... looks like there really may be a bug somewhere.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1155461]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-19 11:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found