in reply to When CGI->new doesn't return a CGI object

At the risk of being niggly...

References can be blessed. And because the slots in a typeglob are references, they can be blessed too. Just like you would bless anything else. And because if you reference something that is dereferenced, you get the same reference you dereferenced, the reference would still blessed, if it was originally.

It's the referent that is blessed into a package, not the reference. When you do:

my @foo = (1,2,3); bless \@foo, 'Bar'; my $x = \@foo; my $y = \@foo;

Then $x and $y are different references that refer to the same array, which has been blessed into the Bar package.

Chapter 3 of TheDamian's Object Oriented Perl covers this topic well for those who are as pedantic as I am :-)

Replies are listed 'Best First'.
Re: Re: When CGI->new doesn't return a CGI object
by demerphq (Chancellor) on Jan 07, 2003 at 21:33 UTC
    While we are being niggly and pedantic i'd like to correct you where you said

    Then $x and $y are different references that refer to the same array, which has been blessed into the Bar package.

    This is a common misunderstanding (even by the serious pros. Data::Dumper has a flaw that IMO can be traced back to this subtle mistake) In reality you should have said

    Then $x and $y are different scalar variables holding a reference to the same array, which has been blessed into the Bar package.

    This is a crucial distinction. Consider how many distinct variables are present in the two following snippets:

    my ($z,$x,$y); $x=\$y; $y=\$x; $z=[$x,$y];
    and
    my $z=[]; $z->[0]=\$z->[1]; $z->[1]=\$z->[0];
    Note that dumping $z in both cases will result in the same output when using Data::Dumper.

    Pedantic can be fun and useful sometimes... :-)

    Oh yes, the correct answer to my question can be found in the source of the node.

    --- demerphq
    my friends call me, usually because I'm late....