faqts : Computers : Programming : Languages : Perl : CGI Programming

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

27 of 257 people (11%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

How do I pass arguments from a webpage to a Perl script?

Mar 26th, 2001 08:57
Allison Smith, Per M Knutsen,


Use CGI.pm for parsing the query string:
#!/usr.bin/perl
use CGI qw(:standard);
$a = param ("var1"); # $a is now "num1"
$b = param ("var2"); # $b is now "num2"
assuming your FORM looks something like this:
<FORM ACTION="script.pl" METHOD="post">
  <INPUT TYPE="text" NAME="var1">
  <INPUT TYPE="text" NAME="var2">
      other form elements go here...
  <INPUT TYPE="submit">
</FORM>
You do not have to use the CGI modual:
For an html form with the method="post" and the above form use:
#gather Variables from STDIN
$content_length = $ENV{'CONTENT_LENGTH'};
read (STDIN, $posted_information, $content_length);
#Decoding the Data
$posted_information =~ s/\+/" "/eg;
$posted_information =~ s/\%20/" "/eg;
#Splitting into Variables
(@fields) = split (/&/, $posted_information);
($variable, $var1) = split (/=/, @fields[0]);
($variable, $var2) = split (/=/, @fields[1]);
#print the http header, begin html page for all
print "Content-type: text/html", "\n\n";
#print the variables
print $var1, $var2;
For the above form using the method "GET", use:
#Splitting into Variables
(@fields) = split (/&/, $ENV {'QUERY_STRING'});
($variable, $var1) = split (/=/, @fields[0]);
($variable, $var2) = split (/=/, @fields[1]);
#Decoding the Data
$var1 =~ s/\+/" "/eg;
$var1 =~ s/\%20/" "/eg;
$var2 =~ s/\+/" "/eg;
$var2 =~ s/\%20/" "/eg;
#print the http header, begin html page for all
print "Content-type: text/html", "\n\n";
#print the variables
print $var1, $var2;