Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: inheritance: constructors

by frankus (Priest)
on Jun 25, 2002 at 11:43 UTC ( #177065=note: print w/replies, xml ) Need Help??

in reply to inheritance: constructors

IMO it's more "future facing" to use a loop on @ISA and the can method.
It enables the Object to handle multiple inheritance.
$self->$_($params) for ( map{ $_->can("_init")||()} @ISA );
This'll execute a "private" method to initialise the correct variables for the object.
As a rule, I pass all Parameters for the function inside a hashref called $params, it makes for easier coding and ledgibility.
I generally have a class property called @manifest, listing the object properties and use a line like:
$self->{$_ns . $_} = $params->{$_} || '' for @manifest;

Where $_ns is the namespace as all the gubbins is stored in one hash, it's best not to overwrite stuff.
I use a class property like this:
my $_ns = __PACKAGE__; 
For I find there are times when __PACKAGE__ doesn't want to play.
I also apply accessor and mutator functions for all the properties:
sub get_spong() { $_[0]->{ $_ns . 'spong' } }
And because all of this is labour-intensive and boring,
I set up a template and also some generic parent classes to ingherit most of the common stuff from.

As a rule of thumb Perl OO is tricky, because it's Perlish OO.
You can rewrite OO from first principals.
Perl OO can be clunky: it mirrors the developers understanding of OO principals since Perl is so liberal.


Brother Frankus.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://177065]
[Corion]: I guess I should not read some discussions on HackerNews - the crowd there is weirdly different - "you have to stay current in tech" (by following all the latest JS trends) is a meme there, but it doesn't really match my experience
[Corion]: At least in my area, experience can beat most new tech anyway because most new tech is just a rehash of things abandoned in the 70s and we still have a lot of that ;)
[Corion]: choroba: Ah, that will be next Friday for $work ;) Drinking enough water is key ;))
[Corion]: But then, maybe that's just my experience with things, and maybe Hacker News is just people much younger who haven't seen more than one tech cycle...
[choroba]: s/water/bear/ and s/enough/too much/
[choroba]: beer
[choroba]: oh ok
[marto]: I crashed the ScotLUG Christmas night, having never actually been to ScotLUG
[choroba]: Corion yeah, I probably already told you about how Bjarne Stroustrup was asked whether he still watched the new tech trends and what really impressed him
[choroba]: His reply was "I watch them, but I haven't been impressed in the last 10 years. There's been nothing new". That was 2 years ago :)

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2016-12-09 10:19 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (150 votes). Check out past polls.