Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Signals and 'IGNORE'

by hotshot (Prior)
on Jan 06, 2003 at 10:28 UTC ( [id://224589]=perlquestion: print w/replies, xml ) Need Help??

hotshot has asked for the wisdom of the Perl Monks concerning the following question:

Hello guys!

Suppose I want my entire program to ignore SIGINT, I would have to add at the top of it the row:
$SIG{INT} = 'IGNORE';
So far so good. Is there a way to let SIGINT gain it's default behaviour? In several functions of mine I would like the function to catch this signal regulary. how do I do it (is it possible to do: $SIG{INT} = 'DEFAULT'; or something?).

It's the oposite case of when all the program use the default behaviour of SIGINT and in one function I need to ignore it and there I can use:
sub func { local $SIG{INT} = 'IGNORE'; ... }


Hotshot

Replies are listed 'Best First'.
Re: Signals and 'IGNORE'
by Zaxo (Archbishop) on Jan 06, 2003 at 10:54 UTC

    The default behavior of $SIG{INT}, the keyboard interrupt, is determined by the OS. On *nixen it is the system's process termination function, exit(), and you can probably rely on a system doing its own version of that if signals exist at all. The *nix behavior is POSIX standard.

    Perl does not set its own handler, so anything that leaves $SIG{INT} undefined will work.

    { local $SIG{INT}; }
    or maybe [for self-documentation - thanks to broquaint for asking]
    { undef local $SIG{INT}; }
    will do.

    After Compline,
    Zaxo

      or maybe [for self-documentation - thanks to broquaint for asking]
      { undef local $SIG{INT}; }
      will do.
      Yuck. Please don't advice people to write this kind of junk. Rather, teach them about Perl's default behaviour: if you localize a scalar variable, it gets the value undef — unless you assign something to it. So
      local $foo;
      should be self-documenting enough. (If it isn't: learn Perl!) But not this... perversion.
        Did you not read the part "or maybe [for self-documentation -"?

        undef local $SIG{INT} is clearer in intention than local $SIG{INT}. Reading other's code I might question if it was a piece of kruft (lavaflow, whatever there calling it now)

        -Lee

        "To be civilized is to deny one's nature."
Re: Signals and 'IGNORE'
by AnonymousNun (Initiate) on Jan 06, 2003 at 20:36 UTC
    Although set $SIG{INT} to undef is perfectly right.

    A more portable solution would be, to remember the value of $SIG{INT} before you set it to 'IGNORE', and restore the old value whenever you want it.

    By doing this, you reduce your dependency on Perl's future behavior changes.

    This is just about style.
      A more portable solution would be, to remember the value of $SIG{INT} before you set it to 'IGNORE', and restore the old value whenever you want it.
      That's precisely what local() is for.

      Over the years, people are so wrapped in the discussions of local vs. my, that they tend to forget what a great concept local really is. Unfortunately, it doesn't work on lexical scalars.

      my $foo; { local $foo; }
      Can't localize lexical variable $foo at ...
      Oddly enough, it does work on individual hash and array elements, even when those hashes and arrays are lexicals!
      my %hash = (a => 'one', b => 'two', c => 'three' ); use Data::Dumper; { local $hash{b} = 'Hello!'; print Dumper \%hash; } print Dumper \%hash;
      Unfortunately, if a localised hash element didn't exist in the outer scope, then when it's restored, it is merely set to undef, not deleted. So you end up with one hash element more than you had before. You can localise the entire hash — if it isn't a lexical, that is; but still...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-19 22:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found