C
ccc31807
Let's suppose you have a web app, and your HTML user interface
contains menu items like this:
<a href="site?menu=home">HOME</a>
<a href="site?menu=faq">FAQ</a>
<a href="site?menu=links">LINKS</a>
<a href="site?menu=contact">CONTACT US</a>
On the server side, you have code that looks like this (as STYLE A):
use CGI;
my $menu = param('menu');
if ($menu eq 'home') { do_this(); }
elsif ($menu eq 'faq') { do_that(); }
elsif ($menu eq 'links') { do_something_else(); }
elsif ($menu eq 'contact') { do_another_thing(); }
else { print "CGI ERROR, unknown $menu\n"; }
Some languages (Lisp and Erlang, for instance) have functions that are
conceptually overloaded and specialized according to their parameters.
For example, using a Perlish hypothetical language, you might do this
(as STYLE B):
do_this('home');
do_this('faq');
do_this('links');
do_this('contact');
do_this(_); # underscore represents an unknown value
Is there any way to specialize functions in Perl based on the value of
their parameters? The app is written in a procedural style, not OO,
and I'd like a way to introduce polymorphism without rewriting it as
an OO app.
The motivation for this question is an application that started with a
small number of elsif branches, and now has dozens of branches and has
become a nightmare to maintain. I have written a little script that
can change the source code from STYLE A to STYLE B but I haven't been
able to figure out a way to use Perl for Lisp-like specialization or
Erlang-like pattern matching.
Thanks, CC.
contains menu items like this:
<a href="site?menu=home">HOME</a>
<a href="site?menu=faq">FAQ</a>
<a href="site?menu=links">LINKS</a>
<a href="site?menu=contact">CONTACT US</a>
On the server side, you have code that looks like this (as STYLE A):
use CGI;
my $menu = param('menu');
if ($menu eq 'home') { do_this(); }
elsif ($menu eq 'faq') { do_that(); }
elsif ($menu eq 'links') { do_something_else(); }
elsif ($menu eq 'contact') { do_another_thing(); }
else { print "CGI ERROR, unknown $menu\n"; }
Some languages (Lisp and Erlang, for instance) have functions that are
conceptually overloaded and specialized according to their parameters.
For example, using a Perlish hypothetical language, you might do this
(as STYLE B):
do_this('home');
do_this('faq');
do_this('links');
do_this('contact');
do_this(_); # underscore represents an unknown value
Is there any way to specialize functions in Perl based on the value of
their parameters? The app is written in a procedural style, not OO,
and I'd like a way to introduce polymorphism without rewriting it as
an OO app.
The motivation for this question is an application that started with a
small number of elsif branches, and now has dozens of branches and has
become a nightmare to maintain. I have written a little script that
can change the source code from STYLE A to STYLE B but I haven't been
able to figure out a way to use Perl for Lisp-like specialization or
Erlang-like pattern matching.
Thanks, CC.