in reply to Re: Re3: How to detect a die() without catching it?
in thread How to detect a die() without catching it?

Well I was a bit hasty in calling it recursion, as it isn't really.

So actually you want the opposite (in a manner of speaking) of what you said, you wish to artificially enforce inheritance of die as *exceptions*. The code you give does exactly what it is supposed to, there is no die called within catcher, and $SIG{__DIE__} is re-localed to the catcher2 scope.

I think Exception::Class is what you want for that. Alternatively, here's some ugly code

local @stack; $SIG{__DIE__} = sub { die(@stack, @_); }; catcher(); sub catcher { local @stack = (@stack, "Caught 1!\n"); catcher2(); } sub catcher2 { local @stack = (@stack, "Caught 2!\n"); sub_that_dies(); } sub sub_that_dies { die "Foo"; }

If, on the other hand, you actually want what's in yor original code, which is just to do something before (outter) dieing then I stand by the original suggestions, and also add END to the list; END output comes last though.

#=begin a require 'b'; print "Hello World\n"; #This die can also be replaced with print && exit $SIG{__DIE__} = sub { print "BOB!\n"; die(@_); }; a::a(); END{ print "Waka waka\n"; #=end a #=begin b package a; sub a{ die("A!"); } 1; #=end b __END__ Hello World BOB! A! at /tmp/b line 3. Waka waka

--
perl -pew "s/\b;([mnst])/'$1/g"

Replies are listed 'Best First'.
Re: Re: Re: Re3: How to detect a die() without catching it?
by samtregar (Abbot) on May 18, 2002 at 01:04 UTC
    You're probably correct that what I want are exceptions, but I don't think I can get them. I'm writing a profiler and it's supposed to work with a wide range of code, most of which will be using straight die()s.

    Thanks for the help!
    -sam