btw, works in
Active Perl v5.6.1 built for MSWin32-x86-multi-thread, build 635 and
Active Perl v5.8.0 built for MSWin32-x86-multi-thread, build 806.

Explanation

perl -e "sub IO::Handle::DESTROY { print("""destroying $_[0]\n""") } $ +a=bless([]);"

outputs:

destroying IO::Handle=IO(0x18214b0) destroying IO::Handle=IO(0x1821570) destroying IO::Handle=IO(0x18215dc)

What you are seeing is STDIN, STDOUT and STDERR being destroyed. Without $anyvar = bless $anyref;, it doesn't work. bless called in a void context doesn't work either. bless is triggering the magic that associates STDOUT and pals to IO::Handle.

davido used other trickery, such as placing the sub in the do, which doesn't make it go away at the end of the do as you might think.

Further trickery is the appearance that both $i refer to the same variable. The $i in the do block is $main::i, while the $i in the DESTROY handler refers to $IO::Handle::i.

$_, on the other hand, it's not associated with any package. Talk about lots of different scopes for such a small program!

So,

sub IO::Handle::DESTROY { !$IO::Handle::i++ and print($_); } # Abracadra, Associat STDIN, STDOUT and STDERR with IO::Handle. $anyvar = bless([]); $_ = "Just another Perl hacker.\n"; # IO::Handle::DESTROY is now called for STDIN, STDOUT and STDERR.


In reply to Re: Package scope SPOILER by ikegami
in thread Package scope by davido

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.