<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>Message</TITLE>
<META content="text/html; charset=iso-8859-1" http-equiv=Content-Type>
<META content="MSHTML 5.00.3835.2200" name=GENERATOR></HEAD>
<BODY>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN class=488163121-10012006>Beware
re-blessers that forgot to clean</SPAN></FONT><FONT color=#0000ff
face=Arial size=2><SPAN class=488163121-10012006> their dirty
laundry.though..</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=488163121-10012006></SPAN></FONT> </DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=488163121-10012006> From
perlobj:</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=488163121-10012006></SPAN></FONT> </DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=488163121-10012006> Although a
constructor can in theory re-bless a
referenced<BR> object currently belonging to
another class, this is almost<BR> certainly going
to get you into trouble. The new class
is<BR> responsible for all cleanup later.
The previous blessing is<BR> forgotten, as an
object may belong to only one class at a<BR>
time. (Although of course it's free to inherit methods
from<BR> many classes.) If you find yourself
having to do this, the<BR> parent class is
probably misbehaving, though.</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=488163121-10012006></SPAN></FONT> </DIV>
<DIV> </DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=488163121-10012006>The plain vanilla re-bless below for
instance doesn't call Foo's DESTROY method -- only</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN class=488163121-10012006>Bar's.
</SPAN></FONT><FONT color=#0000ff face=Arial size=2><SPAN
class=488163121-10012006></SPAN></FONT></DIV>
<DIV> </DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=488163121-10012006> package Foo;<BR> sub new { my
$self= shift; bless {}, $self }<BR> sub zap { my($self,$newself)=@_;
bless $self, $newself }<BR> DESTROY { print "cleaning up Foo...";
}</SPAN></FONT></DIV>
<DIV> </DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=488163121-10012006> package Bar;<BR> sub new { my
$self= shift; bless {}, $self };<BR> DESTROY { print "cleaning up
Bar..."; }</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=488163121-10012006><BR> package main;</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=488163121-10012006> my $foo =
Foo->new("Bar");<BR>
$foo->zap("Bar");<BR> </SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=488163121-10012006>hth, </SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN class=488163121-10012006>--
</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=488163121-10012006>Charles DeRykus</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=488163121-10012006></SPAN></FONT> </DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=488163121-10012006></SPAN></FONT> </DIV>
<BLOCKQUOTE style="MARGIN-RIGHT: 0px">
<DIV></DIV>
<DIV align=left class=OutlookMessageHeader dir=ltr lang=en-us><FONT
face=Tahoma size=2>-----Original Message-----.<BR><B>From:</B> JD Brennan
[mailto:jazzdev@gmail.com] <BR><B>Sent:</B> Tuesday, January 10, 2006 12:15
PM<BR><B>To:</B> Fred Morris<BR><B>Cc:</B> spug-list@pm.org<BR><B>Subject:</B>
Re: SPUG: Unidentified Flying Objects<BR><BR></FONT></DIV>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: rgb(204,204,204) 1px solid; 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></BLOCKQUOTE></BODY></HTML>