SPUG: Unidentified Flying Objects

Fred Morris m3047 at inwa.net
Tue Jan 10 10:26:20 PST 2006


When I contemplate doing something odd, and particularly if it seems
brilliant, I like to take a step back and ask two questions:

1) Can I explain it to another human being?

2) How would I implement this in some other environment?

So I am going to try to explain this, and then at the end there is a little
standalone Perl script which runs and demonstrates just how easy it is to
change the class of an object in midair using Perl.

So my questions are:

1) After reading this through, does it make a bit of sense? :-p

2) I can think of ways to do this with things like a busybox or a container
with snapins (the last bastions of OO zealots who can't admit that
sometimes a callback is just a callback... regardless of whether the object
in question is disc-shaped or cigar-shaped), but are there any other
languages (particularly agile languages) which make it this as seductively
easy? Can you do this in Python (just a dabbler, me)? Ruby? What about Perl
6?



"Unidentified Flying Object": that's an oxymoron, isn't it? You've
identified it as a flying object (look, up in the sky...).

For that matter you've identified it as "unidentified". Maybe being
unidentified gives it some special properties such as telling other air
traffic to give it a wide berth, and if it disappears suddenly off of radar
to send the Marines instead of an ambulance. Maybe you just don't really
care, and identifying it as "unidentified" is good enough for your
purposes.

What if you make a mistake (it's a bird, no it's a plane..)? Wouldn't it be
nice to fix that as easily as saying "no, it's something else"?

In the actual case at hand there is a base class which implements various
scripts for behaviors, and the subclasses provide methods which are
combined to implement those behaviors. Most of the time you know what
something is. But sometimes you don't yet in the course of executing a
behavior it becomes obvious what it is.

The script which follows does this in a most brutal fashion by calling the
same method twice, once before transmogrification and once afterwards; in
practice the internal methods resemble more a series of steps.

--

Fred Morris
http://www.inwa.net/~m3047/contact.html

--

#!/usr/bin/perl -w

use strict;

package Flyer;

sub new {

    my $class = shift;

    my $self = { };

    return bless $self, $class;

} # &new

sub zap {

    my $self = shift;
    my $new_me = shift;

    print "ZAP!\n";

    return bless $self, $new_me;

} # &zap

sub i_am_a {

    my $self = shift;

    return "I'd tell you but then I'd have to kill -9 you";

} # &i_am_a

sub on_a_typical_day {

    my $self = shift;
    my $new_me = shift;

    print 'I am a ' . $self->i_am_a() . "\n";
    $self->zap( $new_me );
    print 'I am a ' . $self->i_am_a() . "\n";

} # &on_a_typical_day

package Bird;

use vars qw/ @ISA /;
@ISA = qw/ Flyer /;

sub i_am_a {

    my $self = shift;

    return "bird, with feathers";

} # &i_am_a

package Plane;

use vars qw/ @ISA /;
@ISA = qw/ Flyer /;

sub i_am_a {

    my $self = shift;

    return "plane, with engines";

} # &i_am_a

# -=-=- let the show begin -=-=-

package main;

my $ufo = Bird->new();

$ufo->on_a_typical_day( 'Plane' );

exit(0)

__END__




More information about the spug-list mailing list