Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

is there a way to avoid dieing ?

by palkia (Monk)
on Apr 27, 2011 at 02:24 UTC ( [id://901451]=perlquestion: print w/replies, xml ) Need Help??

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

hello

In case you've wondered, I'm not asking about biological immortality (although I wouldn't object to an answers for that too).
I'm using a module that sometimes uses the die function and shutting down my program all together.
My question is: Is there a way for me to call a function from that module (without changing it of course), and instead of dieing, having the string printed by that preformed die function, returned instead ?
I guess I want something like this:

my $die_Message = immortal( some_module_function( $param1 , $param2 , .. ) );
so if some_module_function died, the program wouldn't and instead store the die message in $die_Message,
and if it didn't die, it would simply preform the module's function exactly like it would if "immortal" wasn't there (don't care much about the return in this scenario).

thx

Replies are listed 'Best First'.
Re: is there a way to avoid dieing ?
by BrowserUk (Patriarch) on Apr 27, 2011 at 02:46 UTC

    See the block form of eval:

    sub iDieSometimes{ rand() > .5 ? return 12345 : die "T'is a far, far...Oh shit! Aaaaaaaargh|" };; $n = eval{ iDieSometimes() } and print "returned $n" or print "It died + with: $@";; It died with: T'is a far, far...Oh shit! Aaaaaaaargh| at (eval 11) lin +e 1, <STDIN> line 10. $n = eval{ iDieSometimes() } and print "returned $n" or print "It died + with: $@";; It died with: T'is a far, far...Oh shit! Aaaaaaaargh| at (eval 11) lin +e 1, <STDIN> line 11. $n = eval{ iDieSometimes() } and print "returned $n" or print "It died + with: $@";; returned 12345 $n = eval{ iDieSometimes() } and print "returned $n" or print "It died + with: $@";; returned 12345 $n = eval{ iDieSometimes() } and print "returned $n" or print "It died + with: $@";; returned 12345 $n = eval{ iDieSometimes() } and print "returned $n" or print "It died + with: $@";; It died with: T'is a far, far...Oh shit! Aaaaaaaargh| at (eval 11) lin +e 1, <STDIN> line 15.

    Note: That assumes that the sub cannot legitimately return a false value like 0, or undef.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: is there a way to avoid dieing ?
by ikegami (Patriarch) on Apr 27, 2011 at 02:48 UTC
    if (!eval { some_module_function( $param1 , $param2 , .. ); 1 }) { my $die_Message = $@; ... }
    or
    use Try::Tiny qw( try catch ); try { some_module_function( $param1 , $param2 , .. ); } catch { my $die_Message = $_; ... };

    eval, Try::Tiny

Re: is there a way to avoid dieing ?
by Khen1950fx (Canon) on Apr 27, 2011 at 02:29 UTC
Re: is there a way to avoid dieing ?
by planetscape (Chancellor) on Apr 27, 2011 at 06:42 UTC
Re: is there a way to avoid dieing ?
by anonymized user 468275 (Curate) on Apr 27, 2011 at 11:32 UTC
    I wouldn't be likely to use No::Die because it allows execution of code where the programmer determined that conditions were unsuitable for this. It seems most unlikely that disabling die will lead to correct or even tolerable behaviour.

    The cheapest safe way seems to me to be to inherit the module into a wrapper class where the offending method is overridden (could for instance be copied to a local version in an own module and the problem fixed there) and to use objects from that class instead.

    Update: Exception::Died looks like a reasonable approach, although I would still expect method overriding to give me what I actually needed

    One world, one people

Re: is there a way to avoid dieing ?
by mertserger (Curate) on Apr 27, 2011 at 09:53 UTC
    "After all, to the well-organized mind, death is but the next great adventure." Albus Dumbledore
      "I don't want to achieve immortality through my work. I want to achieve it through not dying." -- Woody Allen
      Death is your turn to be luch -- Anonymous Monk
Re: is there a way to avoid dieing ?
by doug (Pilgrim) on Apr 27, 2011 at 18:50 UTC

    Wrap the immortal code in an eval. When it hits die(), the eval() exits and you get the text in $@. Run perldoc -f eval for more details.

    To get a bit fancier, setup a %SIG signal handler to catch (and ignore) die.

    And no intro to immortality is complete without mentioning that no scheme is 100% reliable. If someone wants to kill -9, $$ then you are going down in flames.

    - doug

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-03-29 01:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found