instances that know #new, and Perl OO

Randal L. Schwartz merlyn at stonehenge.com
Mon May 19 08:55:40 UTC 2003


There's been an ongoing quiet debate in the Perl community about the
sanity of a Senior Perl Programmer's advice to allow a "new" method to
do double duty, through a commonly used idiom of code that appeared in
one of the earliest Perl OO tutorials.  Applied to a class, it creates
an instance.  But applied to an *instance*, it creates another
instance, which may or may not refer to any part of the original.

I've been vocal about my objection to this, since I believe in "say
what you mean".  If you want a copy, say ->copy.  If you want a clone,
say ->clone.  But reserve ->new for "$instance = Class->new".

I finally noticed a challenge to my frequent claim was phrased
from another old-timer, who said "well, even smalltalk did it that way".
Ugh.  So I whipped out Squeak, and did the following:

  Metaclass allInstances size
  -> 1280

1280 classes in the squeak 3.6 image.  And then this:

  Metaclass allInstances select: [:m | m soleInstance includesSelector: #new]
  -> #(Behavior class Metaclass class)

Which leads me to believe that the only two classes whose instances
define a #new for their subclasses are Behavior (so all metaclasses
instances know how to new) and Metaclass (so all metaclasses
themselves don't muck with their soleinstances).

Thus, my hypothesis seems to be supported.  The Smalltalk community
doesn't apply #new to non-class instances.

Did I mistate the query, or does the data support my conclusion?

If you're a Perl hacker, see my post at
<http://www.perlmonks.org/index.pl?node=259093>.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn at stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!



More information about the Squeak-dev mailing list