in reply to normal objects?

This Perl not compatible with vegetarians:
package Cow; sub new { my ($this) = shift; my ($class) = ref($this) || $this; my ($meat) = {}; bless ($meat, $class); return $meat; } sub grind { my ($meat) = shift; bless ($meat, "Beef"); return $meat; } package main; my ($bessie) = new Cow(); grind $bessie;
Certainly you could define a base class for both Cow and Beef which would have methods that both would use (via the @ISA method in the package) so that regardless of the condition of the "$bessie", it is still a member of the base class, even after "grind()".

Where other languages can only "cast" types up the class chain, Perl can make lateral moves, or even a move to left field, as you can re-bless() something at will. Presto!

In your application, I suspect the contents of the object might have to be modified by the conversion method (i.e. grind()) before being re-bless()ed so that the data is compatible with the new methods that it will use.

Replies are listed 'Best First'.
Re: Re: normal objects?
by mkmcconn (Chaplain) on Jan 25, 2001 at 01:14 UTC
    tadman, the part you describe is easier than I thought it would be. But (pardon me for thinking out loud - I promise to read the docs) what if $bessie has the $bessie->eat method, prior to re-blessing and a new $bessie->eat method after the conversion by grind()? Am I confusing myself, or, won't there be a conflict?

    mkmcconn

      Yes there will be a conflict. You will need this as well:
      package Beef; use vars qw(@ISA); #if you use strict @ISA = qw(Cow);
      Which is why it's wise to plan out your inheritance tree before you start coding. OOP is only as good as its design.

      To be technical, Beef IS NOT a Cow - a Cow HAS Beef. Take a look into aggregation for alternatives to inheritance.

      Jeff

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      F--F--F--F--F--F--F--F--
      (the triplet paradiddle)