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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing paramaters via browser. 1

Status
Not open for further replies.

naveed010

Programmer
Joined
May 23, 2007
Messages
2
Location
CA
Hi there, fairly new at perl.

Trying to call a perl script with paramaters being passed in. I want to pass two values into a perl script from the browser.

The reading I have done has told me to use a form with a submit buttong, however, I don't want to use a "submit" button.

I want to use a normal link that points to a href=
How do I properly pass in the paramaters, and how do I retreive and use them in the subsequent "perlscript.pl" script.

Thanks in advance.
 
Hi

The GET parameters are available to CGI scripts in the QUERY_STRING environment variable. ( The POST data is available to CGI scripts on the standard input. )
Code:
[gray]# split them[/gray]
@pars=split /&/,$ENV{'QUERY_STRING'};
[gray]# process each[/gray]
foreach $par (@pars) {
  [gray]# separate name and value[/gray]
  ($name,$val)=split /=/,$par;
  [gray]# decode URLEncoded[/gray]
  $val=~s/%([[:xdigit:]]{2})/pack "C",hex $1/ge;
  [gray]# put it in a hash[/gray]
  $par{$name}=$val; 
}
Or just use the CGI module.

Feherke.
 
You've got it right (although you should escape the ampersand to & to be properly HTML compliant). Parse parameters with the standard CGI module. Here's a great CGI tutorial.
 
Thanks guys... Am I just being sloppy/lazy by doing this:

use CGI qw(:param);
my $q = new CGI;

my $val1=param('value1in');
my $val2=param('value2in');


...etc
 
Nope. That's perfectly fine, and vastly preferable to using any of the many self-rolled "solutions" you see around the web. For instance, the code feherke posted doesn't handle multiple values for parameters, or the recommended method of separating parameters with semicolons rather than ampersands (since semicolons don't have to be encoded in HTML).
 
Hi

Absolutely agree. I know my code's deficiencies, but until now every time I wrote a CGI script I needed results faster then would take reading the manual. And this time I forgot to decode the + too. :-(

Regarding the change from ampersand ( & ) to semicolon ( ; ), could you tell me on what is that based ? HTML or HTTP ? I never heard until now that anyone would like to change it. Thank you.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top