Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: Creating Common Constructor

by Flame (Deacon)
on Jul 10, 2003 at 16:27 UTC ( #273033=note: print w/replies, xml ) Need Help??

in reply to Creating Common Constructor

Inheritance is another way to go.

package Foo; our $count = 0; sub new { my $class = (ref($_[0]) ? ref(shift) : shift); $count++; return bless({ @_ }, $class); } package Bar; our @ISA = ('Foo'); sub new { my $self = SUPER::new(@_); #Anything unique to this class can now be applied to the object... return $self; }

I often use this approach when there are several attributes guaranteed to be present in all of the instances, such as the name of the creature or whatever. Also, thanks to inheritance, you can place any methods that all of these classes should support into the base class.

I hope this helps, I've only been awake for... about 3 minutes now, so my interpretation of your question may be a little... off...

Edit/Note: SUPER::new is a common trick used in many object oriented languages when the parent contstructor has code to take care of a lot of the setup work.

My code doesn't have bugs, it just develops random features.

Flame ~ Lead Programmer: GMS (DOWN) | GMS (DOWN)

Replies are listed 'Best First'.
Re: Re: Creating Common Constructor
by exussum0 (Vicar) on Jul 10, 2003 at 20:24 UTC
    Just realize, that he wants to garantee keeping track of them. So if someone writes an animal object without a super, he's screwed.

    But for the attribute thing, i'm sure there's something he can work out.. perhaps keeping the various attributes to create in a config file, have the parent read in said config file, and evertytime you want an object created, the zoo object would dynamically know what to make. Hrm....

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://273033]
[choroba]: You can also try DBD::MariaDB instead of MySQL
[choroba]: it's not on CPAN yet, unfortunately, so you have to install manually from github
[Spenser]: Thanks. I tried DBD::MariaDB just now. It can't find that module.
[Spenser]: oh, I'll check github

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2018-03-17 13:00 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (224 votes). Check out past polls.