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

Re: OO Perl: calling a constructor within a class

by davorg (Chancellor)
on Sep 26, 2001 at 14:10 UTC ( [id://114773]=note: print w/replies, xml ) Need Help??


in reply to OO Perl: calling a constructor within a class

Many of my objects have a structure that looks like this:

package MyObject; sub new { my $thing = shift; my $class = ref $thing || $thing; my $self = bless {}, $class; return $self->init(@_); } sub init { my $self = shift; # do initialisation stuff with the values in @_ return $self; }

Does that help you at all?

Update: Actually I tend to agree with both tilly and merlyn on the class/instance method divide. The $class = ref $thing || $thing idiom just seems to be hard-wired into my conciousness thru reading in so many books. I have to think twice to not use it. Generally my new method will be a class method and I'll only create an instance method version in the rare cases when I need a copy constructor.

--
<http://www.dave.org.uk>

"The first rule of Perl club is you don't talk about Perl club."

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (11)
As of 2024-03-28 09:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found