in reply to Creating Common Constructor
-hthpackage Zoo::Animal; ## it's a hashref! my $count = {}; sub new { ## get the first argument, could be a blessed object or a package na +me my $type = shift; ## get the package name my $class = ref $type || $type; my $self = bless {} $class; ## get the name of the animal my ($animal) = $class =~ /^Zoo::Animal::(.+)$/; $count->{$animal}++; return $self->_init(@_); ## call the animal's init method and retur +n what it returns } ## does nothing in package Zoo::Animal sub _init {} #### package Zoo::Animal::Camel ## no sub new sub _init { $self = shift; ### blah blah blah $self->{title} = "Programming Perl"; return $self; } ... # will call Zoo::Animal::new() then Zoo::Animal::Camel::_init() my $camel = Zoo::Animal::Camel->new();
$will->code for @food or $$;
|
|---|