Passing PERL Variables In a URL

Hi,

As you may know, in PHP it’s easy to pass variables through a URL…a simple ?variable=value stuck on the end of the URL string does the trick…unfortunatly this doesn’t seem to work with Perl. I know it’s possible, but don’t really know how…I recall that it requires the use of an enviromental variable…can someone help me out?


in perl, you have to “get” the variable… php automagically does that for you … isnt php the best … i’m not a perl programmer, i’m a php-all-the-way guy, so i wouldnt know how to “get” the variable, but I bet somebody else in this forum would


just like this

<BLOCKQUOTE><font size=“1” face=“Verdana, Arial”>code:</font><HR><pre>
sub data_parse
{
my $buffer, $pair, @pairs, $name, $value, %FORM;
if ($ENV{“REQUEST_METHOD”} eq ‘POST’)
{
read(STDIN, $buffer, $ENV{‘CONTENT_LENGTH’});
}
else
{
$buffer = $ENV{‘QUERY_STRING’};
}

my (@pairs) = split(/&/, $buffer);
foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$query = $value if ($name eq ‘q’);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(“C”, hex($1))/eg;
if ($FORM{$name} && $name ne ‘q’){
$FORM{$name} .= “~~$value”;
} else {
$FORM{$name} = $value;
}
}
return %FORM;
}



then to call it and access variables do this:

 &lt;BLOCKQUOTE&gt;&lt;font size="1" face="Verdana, Arial"&gt;quote:&lt;/font&gt;&lt;HR&gt;
my %in = &data_parse;

print $in{'var1'} ;
print $in{'var2'} ;
&lt;HR&gt;&lt;/BLOCKQUOTE&gt;



------------------

I place the following code in every script of mine, right under the path to PERL:

<BLOCKQUOTE><font size=“1” face=“Verdana, Arial”>code:</font><HR><pre>
BEGIN {
use CGI::Carp qw(fatalsToBrowser);
}

use CGI qw(param header);
print header;



The above code will do 3 things:

&lt;OL TYPE=1&gt;
&lt;LI&gt; Get the variables
&lt;LI&gt; Print out any errors the script may have, thus avoiding any 500s
&lt;LI&gt; Print the content type
&lt;/OL&gt;

The coe supports both GET and POST methods.  To retreive a variable, simply us:

$variable = param('variable');

Replace "variable" with the name of the field.

Also, I would like to say that PERL is superior in many server side functions that PHP simply CAN'T do.   [img]http://www.SitePoint.com/forums/smile.gif[/img]

Richard

------------------