Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-19 20:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found