PDA

View Full Version : Easy Way To Parse Multiple Pairs


netroact
02-11-2006, 06:58 PM
Whenever I send data from a form to a Perl scripts, there are occasions when I send gobs of field data to the script. I always list out each one individually:

i.e.

my $name = $query->param('name');
my $age = $query->param('age');
my $specs = $query->param('specs');
my $address = $query->param('address');
my $shower_time = $query->param('shower_time');


I use a third party credit card processor on my websites, where I simply send certain predefined names to their script. Some data they use, and other info is then passed to my script on my website return page. If you have a high rated account (not mine), you can send any names to their script, and it will use the applicable ones, and pass the rest on for you.

How do they do that?

Is this a security issue?

Does my post make sense?

FishMonger
02-11-2006, 07:09 PM
I'm not really sure what you're asking, but I find it much easier to read-in all of the form fields in 1 step, placing them into a hash.

http://search.cpan.org/~lds/CGI.pm-3.16/CGI.pm#FETCHING_THE_PARAMETER_LIST_AS_A_HASH%3A

netroact
02-11-2006, 07:43 PM
I believe that is what I am looking for. I've searched that page many times before. I know you can fetch the names and I know you can fetch the values, but how do fetch the name/value pairs? If you could provide an example, it would be very helpful.

netroact
02-11-2006, 07:58 PM
Never mind. You answered my question in mkphoto44's thread.

netroact
02-13-2006, 04:07 AM
I guess I still don't git it Fish Monger. I have a problem with hashes.

I just want to know if there's a way to parse all the fields from a form without listing out each individual parse statement like I did above. It gets to be tiresome to list out a million fields to be parsed, like I am always having to do with my PerlMagick scripts.

FishMonger
02-13-2006, 05:52 AM
Are you using the object-oriented or function-oriented method of the cgi module?

If you're using the object-oriented method, as your earlier post indicates, then this is all that's needed to parse all of the fields placing each field name and value into a hash.
use CGI;

my $query = new CGI;
my %form = $query->Vars;

print "Name: $form{'name'}\n";
print "Age: $form{'age'}\n";
# etc etc

If you're using the function-oriented method, then it's done like this:
use CGI qw(:standard :cgi-lib);

my %form = Vars;

print "Name: $form{'name'}\n";
print "Age: $form{'age'}\n";
# etc etc

netroact
02-13-2006, 06:06 AM
That would be easier for me for some applications, but I was thinking there must be a way to read all the name/value pairs into a hash, and parse them all at once. Then, I could just use them as variables in the script.

use CGI;

my $query = new CGI;

print "Name: $name\n";
print "Age: $age\n";
# etc etc

But, I guess that's just about the same thing, anyways.

Thanks for your help again.

netroact
02-13-2006, 06:32 AM
I started this script awhile back to make animated banners. By the time I get around to finishing it, I will probably have at least 100 form fields to parse. I'm just looking for a shortcut.



#!/usr/bin/perl
use Image::Magick;
use CGI qw(:standard);

use CGI::Carp qw(fatalsToBrowser);

#################################################################################################

my $query = new CGI; # create new CGI object

my $delay = $query->param("delay");
my $banner1 = $query->param("banner1");
my $banner2 = $query->param("banner2");
my $banner3 = $query->param("banner3");
my $banner4 = $query->param("banner4");
my $banner5 = $query->param("banner5");
my $banner6 = $query->param("banner6");
my $banner7 = $query->param("banner7");
my $banner8 = $query->param("banner8");
my $banner9 = $query->param("banner9");
my $banner10 = $query->param("banner10");
my $new_banner = $query->param("new_banner");


if ($banner1)
{

my $directory = "/home/username/public_html/banners";

my @bannermator = ("/$directory/$banner1.gif","/$directory/$banner2.gif");


if ($banner3) { push (@bannermator, "/$directory/$banner3.gif"); }
if ($banner4) { push (@bannermator, "/$directory/$banner4.gif"); }
if ($banner5) { push (@bannermator, "/$directory/$banner5.gif"); }
if ($banner6) { push (@bannermator, "/$directory/$banner6.gif"); }
if ($banner7) { push (@bannermator, "/$directory/$banner7.gif"); }
if ($banner8) { push (@bannermator, "/$directory/$banner8.gif"); }
if ($banner9) { push (@bannermator, "/$directory/$banner9.gif"); }
if ($banner10) { push (@bannermator, "/$directory/$banner10.gif"); }

my($image, $x);
$image = Image::Magick->new;
$x = $image->Read(@bannermator);
warn "$x" if "$x";
$x = $image->Set(delay=>"$delay");
warn "$x" if "$x";
$x = $image->Set(dissolve=>75);
warn "$x" if "$x";
#$x = $image->Set(dispose=>background);
#warn "$x" if "$x";
$x = $image->Set(loop=>infinite);
warn "$x" if "$x";
$x = $image->Write('x.gif');
warn "$x" if "$x";



rename("x.gif", "/home/username/public_html/banners/$new_banner.gif") || die "The file could not be renamed! $!";


print "Content-type: image/gif\r\n\r\n";
print "<html><head></head>\n";
print "<body>\n";
print "<table width='770'><tr>\n";
print "<td align='center' valign='middle'>\n";
print "<br><br><img src='http://somedomain.com/banners/$new_banner.gif'><br><br>\n";
print "</td></tr></table></body></html>";


#################################################################################################

}
else
{

#################################################################################################


my $cgi = new CGI;
my $upload_dir = '/home/username/public_html/banners'; #directory where files will be uploaded.
my $max_files = 10; #maximum number of files allowed.



for (my $count=1; $count<=$max_files; $count++){
my $filen = "file".$count;
my $file = $cgi->param($filen);
my $filename = $file;
$filename =~ s/^.*(\\|\/)//g;

if($file)
{
open(OUT, ">$upload_dir/$filename") || die print "Fail to upload: $!";
while(<$file>)
{
print OUT;
}
close(OUT);
push (@bannermator, "$upload_dir/$filename");
}
}

my($image, $x);
$image = Image::Magick->new;
$x = $image->Read(@bannermator);
warn "$x" if "$x";
$x = $image->Set(delay=>"$delay");
warn "$x" if "$x";
$x = $image->Set(dissolve=>75);
warn "$x" if "$x";
#$x = $image->Set(dispose=>1);
#warn "$x" if "$x";
$x = $image->Set(loop=>infinite);
warn "$x" if "$x";
$x = $image->Write('x.gif');
warn "$x" if "$x";



rename("x.gif", "$upload_dir/$new_banner.gif") || die "The file could not be renamed 2nd part! $!";



print "Content-type: image/gif\r\n\r\n";
print "<html><head></head>\n";
print "<body>\n";
print "<table width='770'><tr>\n";
print "<td align='center' valign='middle'>\n";
print "<br><br><img src='http://somedomain.com/banners/$new_banner.gif'><br><br>\n";
print "</td></tr></table></body></html>";



#################################################################################################

}



I'm still trying to learn how to simplify my code.

shyam
02-13-2006, 08:34 AM
are u looking at something like this

#!/usr/bin/perl -w

use strict;

my %form;
$form{'name'} = 'jack';
$form{'age'} = 48;
$form{'city'} = 'boston';
my $key;
foreach $key (keys(%form)) {
print $key, ': ', $form{$key}, "\n";
} # endfor

or more likely for u case

my $bannerCount = $query->param('bannerCount');
my @bannermator = ();
my $directory = "/home/username/public_html/banners";
for ( my $i = 1; $i < $bannerCount; $i++ ) {
my $banner = $query->param('banner' . $i);
if ( $banner ) {
push(@bannermator, "/$directory/$banner.gif");
}
}

netroact
02-13-2006, 05:38 PM
Thanks for the reply shyam. The 2nd part you reworked will be useful, but with the first part you reworked I see no advantage, because you still have to list the fields. I'm trying to learn whether or not there is a way to parse all the fields from the form without having to list each one individually.

Comminication is not one of my better attributes.

Thanks again for the help.

FishMonger
02-13-2006, 06:14 PM
I don't understand your issue with using the 1 line of code that I showed that imports your params into the hash!
Where/how do you expect to handle the assignment to these
print "Name: $name\n";
print "Age: $age\n";
If you don't want to use the single line and you want them automatically imported, you'll have to modify the code in the CGI module to accommodate that requirement, which is going to be a lot harder and less portable.

Here's another option, which I've never used: "IMPORTING ALL PARAMETERS INTO A NAMESPACE:" http://search.cpan.org/~lds/CGI.pm-3.16/CGI.pm#IMPORTING_ALL_PARAMETERS_INTO_A_NAMESPACE%3A

As far as the other part of your issue, I'd use a single named param for the banners so that when you import the params, the banner values are imported as an array in a single param. i.e. $form{'banners'} holds an array of all the banners (1..10). I'd need to see your html form to give more detailed info on how you need to setup the form and script.

FishMonger
02-13-2006, 06:54 PM
Here's a short example assuming you're passing an array of banner fields.

use strict;
use CGI;

my $cgi = new CGI;
my %form = $cgi->Vars;
my $directory = "/home/username/public_html/banners";
my @bannermator = map { "$directory/$_.gif" } split("\0", $form->{'banners'});

print "$_\n" for @bannermator;

EDIT: that map command replaces the for loop that shyam postd.

netroact
02-13-2006, 07:15 PM
I don't understand your issue with using the 1 line of code that I showed that imports your params into the hash!
Where/how do you expect to handle the assignment to these


If you don't want to use the single line and you want them automatically imported, you'll have to modify the code in the CGI module to accommodate that requirement, which is going to be a lot harder and less portable.

Here's another option, which I've never used: "IMPORTING ALL PARAMETERS INTO A NAMESPACE:" http://search.cpan.org/~lds/CGI.pm-3.16/CGI.pm#IMPORTING_ALL_PARAMETERS_INTO_A_NAMESPACE%3A

As far as the other part of your issue, I'd use a single named param for the banners so that when you import the params, the banner values are imported as an array in a single param. i.e. $form{'banners'} holds an array of all the banners (1..10). I'd need to see your html form to give more detailed info on how you need to setup the form and script.


I think this is what I am looking for.

netroact
02-13-2006, 07:27 PM
Here's a short example assuming you're passing an array of banner fields.

use strict;
use CGI;

my $cgi = new CGI;
my %form = $cgi->Vars;
my $directory = "/home/username/public_html/banners";
my @bannermator = map { "$directory/$_.gif" } split("\0", $form->{'banners'});

print "$_\n" for @bannermator;

EDIT: that map command replaces the for loop that shyam postd.

This will make that part easier. By the way these are from file input types, where they browse their local computer for the values (.gifs). If I understand this right, it will read all the values from the form, and put them in an array if they exist.