ghenry has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks,
Quick one - "Are there any limits on what you can/can't or should/shouldn't put into a constructor?"
(Updated with an example)
For example:
perltoot has:
sub new { my $class = shift; my $self = {}; $self->{NAME} = undef; $self->{AGE} = undef; $self->{PEERS} = []; bless ($self, $class); return $self; }
And WWW::Mechanize has:
sub new { my $class = shift; my %parent_parms = ( agent => "WWW-Mechanize/$VERSION", cookie_jar => {}, ); my %mech_parms = ( autocheck => 0, onwarn => \&WWW::Mechanize::_warn, onerror => \&WWW::Mechanize::_die, quiet => 0, stack_depth => 0, headers => {}, ); my %passed_parms = @_; # Keep the mech-specific parms before creating the object. while ( my($key,$value) = each %passed_parms ) { if ( exists $mech_parms{$key} ) { $mech_parms{$key} = $value; } else { $parent_parms{$key} = $value; } } my $self = $class->SUPER::new( %parent_parms ); bless $self, $class; # Use the mech parms now that we have a mech object. for my $parm ( keys %mech_parms ) { $self->{$parm} = $mech_parms{$parm}; } $self->{page_stack} = []; $self->env_proxy(); # libwww-perl 5.800 (and before, I assume) has a problem where # $ua->{proxy} can be undef and clone() doesn't handle it. $self->{proxy} = {} unless defined $self->{proxy}; push( @{$self->requests_redirectable}, 'POST' ); $self->_reset_page; return $self; }
Thanks,
Gavin.
Walking the road to enlightenment... I found a penguin and a camel on the way.....
Fancy a yourname@perl.me.uk? Just ask!!!
Fancy a yourname@perl.me.uk? Just ask!!!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Object Constructors - Advice of experienced OO Perl Monks
by izut (Chaplain) on Oct 06, 2005 at 11:52 UTC | |
Re: Object Constructors - Advice of experienced OO Perl Monks
by Perl Mouse (Chaplain) on Oct 06, 2005 at 12:20 UTC | |
by izut (Chaplain) on Oct 06, 2005 at 13:05 UTC | |
by ides (Deacon) on Oct 06, 2005 at 15:01 UTC | |
by Perl Mouse (Chaplain) on Oct 06, 2005 at 15:51 UTC | |
by izut (Chaplain) on Oct 06, 2005 at 16:24 UTC | |
| |
Re: Object Constructors - Advice of experienced OO Perl Monks
by phaylon (Curate) on Oct 06, 2005 at 12:29 UTC | |
Re: Object Constructors - Advice of experienced OO Perl Monks
by jeffa (Bishop) on Oct 06, 2005 at 17:53 UTC |
Back to
Seekers of Perl Wisdom