Ovid's Recap: The Future of Perl 5 got me curious, so I did some searching and found something interesting.

Object creation in Perl compared to Smalltalk (the putative parent language of OO languages): They are surprisingly similar.

Using Ovid's Point example, in Smalltalk:

Object subclass: 'Point' instanceVariables: 'x y ' x ^x x: coord x := coord y ^y y: coord y := coord init x := Float new. y := Float new.

Then to create an instance:

p := Point new. p init. p x: 1.5. p y: 1.5.

Doesn't that look a lot like:

my $p = bless {}, Point; $p->init; $p->x(1.5); $p->y(1.5);

For a friendlier object creation, you can define a class method to do all that:

newX: xc Y: yc p := Point new. p init. p x: xc. p y: yc. ^p

Then:

Point newX: 1.5 Y: 1.5.

Also, Smalltalk's new is a method inherited from the Object class and can be overridden by defining a new method as a class method of any user defined class. I'm leaving that as exercise for the readers.

So, maybe Perl 5's much maligned OO system can be blamed on Smalltalk. Not sure what else Larry et al had available to look at, but looks like Smalltalk had a strong influence.

Still, there is hope for Perl 5's OO system.

For those who've forgotten or are not familiar, Perl 5 has an equivalent to Object called UNIVERSAL. All classes in Perl 5 inherit from UNIVERSAL.

In theory, Perl 5 could provide a default new method for all classes by defining a new method in UNIVERSAL.pm

Maybe Ovid can present a compelling enough argument to do that. That would be a great first step.

But, since I'm writing this comment, I'll suggest 2 additions to UNIVERSAL.pm

sub init { # Nothing to do, this is just here so classes don't have to define + init } sub new { my $class = shift; my $object = bless {}, $class; $object->init(@_); return $object; }

I'm sure someone will come up with a better default new. This one is just an example of what could be done.

Disclaimer: None of this code is tested. YMMV


In reply to OO systems and Perl 5 (Was: Recap: Future of Perl 5) by RonW

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.