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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |