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

real basic perl question

Status
Not open for further replies.
Jan 10, 2006
3
GB
Hi i am new to perl and have a section of code in a program i have inherited. can any one give the the general idea what it is doing? see the code below. TIA :)

program{
local (*in)=@_if @_;
local ($i,$loc,$key,$val);

if($ENV{'REQUEST_METHOD'} eq "GET"){
$in=$ENV{'QUERY_STRING'};
}elseif($ENV{'REQUEST_METHOD'}eq"POST"){
read(STDIN,$in,$ENV{'CONTENT_LENGTH'});
}
@in=split(/&/,$in);
foreach $i(0..$#in){
$in[$i]=~s/\+//g;

($key,$val)=split(/=/,$in[$i],2);

@key=~s/%(..)/pack("c",hex($1))/ge;
@val=~s/%(..)/pack("c",hex($1))/ge;

@in{$key}.="\0" if (defined($in{$key}));
@in{$key}.=$val;
}
return 1;
}
 
I believe it's for getting data sent to the server, could be from a form or from a URI string. Better to use the CGI module for this purpose. Looks like it came from an old script.
 
yeah i understood the initial part where it gets the post from the html form but i dont really understand what it is doing with the posted info after that point?

@in=split(/&/,$in);
foreach $i(0..$#in){
$in[$i]=~s/\+//g;

($key,$val)=split(/=/,$in[$i],2);

@key=~s/%(..)/pack("c",hex($1))/ge;
@val=~s/%(..)/pack("c",hex($1))/ge;

@in{$key}.="\0" if (defined($in{$key}));
@in{$key}.=$val;
}
return 1;
}
this bit basically???
 
Its defenitly a part of some CGI script that process some kind of form.
The information in a form can be send either by GET or POST method.So first script is checking the method.
eg is using GET method.
qid=1175050&page=1 is the info passed to the server.
This script is trying to extract this info.Script will split the args at '&' and '=' and seems pushing to a hash.

GET method is not secure because the feilds passed are visible in the address bar.So use POST instead.
use CGI modules for CGI programming.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top