in reply to Re: why "Bless" in OO Perl
in thread why "Bless" in OO Perl

Actuall the thing being referenced is blessed; the reference is still just a reference. The blessing changes the referent, not the reference itself. A new reference taken to the same referent will exhibit blessed / object-y behavior.</pedant>

use strict; {package Foo; sub oik { print "oik\n" } } my $x = bless [], "Foo"; $x->oik; our @y; *y = $x; ( \@y )->oik;

Zaxo's explanation below shows this as well.

Replies are listed 'Best First'.
Re^3: why "Bless" in OO Perl
by davido (Cardinal) on May 03, 2006 at 15:37 UTC

    You are absolutely correct. ...it's a concept I understand but have difficulty expressing clearly, especially in a layman vocabulary. Good job with the linguistic deobfuscation. ;)

    I might add, the referent needn't be a hash, a simple scalar, nor an array. It could also be a file handle, a regexp, and even a sub, though I've never figured out a useful purpose to blessing a sub. Now I'm sure someone will follow-up with a brilliant use of this technique. *grin*


    Dave

      OO: extending a closure object was an example of using a closure as an object base type. The blessed sub is the only possible accessor. It allowed the object to be very opaque, but I didn't see it as enough of a benefit to recommend it.

      Caution: Contents may have been coded under pressure.

      Not brlliant but see Re: How to store CODE ref for a method of accessing the source for a coderef without resorting to B:: trickery (of course it's not perfect since it doesn't preserve closureness, but then that's a hard problem no matter what . . .).

      Update: Never mind, that's not blessing the coderef directly it's overloading &{}. I thought I'd done something similar with blessed coderefs. Hrmm . . .