Well, this might be going out on a limb, but . . .
Sounds like a job for polymorphism!!!
Here's the scenario - you have a group of objects, each
derive from a common parent (default). Each objects overrides
a method called handle - which will take care of the task at
hand.
Here is code for the Default class:
package Default;
use strict;
sub new {
my $class = shift;
my $self = { };
return bless $self, $class;
}
sub handle {
my ($class, $text) = @_;
return $text;
}
1;
All it does is contain an empty hash, and a method called
handle which simply returns what it was handed.
Here are two classes that will derive from Default:
package Upper;
use strict;
use Default;
use vars qw(@ISA);
@ISA = qw(Default);
sub handle {
my ($class, $text) = @_;
return uc($text);
}
1;
################################
package Lower;
use strict;
use Default;
use vars qw(@ISA);
@ISA = qw(Default);
sub handle {
my ($class, $text) = @_;
return lc($text);
}
1;
And finally, the test script. It is a very simple and
generic test to show the flexibility in using polymorphism.
#!/usr/bin/perl -w
use strict;
use Default;
use Upper;
use Lower;
my $arg = shift || '';
my %handlers = (
default => Default->new,
upper => Upper->new,
lower => Lower->new,
);
my $ref = $handlers{$arg} || $handlers{'default'};
print $ref->handle("AaAaAaAa"), "\n";
The hash is used to map command line arguments to the class
to use - your problem might map the name of a web page to
the class to use.
If the argument is not including among the handlers, then
the default handler is used. This will allow you to make
new handlers (which print out the appropriate HTML page).
If you want to try this out, put each of the 3 classes in
their own file (with .pm extension), and copy the test
script to an executable file in the same directory. Run
the test script with
- no args
- the arg 'lower'
- the arg 'upper'
- the arg 'foobar'
Hope this helps, at least I hope it gives you insight into
ways to solve your problem.
Jeff
L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
F--F--F--F--F--F--F--F--
(the triplet paradiddle)
|