To further this oddity:

package Exception; sub TIESCALAR { bless {}, $_[0] } sub STORE { print "Changing the value from '$@' to '$_[1]' (not really +)\n" } sub FETCH { print "$@\n";42 } package main; my $t = tie $@, 'Exception' or die "Unable to do what you want\n"; my ($foo, $bar) = (0, 0); $@ = "hi"; eval { $foo = 42 / $bar }; print "The value is : $@\n"; $t->STORE("taking a look"); print "Value is still: $@, though\n"; $@ = 'blah'; print "Now the value is : $@\n"; __END__ Changing the value from 'hi' to 'hi' (not really) The value is : Illegal division by zero at - line 10. Changing the value from 'Illegal division by zero at - line 10. ' to 'taking a look' (not really) Value is still: Illegal division by zero at - line 10. , though Changing the value from 'blah' to 'blah' (not really) blah Now the value is : 42

Strange that it is actually manipulating $@ in some fashion when you assign something to it.

Update: Ah, antirice showed me some neat tricks. The problem seems to be that the eval's capture is putting the POK flag (has valid public pointer value) on $@. So when you check $@, perl sees the POK flag and just prints what's contained in the PV. It also seems that the PV gets updated no matter what and that checking $@ in the STORE and FETCH are just getting the PV that was set. Funny thing is that the POK flag gets turned off again when you assign to $@ (guess it realizes it's magical once again?). Code and results follow:

# perl package Exception; sub TIESCALAR { bless {}, $_[0] } sub STORE { } sub FETCH { } package main; use Devel::Peek; my $t = tie $@, 'Exception' or die "Unable to do what you want\n"; my ($foo, $bar) = (0, 0); Dump($@); eval { $foo = 42 / $bar }; Dump($@); $@ = 'blah'; Dump($@); __END__ SV = PVMG(0x8135944) at 0x811b12c REFCNT = 1 FLAGS = (GMG,SMG,RMG,pPOK) IV = 0 NV = 0 PV = 0x8124100 ""\0 CUR = 0 LEN = 240 MAGIC = 0x8127e40 MG_VIRTUAL = &PL_vtbl_packelem MG_TYPE = PERL_MAGIC_tiedscalar(q) MG_FLAGS = 0x02 REFCOUNTED MG_OBJ = 0x81269a4 SV = RV(0x8140418) at 0x81269a4 REFCNT = 1 FLAGS = (ROK) RV = 0x811b240 SV = PVHV(0x812670c) at 0x811b240 REFCNT = 2 FLAGS = (OBJECT,SHAREKEYS) IV = 0 NV = 0 STASH = 0x81269ec "Exception" ARRAY = 0x0 KEYS = 0 FILL = 0 MAX = 7 RITER = -1 EITER = 0x0 SV = PVMG(0x8135944) at 0x811b12c REFCNT = 1 FLAGS = (GMG,SMG,RMG,POK,pPOK) IV = 0 NV = 0 PV = 0x8124100 "Illegal division by zero at - line 13.\12"\0 CUR = 39 LEN = 240 MAGIC = 0x8127e40 MG_VIRTUAL = &PL_vtbl_packelem MG_TYPE = PERL_MAGIC_tiedscalar(q) MG_FLAGS = 0x02 REFCOUNTED MG_OBJ = 0x81269a4 SV = RV(0x8140418) at 0x81269a4 REFCNT = 1 FLAGS = (ROK) RV = 0x811b240 SV = PVHV(0x812670c) at 0x811b240 REFCNT = 2 FLAGS = (OBJECT,SHAREKEYS) IV = 0 NV = 0 STASH = 0x81269ec "Exception" ARRAY = 0x8127e60 KEYS = 0 FILL = 0 MAX = 7 RITER = -1 EITER = 0x0 SV = PVMG(0x8135944) at 0x811b12c REFCNT = 1 FLAGS = (GMG,SMG,RMG,pPOK) IV = 0 NV = 0 PV = 0x8124100 "blah"\0 CUR = 4 LEN = 240 MAGIC = 0x8127e40 MG_VIRTUAL = &PL_vtbl_packelem MG_TYPE = PERL_MAGIC_tiedscalar(q) MG_FLAGS = 0x02 REFCOUNTED MG_OBJ = 0x81269a4 SV = RV(0x8140418) at 0x81269a4 REFCNT = 1 FLAGS = (ROK) RV = 0x811b240 SV = PVHV(0x812670c) at 0x811b240 REFCNT = 2 FLAGS = (OBJECT,SHAREKEYS) IV = 0 NV = 0 STASH = 0x81269ec "Exception" ARRAY = 0x8127e60 KEYS = 0 FILL = 0 MAX = 7 RITER = -1 EITER = 0x0

In reply to Re: Tying $@ - weird behavior by !1
in thread Tying $@ - weird behavior by Limbic~Region

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.