On 1/10/06, <b class="gmail_sendername">Fred Morris</b> <<a href="mailto:m3047@inwa.net">m3047@inwa.net</a>> wrote:<div><span class="gmail_quote"></span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>sub zap {<br><br> my $self = shift;<br> my $new_me = shift;<br><br> print "ZAP!\n";<br><br> return bless $self, $new_me;</blockquote><div><br>
Yeah, that's odd. Calling bless more than once. That<br>
is kinda cool. I don't think you can do that in Ruby.<br>
Perl is strange that way. An object is just a<br>
reference to an object (typically a hash) marked<br>
as a certain type. I never realized that you could<br>
change the type on the fly, but of course it makes<br>
sense given how Perl does objects.<br>
<br>
I can't think of any other language where you can change<br>
the run-time type of an object after you've created it.<br>
</div><br>
You can do something similar in JavaScript and Smalltalk.<br>
You can change add or change a method on an object.<br>
Something similar to your example (in JavaScript):<br>
<br>
var ufo = new Bird();<br>
ufo.i_am_a();<br>
ufo.i_am_a = Plane.i_am_a;<br>
ufo.i_am_a();<br>
<br>
But in Perl it's cooler because you get all the<br>
methods of the new type at once. In JavaScript<br>
or Smalltalk you'd have to set them all one at<br>
a time, I think.<br>
<br>
Arc (a dialect of Lisp) has objects that are tagged<br>
objects, but I don't know if Arc really has the<br>
notion of a run-time type. Arc isn't actually<br>
finished yet, so it's anyone's guess. Arc is more<br>
like JavaScript in that methods are part of the hash,<br>
whereas in Perl methods are part of a package that<br>
has the name of the run-time type.<br>
<br>
Now as to why you'd want to do this, I don't know.<br>
Might be useful for adding methods to an instance<br>
on the fly. I've done that in JavaScript, but it might<br>
be awkward in Perl to keep the existing methods and<br>
add a new one.<br>
<br>
If you just changed the object's type on the fly it might<br>
be initialized properly - that is the contents of it's<br>
hash might not have all the right keys set.<br>
<br>
JD<br>
<br></div>