Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Answer: How do I make a constructor?

by merlyn (Sage)
on Apr 26, 2000 at 00:51 UTC ( #9106=categorized answer: print w/replies, xml ) Need Help??

Q&A > object-oriented programming > How do I make a constructor? - Answer contributed by merlyn

The use of
  $class = ref($class) || $class;
is unneeded 90% of the time, and insufficient for 90% of the rest of the time.

If you want a clone method, use a clone or copy method on an instance. If you want another object of the same class as the first object, use

  my $two = ref($one)->new(@parms);
Which reduces our canonical simple constructor to:
  sub new {
    my $class = shift;
    my $self = { @_ };
    bless $self, $class;
Nice and simple.

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
[Lady_Aleena]: Module is currently 104 lines but will shirnk to 63. The script using the module is currently 40 lines but will grow to 82, 180 to 146 lines total. (This is after rewrting the data files.)

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2017-05-29 03:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?