in reply to Re: Where did $@ go?
in thread Where did $@ go?

I commented out the try and put an eval back in, and after the call got:
SV = PV(0x187eb88) at 0x1880ce8 REFCNT = 1 FLAGS = (ROK) RV = 0x55b67b0 SV = PVHV(0x55aa5f0) at 0x55b67b0 REFCNT = 1 FLAGS = (OBJECT,OVERLOAD,SHAREKEYS) STASH = 0x49f1540 "Email::Sender::Failure" ARRAY = 0x55dfed0 (0:3, 1:3, 2:2) hash quality = 111.4% KEYS = 7 FILL = 5 MAX = 7 RITER = -1 EITER = 0x0 Elt "stack_trace" HASH = 0x7848e70 SV = PVMG(0x56b3b30) at 0x56778c0 REFCNT = 1 FLAGS = (ROK) IV = 0 NV = 0 RV = 0x56779f8 SV = PVHV(0x55aa708) at 0x56779f8 REFCNT = 1 FLAGS = (OBJECT,OVERLOAD,SHAREKEYS) STASH = 0x567ad90 "Devel::StackTrace" ARRAY = 0x55dbba0 (0:5, 1:3) hash quality = 150.0% KEYS = 3 FILL = 3 MAX = 7 RITER = -1 EITER = 0x0 Elt "index" HASH = 0x91501139 SV = NULL(0x0) at 0x56779e0 REFCNT = 1 FLAGS = () PV = 0x56779f8 "" CUR = 0 LEN = 0 Elt "_recipients" HASH = 0x82243041 SV = RV(0x56b5af8) at 0x56b5ae8 REFCNT = 1 FLAGS = (ROK) RV = 0x56b5a58 SV = PVAV(0x5675d40) at 0x56b5a58 REFCNT = 1 FLAGS = () ARRAY = 0x258b8a0 FILL = 0 MAX = 0 ARYLEN = 0x0 FLAGS = (REAL) Elt No. 0 SV = PV(0x56ae658) at 0x56b5ab8 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x56a4ff0 "sales@LEDsbythefoot.com"\0 CUR = 23 LEN = 24 Elt "stack_trace_class" HASH = 0x850b24fb SV = PV(0x527e468) at 0x5677638 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x55dd2f0 "Devel::StackTrace"\0 CUR = 17 LEN = 24 PV = 0x55b67b0 "" CUR = 0 LEN = 0
I changed it to Dump($@) if $@; to make sure it was still testing as true, and then got a shorter dump:
SV = PV(0x187eb88) at 0x1880ce8 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x5592da0 ""\0 CUR = 0 LEN = 8
So... testing it as a bool will destroy it! Am I reading that right?

Replies are listed 'Best First'.
Re^3: Where did $@ go?
by ikegami (Patriarch) on Mar 22, 2011 at 03:06 UTC

    I'm not sure what I was hoping to find out, but I can tell you this: Email::Sender::Failure overloads stringification with fallback, which means the object gets stringified on testing it as a bool. $@ appears to be getting clobbered, which could happen in the stringification code.

    I'm pretty sure that only referencing $@ once will do the trick:

    if (my $e = $@) { ... use $e here ... }

    And if not, this should definitely do the trick:

    if (my $e = "$@") { ... use $e here ... }

    That should get the value the if was seeing.

    (You can move the assignment out of the "if" if you prefer.)

      That's what I'm thinking too: The overloaded bool for the object itself must contain eval blocks.

      So, the classic code as shown in the Camel Book simply can't work. If you don't use Try::Tiny (or compatible system? Do they interoperate properly?), you can only use $@ once.

      Now, as I'm updating my idioms, I'll just use Try::Tiny. And if editing old code, it might be necessary to assign $@ to a local first, and change subsequent uses. If a function changes so it throws a fancy object now, or uses Moose in the new version, old code that calls something that calls something that calls the function which throws an exception will be broken.

      IMO, This specific case (stringification and boolification of the exception object) should take more care not to clobber $@. After all, one of the things Try::Tiny claims to do is localize $@. The guts of the Failure object needs to use that!

        So, the classic code as shown in the Camel Book simply can't work. If you don't use Try::Tiny (or compatible system? Do they interoperate properly?), you can only use $@ once.
        Once? Try not at all. See the other messages in this thread. Pre-5.14, $@ is unreliable (and post 5.14, your problem won't magically solve itself - and I doubt Try::Tiny can determine why a block died without looking at $@). Never use $@ to determine whether an eval succeeded or not. Just use its return value. I'm not saying one shouldn't use Try::Tiny, but I do want to point out that safely determining whether an eval succeeded or not does not require a module:
        eval { ... your code goes here ... 1; } or do { my $err = $@ || "Unknown error"; ... error handling ... };

        This specific case (stringification and boolification of the exception object) should take more care not to clobber $@.

        And handling of other cases is improved in 5.14.