John M. Dlugosz has asked for the wisdom of the Perl Monks concerning the following question:

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?

Replies are listed 'Best First'.
Re: Where is blessing magic located?
by perrin (Chancellor) on Nov 06, 2002 at 04:44 UTC
    It's all explained here.

      Thank &deity; for the lack of sesquipedalian jargon.


      Nah! You're thinking of Simon Templar, originally played (on UKTV) by Roger Moore and later by Ian Ogilvy
Re: Where is blessing magic located?
by bart (Canon) on Nov 06, 2002 at 08:54 UTC
    You remember wrong. It's all explained in perlobj. I quote:
    A clarification: Perl objects are blessed. References are not. Objects know which package they belong to. References do not. The bless() function uses the reference to find the object. Consider the following example:
    $a = {}; $b = $a; bless $a, BLAH; print "\$b is a ", ref($b), "\n";
    This reports $b as being a BLAH, so obviously bless() operated on the object and not on the reference.
Re: Where is blessing magic located?
by chromatic (Archbishop) on Nov 06, 2002 at 06:58 UTC

    You don't bless a hash, you bless a reference. Always. (For the purpose of argument, let's not consider tie here.) The hash is not the object. The (blessed) reference is.

    my $scalar = 1; my $scalar_reference = \$scalar; my $object = bless $scalar_reference, 'class_name'; $scalar = 2; print "Yep, still an object!\n" if UNIVERSAL::isa( $object, 'class_name' );
      That's only half of my test. My code shows that assigning to the whole object doesn't lose the blessing, but also that two references to the same object can't be differently-blessed.

        You said it yourself -- it's the same object. Try this:

        my %hash; my $x = \%hash; my $y = \%hash; print "($x) [$y]\n";

        The blessing isn't associated with the value; it's attached to the container. bless adds or changes that through a reference. Since you have two references to the same thing, and since you can only have one blessing per container, you're seeing the expected and designed behavior.

        If you want two different objects, you need two different containers.

        (I'm using the word container to differentiate between a variable and its value.)

Re: Where is blessing magic located?
by Elian (Parson) on Nov 07, 2002 at 05:16 UTC
    In perl 5, values and variables are rather mushed together. The short answer is that that the object reference's referent, be it an array, hash, or scalar, is blessed into a package. This SV-like thing (as it may be an AV, HV, CV, or GV (the latter two if you're getting really weird)) has a little note tacked onto it saying that it's now an object, and marking the package the object belongs to.

    When you assign to the entire array/hash/scalar that is your object, perl doesn't generate a new array/hash/scalar--it cleans it out and reuses it, including any blessing on it. (Yes, in some circumstances magic is removed. This isn't one of them)

    You can check this with this little snippet:

    $foo = \%bar; print $foo, "\n"; %bar = (); $foo = \%bar; print $foo, "\n";
    which produces the output
    HASH(0x80d0b84)
    HASH(0x80d0b84)
    showing that %bar hasn't moved. (And is, therefore, the same variable)