I dimly recall somewhere that blessing is a property of the reference, not the object itself.

I did a little testing, and got contradictary results.

package C1; sub foo { my $self= shift; print "C1::foo called @_ :", join (',', keys %$self), "\n"; } package C2; sub foo { my $self= shift; print "C2::foo called @_ :", join (',', keys %$self), "\n"; } package main; my $x= bless \%hash, "C1"; my $y= bless \%hash, "C2"; $x->foo ("before"); $y->foo ("before"); %hash= ( key=>'value'); $x->foo ("after"); $y->foo ("after");
From this I discover that blessing a hash before its assigned to "sticks", and assigning to it doesn't lose its blessing. I'm supposing %hash was actually defined upon first use, where the reference was taken.

So if the blessing is a property of the reference, not the object, then I should be able to have two different references with different blessings. But I cannot. One affects the other.

If the blessing is a property of the object, then why doesn't assigning to the object as a whole lose it?

I think what's going on is that blessing is a property of the object's identity, not its value. Assignment changes the value, but not the container that holds that value. So where it said the blessing is a property of the reference, it really meant THE reference of that object, which is itself the (same) value in two reference variables (if you think if reference vars as pointers).

Am I on the right track?


In reply to Where is blessing magic located? by John M. Dlugosz

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.