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:
All it does is contain an empty hash, and a method called handle which simply returns what it was handed.package Default; use strict; sub new { my $class = shift; my $self = { }; return bless $self, $class; } sub handle { my ($class, $text) = @_; return $text; } 1;
And finally, the test script. It is a very simple and generic test to show the flexibility in using polymorphism.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;
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).#!/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";
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
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)
In reply to (jeffa) Re: CGI coding style
by jeffa
in thread CGI coding style
by 2501
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |