Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Ovid (2709)

Ovid
  (email not shown publicly)
http://publius-ovidius.livejournal.com/
AOL IM: ovidperl (Add Buddy, Send Message)

Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.

Journal of Ovid (2709)

Tuesday January 17, 2006
06:24 PM

Polymorphism instead of conditionals

[ #28392 ]

Some days I just get mental blocks. I had some ugly code which was trying to test which server I wanted to start and the code was filled with if/elsif/else blocks depending upon the server I wanted to launch. There's something about trying to work with servers and launch new processes which causes me to forget about robust language design. Then my boss asked my why I was doing it the way I was doing it and it took me a bit to realize I was being foolish. Now, my large, ungainly code has been reduced to this (pared down for readability):

my $command = lc shift @ARGV || '';
unless ( $command =~ /^stop|start|restart$/ ) {
    die localize("You must specify stop, start, or restart");
}

my $engine = ENGINE_CLASS;   # loaded elsewhere
eval "use $engine";
if ( my $error = $@ ) {
    die localize("Could not load [_1]: [_2]", $engine, $error);
}

$engine->$command;

Now, each engine class implements its own start, stop, and restart methods and all of the code is much cleaner. It bugs me when I get mental blocks like this.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.