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

Answer: How do I make a constructor?

( #9106=categorized answer: print w/ replies, xml ) Need Help??

Q&A > object-oriented programming > How do I make a constructor? 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.

Comment on Answer: How do I make a constructor?
Log In?

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2015-11-30 08:58 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (766 votes), past polls