FAQ

On 2014-03-28 21:56, Andy Bach wrote:
On Fri, Mar 28, 2014 at 3:05 PM, Dr.Ruud wrote:

if ($@) {
die "$@";
}


Never test $@, only use the value of $@ after a failed eval.


Why not?
Because $@ is a global variable, which can (AKA will) get changed
between the failure and your test.

So the answer is: Why gamble if you don't need to?


    my $obj;
    eval {
        $obj = get_obj();
        1; # success
    }
    or do {
        my $eval_error = $@ || "Zombie Error";
        warn "WARNING: ", $eval_error;
        ...;
    };


Recent Perls localize $@ more, so the odds of getting into trouble with
code that is testing $@, have become smaller over the years. Which is
good because there is plenty of code around that still does it.

--
Ruud

Search Discussions

Discussion Posts

Previous

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 11 of 11 | next ›
Discussion Overview
groupbeginners @
categoriesperl
postedMar 27, '14 at 1:31a
activeMar 29, '14 at 9:03a
posts11
users8
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase