Sometimes I stumble into hard-to-trace bugs because I've misspelled the name of a CGI parameter-- I use
param('username')
in one place, and
param('user name')
in another. What I wind up doing is defining constants like so:
use constant USERNAME_PARAM => 'username';
print "User name is: ", param(USERNAME_PARAM);
This gets old pretty quickly, though. Recently I started to wonder: what if I could set up a way so that I could just set up subroutines automatically to return a named parameter, a la constant.pm?
So I wrote up cgiparam.pm. Using it, one could have a file like so:
use strict;
use CGI ':cgi';
use cgiparam (FOO_PARAM => 'foo',
BAR_PARAM => 'bar',
BAZ_PARAM => 'baz');
print STDOUT "Foo is ", FOO_PARAM, "\n",
"Bar is ", BAR_PARAM, "\n",
"Baz is ", BAZ_PARAM, "\n";
And you'll get the values of the CGI parameters foo, bar, and baz in the printout. Plus, you're still using strict, so if you try to use an unspecified parameter, you'll get a compile-time error.
I've tested this once, but have no idea if it's useful...
UpdateWith help from tye, I've added lvalue capability to the code. So now you can do things like:
use CGI ':cgi';
use cgiparam FOO_PARAM => 'foo';
FOO_PARAM = 'blibble';
print STDOUT FOO_PARAM;
and out will come 'blibble'.
Further update Altered the module so that you can supply the names for your own routines, plus removed the direct reference to CGI.pm there so that one can use CGI::Fast. In the future, I'll have the thing check to see if CGI has been loaded (if possible) and load it as necessary. |