SPUG: Unidentified Flying Objects
DeRykus, Charles E
charles.e.derykus at boeing.com
Tue Jan 10 13:47:49 PST 2006
Beware re-blessers that forgot to clean their dirty laundry.though..
From perlobj:
Although a constructor can in theory re-bless a referenced
object currently belonging to another class, this is almost
certainly going to get you into trouble. The new class is
responsible for all cleanup later. The previous blessing is
forgotten, as an object may belong to only one class at a
time. (Although of course it's free to inherit methods from
many classes.) If you find yourself having to do this, the
parent class is probably misbehaving, though.
The plain vanilla re-bless below for instance doesn't call Foo's DESTROY method -- only
Bar's.
package Foo;
sub new { my $self= shift; bless {}, $self }
sub zap { my($self,$newself)=@_; bless $self, $newself }
DESTROY { print "cleaning up Foo..."; }
package Bar;
sub new { my $self= shift; bless {}, $self };
DESTROY { print "cleaning up Bar..."; }
package main;
my $foo = Foo->new("Bar");
$foo->zap("Bar");
hth,
--
Charles DeRykus
-----Original Message-----.
From: JD Brennan [mailto:jazzdev at gmail.com]
Sent: Tuesday, January 10, 2006 12:15 PM
To: Fred Morris
Cc: spug-list at pm.org
Subject: Re: SPUG: Unidentified Flying Objects
On 1/10/06, Fred Morris <m3047 at inwa.net> wrote:
sub zap {
my $self = shift;
my $new_me = shift;
print "ZAP!\n";
return bless $self, $new_me;
Yeah, that's odd. Calling bless more than once. That
is kinda cool. I don't think you can do that in Ruby.
Perl is strange that way. An object is just a
reference to an object (typically a hash) marked
as a certain type. I never realized that you could
change the type on the fly, but of course it makes
sense given how Perl does objects.
I can't think of any other language where you can change
the run-time type of an object after you've created it.
You can do something similar in JavaScript and Smalltalk.
You can change add or change a method on an object.
Something similar to your example (in JavaScript):
var ufo = new Bird();
ufo.i_am_a();
ufo.i_am_a = Plane.i_am_a;
ufo.i_am_a();
But in Perl it's cooler because you get all the
methods of the new type at once. In JavaScript
or Smalltalk you'd have to set them all one at
a time, I think.
Arc (a dialect of Lisp) has objects that are tagged
objects, but I don't know if Arc really has the
notion of a run-time type. Arc isn't actually
finished yet, so it's anyone's guess. Arc is more
like JavaScript in that methods are part of the hash,
whereas in Perl methods are part of a package that
has the name of the run-time type.
Now as to why you'd want to do this, I don't know.
Might be useful for adding methods to an instance
on the fly. I've done that in JavaScript, but it might
be awkward in Perl to keep the existing methods and
add a new one.
If you just changed the object's type on the fly it might
be initialized properly - that is the contents of it's
hash might not have all the right keys set.
JD
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/spug-list/attachments/20060110/13e64476/attachment.html
More information about the spug-list
mailing list