Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

More efficient way to declare variables 2

Status
Not open for further replies.

skw

Instructor
Jun 1, 1999
19
US
Okay, I have my Perl script and I declared my variables. I have a lot of variables, over 50 and it is too time consuming having to type out each variable name, is there a more efficient way to declare my variables?

This is what I have ...

my $frm = new CGI;

my $NAME = $frm->param('NAME');
my $UID = $frm->param('UID');


..
..

print MAIL "Name: $NAME\n";
print MAIL "UId: $UID\n";

It'll be time consuming having to type these out so many times from declaring the variable, setting up the print statements and setting up the print output to screen results.

Any suggestions appreciated.
 
Could you just print them direcly?

print MAIL "Name: ", $frm->param('NAME'), "\n";

Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

It's like this; even samurai have teddy bears, and even teddy bears get drunk.
 
The method I use is:

my %cgiparams = ();
my $cgi = new CGI();
foreach my $name ($cgi->param) {
my $value = $cgi->param($name);
next unless($value);

$cgiparams{$name} = $value
if($rules{$key} && $value =~ /^$rules{$key}$/);
}

The rules are a set of regexes that validate whether a particular paramater is accepted and in the correct format. I then reference everything via $cgiparams{'name'}.

If you wanted to debug this you would simple have:

foreach my $name (keys %cgiparams) { print "$name: $cgiparams{$name}\n" }

Makes life alot easier :)


Barbie
Leader of Birmingham Perl Mongers
 
Barbie, thanks that's exactly what I'm trying to accomplish!

Mike, I'll go through the FAQ link you provided.

 
I should also mention that this is an appropriate method to use to untaint CGI values, if you were using the -T option at all.

Barbie
Leader of Birmingham Perl Mongers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top