perlquestion
ghenry
<p>Dear Monks,</p>
<p>Quick one - "Are there any limits on what you can/can't or should/shouldn't put into a constructor?"</p>
<p><small><b>(Updated with an example)</b></small><br/>For example:
<readmore>
[doc://perltoot] has:<br/>
<c>
sub new {
my $class = shift;
my $self = {};
$self->{NAME} = undef;
$self->{AGE} = undef;
$self->{PEERS} = [];
bless ($self, $class);
return $self;
}
</c></p>
<p>And [dist://WWW::Mechanize] has:<br/>
<c>
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;
}
</c></readmore></p>
<p>Thanks,<br/>Gavin.</p>
<div class="pmsig"><div class="pmsig-386673">
<small>Walking the road to enlightenment... I found a penguin and a camel on the way.....<br/>
Fancy a yourname@perl.me.uk? Just ask!!!</small>
</div></div>