Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Reblessing (was: OO Perl: calling a constructor within a class)

by jeroenes (Priest)
on Sep 27, 2001 at 09:52 UTC ( #115008=note: print w/ replies, xml ) Need Help??


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)


Comment on Reblessing (was: OO Perl: calling a constructor within a class)
Download Code
Re: Reblessing (was: OO Perl: calling a constructor within a class)
by dragonchild (Archbishop) on Sep 27, 2001 at 17:39 UTC
    *shudders at the thoughts of his objects doing the can('can')*

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      Were you thinking something like this?
      Objects can always can, but a special can can can the usual can. But to see it's a special can you can:
      $check_that_can_with = UNIVERSAL::can($obj,'can') ne \&UNIVERSAL::can;
      But you can't catch the man who replaced the default can:
      my $can = \&UNIVERSAL::can; local $^W = 0; *UNIVERSAL::can = sub { $_[1] eq 'can' ? \&UNIVERSAL::can : &$can; };
      So can that man before he cans your can!
      (Sorry)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (11)
As of 2014-07-29 15:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (220 votes), past polls