...

View Full Version : POST and GET



Phil Jackson
01-27-2010, 04:46 PM
Hi, could someone please explain how post and get data is delt with in perl?

Phil Jackson
01-27-2010, 05:14 PM
I think im on the right track but getting silly error:



for (split /\&/, <STDIN>) {
(my $key, my $val) = split /=/;
$val =~ s/\+/ /g;
$val =~ s/%([0-9a-fA-F]{2})/chr(hex($1))/ge;
my $arg{$key} = $val;
}


error
global symbol $val requires explicit package name


Any ideas on why?

FishMonger
01-27-2010, 06:34 PM
Besides the syntax error on the var assignment, you're using the wrong approach. You should be using the CGI module.


use CGI;

my $cgi = CGI->new; # handles/processes both GET and POST
my %arg = $cgi->Vars; # Let the module handle the dirty work

FishMonger
01-27-2010, 06:35 PM
To answer your question more directly:

change:

(my $key, my $val) = split /=/;

to:

my ($key, $val) = split /=/;

CrzySdrs
01-27-2010, 06:38 PM
I am not sure why you are getting that error. I made a few minor changes to your code, anyway due to some other minor issues.

I ran on perl v5.8.8


#!/usr/bin/perl
use strict;

my %arg;
for (split /\&/, <STDIN>) {
my ($key, $val) = split /=/;
$val =~ s/\+/ /g;
$val =~ s/%([0-9a-fA-F]{2})/chr(hex($1))/ge;
$arg{$key} = $val;
}

foreach my $k (keys %arg) {
print "Key:" . $k . ' Value:' . $arg{$k} . "\n";
}


And tested it as such:


$ echo "test=1&hello=5" | ./test2.pl
Key:test Value:1
Key:hello Value:5


However, I would highly recommend that you use the CGI module from CPAN to write any web based app, since it provides the functionality to get all submitted variables. http://search.cpan.org/dist/CGI.pm/lib/CGI.pm#FETCHING_THE_VALUE_OR_VALUES_OF_A_SINGLE_NAMED_PARAMETER:

This module is usually in a default perl installation so you may already have it.

Phil Jackson
01-28-2010, 06:12 AM
it was the "my %arg" but going to try the mod as i've read it is better practice for some reason, cheers guys.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum