Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Best way to replace 'die' with 'warn'

by dsheroh (Monsignor)
on Dec 06, 2009 at 10:22 UTC ( #811317=note: print w/replies, xml ) Need Help??


in reply to Best way to replace 'die' with 'warn'

The basic issue here, I think, is that, in Perl, "try" is spelled "eval" and "throw" is spelled "die". You've just run into the Perl exception-handling model and wrapping the call in eval is how this is intended to be handled.

So, what to do about it?

The most bare-bones way to deal with it would be

eval { weather_subroutine(); } or do { warn $@; }; # <-- Note trailing semicolon; it *does* matter
You could go even more minimal with
eval { weather_subroutine(); } warn $@ if $@;
but that has some weird edge cases where $@ can get messed up, so the eval { } or do { }; construct is more reliable.

If you want something more robust and/or with more familiar spellings, take a look at TryCatch or Try::Tiny. (The Try::Tiny synopsis shows how to use it to turn an exception into a warning, so you could even lift that example straight out of the docs.)

Replies are listed 'Best First'.
Re^2: Best way to replace 'die' with 'warn'
by puterboy (Scribe) on Dec 06, 2009 at 10:56 UTC
    Thanks - the bare bones method looks nice and seems to do exactly what I need.
Re^2: Best way to replace 'die' with 'warn'
by puterboy (Scribe) on Dec 06, 2009 at 18:13 UTC
    OK. I just uncovered an "unintended" side effect when I tried to apply this technique in a converse fashion *another* script where instead of trying to prevent the script from exiting on "die", I only want to prepend an action to occur before exiting. Since this case in a sense the converse of the case in this thread, to avoid confusion, I am asking the full question in a new thread id=//811358.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://811317]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (10)
As of 2020-10-19 15:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (205 votes). Check out past polls.

    Notices?