Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: How do I make a constructor?

by jamesduncan (Novice)
on Apr 08, 2003 at 11:34 UTC ( [id://248886]=note: print w/replies, xml ) Need Help??


in reply to How do I make a constructor?

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...

Replies are listed 'Best First'.
Re: Answer: How do I make a constructor? (different responsibilities)
by Aristotle (Chancellor) on Apr 13, 2003 at 12:51 UTC
    I'd be inclined to write that as follows:
    sub new { return (bless {}, shift)->init(@_); } sub init { my $self = shift; $self->SUPER::init(@_); 1 # put stuff to initialize here or die "Failed to init $self: blah blah\n"; return $self; }

    Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-25 22:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found