Click to See Complete Forum and Search --> : Perl Problem with $ENV{'QUERY_STRING'}


MrNewbie
06-13-2001, 12:25 PM
I found an example of getting the values submitted to a CGI script and storing them in a hash array so I modified it to do what I need but I like to use strict and warnings to help me write better perl. Here is my code:


#!/usr/bin/perl -w

use strict;
use warnings;

my %cgivars;
%cgivars = &getcgi();

sub getcgi {
local(%vars);
local($name, $value);

foreach (split('&', $ENV{'QUERY_STRING'})) {
s/\+/ /g;
($name, $value)= split('=', $_, 2);
$name=~ s/%(..)/chr(hex($1))/ge;
$value=~ s/%(..)/chr(hex($1))/ge;
$vars{$name}.= "\0" if defined($vars{$name});
$vars{$name}.= $value;
}
return %vars;
}


The problem I have is with this:

local(%vars);
local($name, $value);

Can't I do:

my local(%vars);
my local($name, $value);

So that the variables are predefined? It seems I can't because if I do I get lots of errors. What can I do to stop the errors?
Basically the only errors I got without the my's are that the variables need explicit declaration. But with them I get the same errors and these too:

No such class local at banadmin.pl line 10, near "my local"
Global symbol "%vars" requires explicit package name at banadmin.pl line 10.
No such class local at banadmin.pl line 11, near "my local"

Sorry for wording things unclearly or confusingly hehe but can anyone help?
Thanks

MrNewbie
06-13-2001, 03:29 PM
I've managed to fix it! But I have a related question, is there a way to pass a $ENV{'QUERY_STRING'} string to the program from command line? I've tried:

perl program.pl arg=value action=value
perl program.pl 'arg=value&action=value'

And neither seem to work.
Thanks

YaRness
06-13-2001, 04:23 PM
you want to use just "my", not "local" in your proggie. you will probably never have to use local().

if the perl script is not called from a web site, i don't think there IS a 'query string' environment variable. if you are trying command-line stuff for testing purposes, maybe you can stick in a couple lines of code that pull your data off the command line instead of the query string. like


#switch these two when you want to use URL args instead of command line args
#my $string = $ENV{'QUERY_STRING'}
my $string = join '', @ARGV;
#
foreach (split('&', $string)) {
#etc

and then do foo=bar_AMPERSAND_baz=narf on the command line. or in the above code do "join '&'" and do foo=bar baz=narf on the command line if it doesn't work with the ampersands on the command line (i'm working in windows 2000, so i had to do the latter, i dunno how bash will handle ampersands).


[edit] stupid ampersand was getting eaten by ubb code

[ 13 June 2001: Message edited by: YaRness ]

MrNewbie
06-13-2001, 04:38 PM
Bash won't handle &'s because it takes that as it should excecute the command before it then the command after it but I think that all that I need to do is write something to convert a different character that I will use on the command line instead, into an ampersand then it should work I think.
Thanks a lot

[GoRN]
06-13-2001, 05:50 PM
QUERY_STRING='foo=bar&baz=fu' perl program

that should work.

good luck

Ben Briggs
06-13-2001, 06:11 PM
The make bash accept the &'s, put a slash before each one:

test=1\&hello=world

MrNewbie
06-14-2001, 06:52 AM
Wow the QUERYY_STRING='foo=bar&baz=fu' perl program worked, thanks!

TheLinuxDuck
06-14-2001, 07:57 PM
Mr Newbie:

If you use the CGI module for perl, it will make the handling of input much easier. Their param function will get data from the browser via POST or GET, and also from the command line. For example:

#!/usr/bin/perl -w
use strict;
use CGI;
#
# create new CGI object
#
my($cgi)=new CGI();
#
# check for parameters, then list them, with
# their values
#
if($cgi->param()) {
for($cgi->param()) {
print "param: $_\t\t\tvalue: ",
$cgi->param($_), "\n";
}
}
exit;


Here is the command line use:

root@massive:/apache/cgi-bin# ./testparam.cgi this=that I=me bacon=good warm=soc
ks%2f%20man!
Content-type: text/plain

param: this value: that
param: I value: me
param: bacon value: good
param: warm value: socks/ man!


And from a browser, using testparam.cgi?this=that&I=me&bacon=good&warm=socks%2f%20man! (http://testparam.cgi?this=that&I=me&bacon=good&warm=socks%2f%20man!)

param: this value: that
param: I value: me
param: bacon value: good
param: warm value: socks/ man!


Makes it really REALLY easy! (^=

[ 14 June 2001: Message edited by: TheLinuxDuck ]

YaRness
06-15-2001, 10:47 AM
i love perl modules, they are always doing cool stuff behind your back.

TheLinuxDuck
06-15-2001, 02:41 PM
Originally posted by YaRness:
<STRONG>i love perl modules, they are always doing cool stuff behind your back.</STRONG>

heck, you're the reason why I started getting into them in the first place. you kept doing stuff with your lno.pm module, and it inspired me to learn about them. Since then, kmj and I have implemented modules on the ccae site for common functions the scripts share. (^=

So, thanks! (^=

YaRness
06-15-2001, 02:50 PM
i didn't really do all that much, just put all my junk into one place.

i'm always wanting to do more stuff with other people, but i just never have the time after work and other stuff i do. i've been trying to get the web site for my dojo up, but i just haven't gotten around to finishing it. it's part laziness, but part just trying to have some me time :/.

TheLinuxDuck
06-16-2001, 12:26 AM
Originally posted by YaRness:
<STRONG>i'm always wanting to do more stuff with other people, but i just never have the time after work and other stuff i do.</STRONG>

I hear you. I've been on a java kick lately, so I've neglected the ccae site as well as other stuff.. man, it's tough to balance all this stuff I wanna do out!! (^=

If you want to collaborate with someone on a project, why don't you and I pick something small and just do it as it comes, so we're not really under any kind of deadline..

<STRONG>i've been trying to get the web site for my dojo up, but i just haven't gotten around to finishing it. it's part laziness, but part just trying to have some me time :/.</STRONG>

Heh.. me.. it's straight up laziness. sometimes I wish I was more motivated for this kind of thing, but then I think if I was, I'd never take the time to look around me and just enjoy the moment.

Or something like that.