Hmm, this seems to be a trick with bless. At least that's all I can deduce from this snippet:

#!/usr/bin/perl -l *Bar:: = \*Foo::; print ref bless{}, "Bar"; __END__ Foo

Haven't seen anything about this in any of the docs but the behavior is consistent since at least 5.6.1.

Update: Even more curious...

#!/usr/bin/perl -l sub Foo::j { print "F ",ref shift } sub Bar::j { print "B ",ref shift } $a = bless {}, "Bar"; *Bar:: = \*Foo::; $b = bless {}, "Bar"; print "\$a is type ",ref $a; print "\$b is type ",ref $b; $a->j; Bar::j($a); *{"Bar::j"}{CODE}->($a); $b->j; __END__ $a is type Bar $b is type Foo B Bar B Bar F Bar F Foo

I think there are some compile-time vs runtime considerations that must be made.

Update part 2: As far as I understand it, when you bless a reference, the blessed reference will get a pointer to the stash designated by the string you pass as the second parameter. The blessed reference never actually gets the string passed as the second parameter. What ref actually returns is the name on the stash to which the reference points. Thus since Bar points to Foo's stash, you will always get Foo when you bless with Bar.


In reply to Re: Two argument bless sometimes ignores the class name? by !1
in thread Two argument bless sometimes ignores the class name? by Ovid

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.