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

help in $SIG

by uva (Sexton)
on Feb 09, 2006 at 06:00 UTC ( #529005=perlquestion: print w/replies, xml ) Need Help??
uva has asked for the wisdom of the Perl Monks concerning the following question:

hello monks,,
local $SIG{'__DIE__'} = sub { (my $x = $_[0]) =~ s/foo/bar/g; die $x }; eval { die "foo lives here" };
..... can you please explain the code. is $SIG should us with "_DIE_"? when ever die is used, it automatically invokes _DIE_,is it true? i read perl is using top down approach, i debugged the above program , the execution goes up to eval and jumps to $SIG it is possible?explain me please.

Replies are listed 'Best First'.
Re: help in $SIG
by sk (Curate) on Feb 09, 2006 at 06:32 UTC
    From the Camel Book -

    The routine indicated by $SIG{__DIE__} is called when a fatal exception is about to be thrown. The error message is passed as the first argument. When a __DIE__ hook routine returns, the exception processing continues as it would have in the absence of the hook, unless the hookroutine itself exits via a goto, a loop exit, or a die. The __DIE__ handler is explicitly disabled during the call, so that you yourself can then call the real die from a __DIE__ handler. (If it weren't disabled, the handler would call itself recursively forever.) The case is similar for __WARN__.

    When you call  die "something" the sub ref'ed in  $SIG{__DIE__} is called (you have an anonymous sub in your code). So the top-down approach is broken because your die called the subroutine with your die text as the first arg. If you modify your code to something like this, you will see what is going on -

    #!/usr/bin/perl use strict; use warnings; local $SIG{'__DIE__'} = sub { (my $x = $_[0]) =~ s/foo/bar/g; print $x +; die $x; }; eval { die "foo lives here" }; print "got here\n";


    bar lives here at testme line 7. got here
    As you can see foo changed to bar. Also the program prints got here becaue die was called from an eval block.

    As an exercise try deleting eval (just the word from the above code) see what happens -



      thanks SK,,can you give link for learning things like $SIG,$ENV.,etc., $SIG is inbuit one right?
        Check out  perldoc perlvar on your local machine. You can also use the online version of perldoc (nice formatting). Here is the link to perlvar online

        Check out Tutorials and Q&A links. They have very good info. cheers


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://529005]
Approved by sk
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2018-06-18 21:54 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (111 votes). Check out past polls.