Ok, I think that the I no longer talk about blessing references
themselves directly. Here's from the current perlobj.pod;
it could probably be done better.
35-Unlike say C++, Perl doesn't provide any special syntax for
36-constructors. A constructor is merely a subroutine that returns a
37:reference to something "blessed" into a class, generally the
38-class that the subroutine is defined in. Here is a typical
39-constructor:
40-
41- package Critter;
42: sub new { bless {} }
43-
44-The C<{}> constructs a reference to an anonymous hash containing no
45:key/value pairs. The bless() takes that reference and tells the object
46-it references that it's now a Critter, and returns the reference.
47-This is for convenience, since the referenced object itself knows that
48:it has been blessed, and its reference to it could have been returned
49-directly, like this:
50-
51- sub new {
52- my $self = {};
53: bless $self;
54- return $self;
55- }
----------
60- sub new {
61- my $self = {}
62: bless $self;
63- $self->initialize();
64- return $self;
----------
66-
67-If you care about inheritance (and you should; see L<perlmod/"Modules:
68:Creation, Use and Abuse">), then you want to use the two-arg form of bless
69-so that your constructors may be inherited:
70-
----------
72- my $class = shift;
73- my $self = {};
74: bless $self, $class
75- $self->initialize();
76- return $self;
----------
79-Or if you expect people to call not just C<CLASS->new()> but also
80-C<$obj->new()>, then use something like this. The initialize()
81:method used will be of whatever $class we blessed the
82-object into:
83-
----------
86- my $class = ref($this) || $this;
87- my $self = {};
88: bless $self, $class
89- $self->initialize();
90- return $self;
----------
96-only be accessed through the class's methods.
97-
98:A constructor may re-bless a referenced object currently belonging to
99-another class, but then the new class is responsible for all cleanup
100:later. The previous blessing is forgotten, as an object may only
101-belong to one class at a time. (Although of course it's free to
102-inherit methods from many classes.)
103-
104:A clarification: Perl objects are blessed. References are not. Objects
105:know which package they belong to. References do not. The bless()
106-function simply uses the reference in order to find the object. Consider
107-the following example:
----------
109- $a = {};
110- $b = $a;
111: bless $a, BLAH;
112- print "\$b is a ", ref($b), "\n";
113-
114:This reports $b as being a BLAH, so obviously bless()
115-operated on the object and not on the reference.
116-
----------
268-
269-Perl doesn't do nested destruction for you. If your constructor
270:reblessed a reference from one of your base classes, your DESTROY may
271-need to call DESTROY for any base classes that need it. But this only
272:applies to reblessed objects--an object reference that is merely
273-I<CONTAINED> in the current object will be freed and destroyed
274-automatically when the current object is freed.
----------
344- $test = \$test;
345- warn "CREATING " . \$test;
346: return bless \$test;
347- }
348-
--tom
>>> From: Tim Bunce <Tim.Bunce@ig.co.uk>
>>> But perlobj says:
>>>
>>> 1. An object is simply a reference that happens to know
>>> which class it belongs to.
>>>
>>> That seems to contradict you on point points.
>>>
>>> I tend to agree with perlobj.
>>
>>As I just told Jack, perlobj is wrong :)
>If not wrong, it is at least misleading and inconsistent in the
>use of the word "object" in places. About a couple of months
>back this troubled me and I muttered:
>-------------
> ...In the beginning you read with the mounting certainty
>that an "object" is simply a blessed reference, but later you
>hear,
> "..bless() takes that reference and tells the object it references.."
>and still later,
> "..The bless() function simply uses the reference in order to find the
> object.."
>which really conveys that there is indeed a distinction between a
>blessed reference and the real object.
>-------------
>>
>>1. The reference is not the object.
>>2. Objects are blessed, references are not.
>>
>>Dean
>>
>I would be happy to see a revised perlobj that covered the distinction
>without making you read between the lines.
> - Sarathy.
> gsar@engin.umich.edu
themselves directly. Here's from the current perlobj.pod;
it could probably be done better.
35-Unlike say C++, Perl doesn't provide any special syntax for
36-constructors. A constructor is merely a subroutine that returns a
37:reference to something "blessed" into a class, generally the
38-class that the subroutine is defined in. Here is a typical
39-constructor:
40-
41- package Critter;
42: sub new { bless {} }
43-
44-The C<{}> constructs a reference to an anonymous hash containing no
45:key/value pairs. The bless() takes that reference and tells the object
46-it references that it's now a Critter, and returns the reference.
47-This is for convenience, since the referenced object itself knows that
48:it has been blessed, and its reference to it could have been returned
49-directly, like this:
50-
51- sub new {
52- my $self = {};
53: bless $self;
54- return $self;
55- }
----------
60- sub new {
61- my $self = {}
62: bless $self;
63- $self->initialize();
64- return $self;
----------
66-
67-If you care about inheritance (and you should; see L<perlmod/"Modules:
68:Creation, Use and Abuse">), then you want to use the two-arg form of bless
69-so that your constructors may be inherited:
70-
----------
72- my $class = shift;
73- my $self = {};
74: bless $self, $class
75- $self->initialize();
76- return $self;
----------
79-Or if you expect people to call not just C<CLASS->new()> but also
80-C<$obj->new()>, then use something like this. The initialize()
81:method used will be of whatever $class we blessed the
82-object into:
83-
----------
86- my $class = ref($this) || $this;
87- my $self = {};
88: bless $self, $class
89- $self->initialize();
90- return $self;
----------
96-only be accessed through the class's methods.
97-
98:A constructor may re-bless a referenced object currently belonging to
99-another class, but then the new class is responsible for all cleanup
100:later. The previous blessing is forgotten, as an object may only
101-belong to one class at a time. (Although of course it's free to
102-inherit methods from many classes.)
103-
104:A clarification: Perl objects are blessed. References are not. Objects
105:know which package they belong to. References do not. The bless()
106-function simply uses the reference in order to find the object. Consider
107-the following example:
----------
109- $a = {};
110- $b = $a;
111: bless $a, BLAH;
112- print "\$b is a ", ref($b), "\n";
113-
114:This reports $b as being a BLAH, so obviously bless()
115-operated on the object and not on the reference.
116-
----------
268-
269-Perl doesn't do nested destruction for you. If your constructor
270:reblessed a reference from one of your base classes, your DESTROY may
271-need to call DESTROY for any base classes that need it. But this only
272:applies to reblessed objects--an object reference that is merely
273-I<CONTAINED> in the current object will be freed and destroyed
274-automatically when the current object is freed.
----------
344- $test = \$test;
345- warn "CREATING " . \$test;
346: return bless \$test;
347- }
348-
--tom
>>> From: Tim Bunce <Tim.Bunce@ig.co.uk>
>>> But perlobj says:
>>>
>>> 1. An object is simply a reference that happens to know
>>> which class it belongs to.
>>>
>>> That seems to contradict you on point points.
>>>
>>> I tend to agree with perlobj.
>>
>>As I just told Jack, perlobj is wrong :)
>If not wrong, it is at least misleading and inconsistent in the
>use of the word "object" in places. About a couple of months
>back this troubled me and I muttered:
>-------------
> ...In the beginning you read with the mounting certainty
>that an "object" is simply a blessed reference, but later you
>hear,
> "..bless() takes that reference and tells the object it references.."
>and still later,
> "..The bless() function simply uses the reference in order to find the
> object.."
>which really conveys that there is indeed a distinction between a
>blessed reference and the real object.
>-------------
>>
>>1. The reference is not the object.
>>2. Objects are blessed, references are not.
>>
>>Dean
>>
>I would be happy to see a revised perlobj that covered the distinction
>without making you read between the lines.
> - Sarathy.
> gsar@engin.umich.edu