All,
I had this idea pop into my head about being able to catch and re-throw exceptions, concatenate them, get a history, etc by tying $@. I figured I would TIAS (Try It And See).
package Exception; sub TIESCALAR { bless {}, $_[0] } sub STORE { print "Setting the value of ", '$@', "\n" } sub FETCH { 42 } package main; tie $@, 'Exception' or die "Unable to do what you want\n"; my ($foo, $bar) = (0, 0); eval { $foo = 42 / $bar }; print "The value is : $@\n"; $@ = 'blah '; print "Now the value is : $@\n";
Before running the code, you might expect a few things to happen: If you picked option 4, you would be correct:

When the eval catches the illegal by 0 exception, $@ is set and yet the STORE code is not called.
When the subsequent print occurs, the value is not 42, but the actual exception and the FETCH code is not called.
When I set $@ to 'blah', it begins acting as you might expect running the STORE code.
In the last print statement it is still behaving normally (running the FETCH code).

It reverts back to the weird behavior if you do another eval/print. I can imagine the eval setting of the variable bypassing the tied interface, but how does it know to avoid it when printing in some cases but not others? Admittedly, I spent no time investigating this but does anyone want to speculate on this behavior?

Cheers - L~R


In reply to 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.