SPUG: Unidentified Flying Objects

JD Brennan jazzdev at gmail.com
Tue Jan 10 12:15:12 PST 2006

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 = Plane.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.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/spug-list/attachments/20060110/db69d31e/attachment.html

More information about the spug-list mailing list