Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Where did $@ go?

by Corion (Patriarch)
on Mar 21, 2011 at 08:22 UTC ( [id://894435]=note: print w/replies, xml ) Need Help??


in reply to Where did $@ go?

The hip way of doing this is to use Try::Tiny. Personally, I prefer the following approach, to make sure the code actually died:

my $ok = eval { $mailer->send(...); 1; }; if (! $ok) { my $err = $@; ... };

But even my approach does not explain to me why if( $@ ) would be true but "$@" resp. "$err" would be empty.

Replies are listed 'Best First'.
Re^2: Where did $@ go?
by John M. Dlugosz (Monsignor) on Mar 21, 2011 at 09:14 UTC
    Tracing back through the calls, I found "Try::Tiny". It claims to localize $@. But if it does funny things, it means the caller must use Try::Tiny as well to get sensible results! Does every try-ing module create a different dependency on its users? That would be a nightmare! I guess it "restores" it after the non-existent catch!

    Using Try::Tiny, I get the thrown object in the catch block.

    Even data dumper shows, in my original call, $@ to be ''. But the code branch is executed, so it is true.

    As for your example, the core docs say that if eval completes without error, then $@ is guaranteed to be a null string, so you can write eval{...}; warn if $@;. You should not need to ensure your block evaluates to true, by original design of the feature.

      As for your example, the core docs say that if eval completes without error, then $@ is guaranteed to be a null string
      Even if that would be true, then
      so you can write eval{...}; warn if $@;
      doesn't follow.

      For the second, you also need $@ to be true if the eval fails. Which doesn't need to be the case.

      The problem is end-of-scope effects. eval { } is a block. Leaving the block, whether due to reaching the end, entering a return, or because the code dies, triggers end of scope effects. Which may cause more code to be executed (think DESTROY blocks). Which may clear, or set $@.

      This is a known, but hard problem to solve (as one doesn't want to lose information carried in $@). There will be improvements in 5.14, IIRC.

        Re even if that were true:
        The eval docs state "If there was no error, $@ is guaranteed to be a null string."

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2024-03-28 11:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found