http://www.perlmonks.org?node_id=115008


in reply to Re: Re: OO Perl: calling a constructor within a class
in thread OO Perl: calling a constructor within a class

As merlyn showed me before, you could alternatively use 'can' to check whether $me is an object or not, and leave it alone when it is, trusting that the class of the object is fine:
if ( ref $me and $me->can('can') ){ .....
I did have some discussion with tye about can, and at the time he convinced me to use isa instead: ... $me->isa('UNIVERSAL').

More importantly, reading perlbot I realised that all that caution about bless is unnecessary. So I rephrase my advise above:

There is nothing special about constructors other than that they bless a variable. Since you are allowed to rebless a variable, just call the constructors at will.
In perlbot you can find more info and an example of nested constructors.

Reblessing assumes that the caller knows what (s)he is doing and that the constructor should always return an object of its own class. This antagonizes the logic behind bless-checking. The reblessing approach is the most flexible one, as constructors do not have to make assumptions about which class the returned objects should be in.

This in turn made me realise that multiple inheritance could, with a little caution, even apply to the object's data. Just call both constructors and merge the resulting anonymous arrays. Problems will arise when the inherited classes use the same hash-entries for different purposes or when different types of variables are used, of course. At the end, one just reblesses the anonymous hash.

Jeroen
"We are not alone"(FZ)