Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

can't assign signal handler an undef value

by papidave (Monk)
on Dec 21, 2007 at 22:38 UTC ( #658559=perlquestion: print w/replies, xml ) Need Help??
papidave has asked for the wisdom of the Perl Monks concerning the following question:

Ok, this one has me entirely confused. Although I have a workaround (see below), the reason I even need a workaround escapes me.

The requirement is to be able to allow users to interrupt a certain operation (Ctrl-C) which may be called from an application that has $SIG{INT} set to 'IGNORE'. The initial code for this (Perl 5.8.8, Linux) was:

my $intr = 0; my $save_handler = $SIG{INT}; $SIG{INT} = sub { $intr++ }; # do something here ... $SIG{INT} = $save_handler; # check value of $intr to see if I was interrupted...

What I found was that the code worked properly when $SIG{INT} had been blocked, but if called from code that did not alter $SIG{INT}, I got slammed with a "Use of uninitialized value in scalar assignment" warning.

Since I want my code to run clean, I did some investigation. First, the key INT exists, and has an undefined value. But I get the warning when I try to assign an undefined value back into the hash.

I can avoid the assignment by using a local $SIG{INT}, e.g.:

my $intr = 0; { local $SIG{INT}; $SIG{INT} = sub { $intr++ }; # do something here ... } # end of scope for local handler # check value of $intr to see if I was interrupted...

... but this seems entirely bogus. Can someone suggest a good reason why %SIG would be initialized with values that I can't assign to it? I don't get this warning when I do a simple "$foo{INT} = undef;"

Replies are listed 'Best First'.
Re: can't assign signal handler an undef value
by shmem (Canon) on Dec 21, 2007 at 23:20 UTC
    Nothing special about the %SIG hash, but about assigning an undefined value. Try
    my $intr = 0; my $save_handler = $SIG{INT}; $SIG{INT} = sub { $intr++ }; # do something here ... $SIG{INT} = $save_handler || ''; # assign handler back if true, els +e '' # check value of $intr to see if I was interrupted...


    Well, there is something special about the %SIG hash. It's not just a normal hash, because it is the %SIG hash. Assignments have to be defined (an empty string is false, but still defined), and the keys are fixed. E.g. you can't just make up signals:

    qwurx [shmem] ~ > perl -wle '$SIG{BLORF} = undef' No such signal: SIGBLORF at -e line 1.

    You could also say

    my $save_handler = $SIG{INT} || '';

    to have a defined (but empty) value in $save_handler.


    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: can't assign signal handler an undef value
by sgifford (Prior) on Dec 22, 2007 at 05:46 UTC
    I see the same behavior on my system, and I agree it's very strange. Your workaround is cleaner than your original solution, though, in case something die's.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://658559]
Approved by Joost
What's the matter? Cat got your tongue?...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2017-04-24 05:35 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (433 votes). Check out past polls.