Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: OO Perl: calling a constructor within a class

by davorg (Chancellor)
on Sep 26, 2001 at 14:10 UTC ( #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.


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

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2021-05-16 09:02 GMT
Find Nodes?
    Voting Booth?
    Perl 7 will be out ...

    Results (151 votes). Check out past polls.