While working on How's your Perl?, I was trying to find a way to create a blessed object without using bless. I stumbled across a couple:
open my $foo, ">&=STDOUT"; $foo = *$foo{IO}; print ref $foo
creates a IO handle with class IO::Handle (but doesn't actually load that module, which would violate the rules). (For backward compatibility, the class is actually FileHandle if you have used FileHandle and IO::Handle otherwise.)

Then I recalled a less intrusive way to do it:

select(select(my $foo)); $foo = *$foo{IO}; print ref $foo
There is actually a perl core module that uses this; I put the code there because I wanted to have a test in the core for this way of creating an IO handle without open, etc.

Unfortunately, this creates a second-class object. It doesn't honor overload in IO::Handle or FileHandle:

{ package FileHandle; use overload q[""]=>sub {"string me!"} } select(select(my $foo)); $foo = *$foo{IO}; print "$foo";
gives FileHandle=IO(0xa05c058) instead of the desired "string me!".

Then a little browsing through the perl source brought this builtin way to do it:

{ package PerlIO::Layer; use overload q[""]=>sub {"string me!"} } $foo = PerlIO::Layer->find("raw"); print "$foo"; # Update: this only works for 5.8.x (actually only tested on 5.8.2) # and not even then if you've built a perl without layers support.
Again, this doesn't actually load any module or use bless, but this time the overloading works. (Obviously overload itself is a module, but it is a pure perl module and you can duplicate how it works; getting that to work under use strict may be troublesome, but I think it is possible.)

My current attempts at answers are on my scratch pad. I've so far avoided looking at other's answers while I haven't given up hope on the remaining problems, so please no spoilers here (though comments on my answers so far would be nice).

Update: removed useless BEGIN's, double print


In reply to blessless blessing by ysth

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.