Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Creating Common Constructor

by smalhotra (Scribe)
on Jul 10, 2003 at 16:25 UTC ( [id://273032]=note: print w/replies, xml ) Need Help??


in reply to Creating Common Constructor

Try subclassing with something like this:

package 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();
-hth
Sidharth.

$will->code for @food or $$;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-24 03:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found