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.