http://qs321.pair.com?node_id=497888

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