Mailing List Archive

Re: UNIVERSAL
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
Re: UNIVERSAL [ In reply to ]
>From: Tom Christiansen <tchrist@mox.perl.com>

>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.


>A constructor is merely a subroutine that returns a
>37:reference to something "blessed" into a class

Yes! I'm in heaven...


>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.

You've cleared up the documentation. The clarification and demonstration
aren't needed anymore.



>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.

I like it.


Dean
Re: UNIVERSAL [ In reply to ]
Re: UNIVERSAL [ In reply to ]
On Sat, 16 Dec 1995, Felix Gallo wrote:

> > You've cleared up the documentation. The clarification and demonstration
> > aren't needed anymore.
>
> To voice a dissenting opinion, the more that the documentation demonstrates
> or attempts to explain about objects, references and blessing, the happier
> those of us of ignoble, stultified and low-amperage descent will be. For
> some of us, B<my> is still an arcane mystery only fully grasped by
> spooky bearded druids.
>
> Felix
> will hack perl for woad

I've noticed that programmers, myself included, tend to see redundancy as
inherently bad, even when writing English. Granted, saying the same thing
several different ways, in several different places, is hard to maintain and
prone to errors. But for the reader, it can be a big help. Important or
difficult ideas should be reinforced by repetition and rephrasing. Remember
how often the perl docs mention "-w"?

--
-- Phil Rand <prand@spu.edu> aka <postmaster@spu.edu>
-- Computer & Information Systems (206) 281-2428
-- Seattle Pacific University, 3307 3rd Ave W, Seattle, WA 98119
-- http://paul.spu.edu/~prand/
--
Re: UNIVERSAL [ In reply to ]
: Important or difficult ideas should be reinforced by repetition and
: rephrasing. Remember how often the perl docs mention "-w"?

You can say that again. :-)

A good playwright will mention any important fact at least 3 times.
Otherwise most people will miss it. It won't be stated the same way
each time, of course.

"I'm going out. Where'd that grocery list get to?"
...
"She went to the store. I hope she remembers the wine."
...
"Hi, I'm back. I got your favorite kind of Spam."

Larry
Re: UNIVERSAL [ In reply to ]
>A good playwright will mention any important fact at least 3 times.

or a teacher.

>Otherwise most people will miss it. It won't be stated the same way
>each time, of course.

in the next doc interation (which i hope to wrap up today for andy)
i've redundantly put the same thing in more than one place sometimes.
doesn't seem too terrible.

did you know one of the things we get dinged on is that perl has TOO
MUCH documentation? when people try to decide whether to approve tcl
or perl for a project, they measure the doc thickness and pick perl.
awk is another contender. nuts.


--tom
Re: UNIVERSAL [ In reply to ]
Phil Rand <prand@paul.spu.edu>, in a magnificent manifestation of deity, wrote:
>I've noticed that programmers, myself included, tend to see redundancy as
>inherently bad, even when writing English. Granted, saying the same thing

This has often been my problem.

>several different ways, in several different places, is hard to maintain and
>prone to errors. But for the reader, it can be a big help. Important or
>difficult ideas should be reinforced by repetition and rephrasing. Remember
>how often the perl docs mention "-w"?

I think that this should be reiterated. (smirk) My teaching teacher
told me that when you are teaching, you should tell 'em, tell 'em again
and then tell 'em what you told them. In documentation and
tutorial/instructional/howto documents, repetition is good as long as
you paraphrase.

Darren
<torin@daft.com> <http://www.daft.com/~torin/> <tnd@iswp.org> <torin@io.com>
Darren Stalder/2608 Second Ave, @282/Seattle, WA 98121-1212/USA/+1-800-921-4996
@ Do you have your clothes on? I probably don't. Take yours off. Feel better. @
@ Sysadmin, webweaver, postmaster for hire. C and Perl programmer and tutor. @