Contributed by Anonymous Monk on Apr 20, 2000 at 20:26 UTC
Q&A  > object-oriented programming

Answer: 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.
Answer: How do I make a constructor?
contributed by chromatic

Simply create a subroutine that initializes something, blesses a reference to it into the appropriate class, and returns the blessed reference. There is no enforced naming scheme (as there is in C++ or Java, for example), but many Perl hackers use new:

sub new { my $class = shift; # allow for inheritance $class = ref($class) || $class; # allow indirect or # direct calling my $self = {}; # create an anonymous # array to hold member data bless($self, $class); # allow for inheritance return $self; # return new object }
That's the canonical constructor. It takes the name of the class into which to bless the object (provided automatically), discerns the proper name (whether it is called through an instance of that class or indirect means), creates an anonymous hash for member data, uses the dual-argument form of bless to be sure that the object is created as a type of the desired (not just the current) class, and provides it to the caller in a nicely wrapped package.

You will probably want to be in your own package before you declare this constructor, though.

Answer: How do I make a constructor?
contributed by jamesduncan

I tend to use:

sub new { my $class = shift; my $self = {}; if (bless( $self, $class)->init( @_ )) { return $self; } else { # throw some sort of error } } sub init { 1; }
That way all subclasses can initialize themselves in a standard way with something along the lines of:
sub init { my $self = shift; if ($self->SUPER::init( @_ )) { ## do some sort of initialization ## or return false return 1; } else { return 0; } }
and I'll know if something goes wrong...
Answer: How do I make a constructor?
contributed by jdhedden

I'm a pragmatist, and see nothing wrong with supporting $obj->new(). In fact, perlobj even mentions it. If your class takes args that are in addition to what the parent class uses in ->new(), then you need a bit more code.

# Create a new object sub new { my $thing = shift; my $class = ref($thing) || $thing; my $self = {}; bless($self, $class) if (! $self->_init($thing, @_)) { # Failed to initialize # Throw some sort of error, or return; # Returns 'undef' } return ($self); } # Initialize a new object sub _init { my $self = shift; my $thing = shift; # Separate '@_' into args for parent class and args for this sub +class my @parent_args = ...; my @my_args = ...; # Perform parent class initialization if (! $self->SUPER::_init($thing, @parent_args)) { # Parent class initialization failed return (0); } # Perform subclass initialization # Making use of '@my_args', if any if (ref($thing)) { # $thing->new( ... ) was called # Make use of '@my_args', if any # And make use of object's data, if applicable } else { # CLASS->new( ... ) was called # Make use of '@my_args', if any } return (1); }
Note that $thing is passed to _init() in addition to the args (cf. jamesduncan's code above). When the user calls $obj->new(), this allows the initialization method to make use of data contained in the calling object, if that is applicable to your code.
Answer: How do I make a constructor?
contributed by SoftTerrier

Don't call your contructors explicitly throughout the code. Use Class Factory instead.

Please (register and) log in if you wish to add an answer

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.