Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^3: Perl semi-object without a constructor

by tobyink (Canon)
on Feb 21, 2013 at 17:37 UTC ( [id://1019990]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Perl semi-object without a constructor
in thread Perl semi-object without a constructor

"All of the texts I looked at started with a package and a sub called 'new' to create the object."

It is convention to name constructors new but there's no requirement to do so. You could just as well call it old or frangipane and it would make not one whit of difference to the Perl interpreter. But the reason that you should call a constructor new (and not old or frangipane) is for humans reading your code. When they see new they'll know precisely what that method call is doing!

That said, it's quite common for a class to define additional constructors such as new_from_json or new_from_yaml in addition to their standard new constructor. These are often wrappers around new that do additional pre-processing or post-processing.

package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1019990]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found