简体   繁体   中英

How to assign perl cgi url query string to variable?

Please suggest how to assign query_string values to individual variable ?

my @values = split(/&/,$ENV{QUERY_STRING});

foreach my $i (@values) {
    ($fieldname, $data) = split(/=/, $i);
    print "$fieldname = $data<br>\n";
}

I am getting values below. I am trying to assign to individual values like $org, $value,$source,$target.

key =  MAIN
value = test
source = master
target = 123

Firstly, manual parsing of CGI parameters using code like that went out of fashion twenty years ago. You could be far better advised to use the param() method from CGI.pm.

use CGI 'param';

foreach my $fieldname (param()) {
  print "$fieldname = ", param($fieldname), "\n";
}

You could declare the variables directly.

use CGI 'param';

my $key =   param('something');
my $value = param('something else');
# etc...

But perhaps it's better to store your parameters in a hash.

use CGI 'param';

my %param;
foreach (param()) {
  $param{$_} = param($_);
}

And finally, it's 2015. Why are you still writing CGI programs?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM