http://www.perlmonks.org?node_id=161783


in reply to use strict seems to hose my code

Thanks for your replies. I didn't post code initially because I didn't think people would appreciate me dumping the problem directly onto them. But here's the relevant code:

Immediately after the relevant beginning lines, I create three hashes I'll use. %INPUT is the one in question. I haven't got it far enough to know if the other two work. We all know %ENV, and %CFG has values from a config file I load.

my (%INPUT,%CFG,%ENV);

To clean up my "main" program, I dump off the initialization of %INPUT to a subroutine, which I call immediately after my variable declarations (save a few comments in between):

my ($buffer,$name,$value,@pairs,$pair); @pairs = split(/&/, $buffer); foreach $pair (@pairs) { $name, $value) = split(/=/, $pair); $value =~ tr/+/ /; value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg; $INPUT{$name} = $value; print "\$INPUT{$name} = $value<br>"; }


That last line was put in to help me see if any of the values are printed. I expected it to at least print "$INPUT{} = " (null for the actual values), but in fact, it didn't print anything.

Even if I put this code immediately after the above "my (...)" code, it still doesn't give me any output.

Replies are listed 'Best First'.
Re: Some code
by stephen (Priest) on Apr 24, 2002 at 21:15 UTC

    First, what you have there is one of the oldest bits of hand-recopied Perl code on the Internet. It's from the original cgi-lib.pl by a nice fella named Steve Brenner. You should use CGI. See use CGI or die; for why.

    Next, %ENV is a global variable. Putting it in a my declaration will wipe it clean. Thus there will be no $ENV{'QUERY_STRING'}, no $ENV{'CONTENT_LENGTH'}, thus no $buffer, and thus no output. There's no need to try to localize %ENV; strict knows it's a global and won't yammer.

    Update: Never mind %ENV, $buffer is empty. Use CGI instead. :)

    stephen

Re: Some code
by CukiMnstr (Deacon) on Apr 24, 2002 at 21:11 UTC
    Maybe you stripped more than what you had to to post your code here, but from what I can see,
    my ($buffer,$name,$value,@pairs,$pair); @pairs = split(/&/, $buffer);

    you are acting on an empty $buffer: the my declaration of $buffer 'initializes' it, so when you use split() to get the values from it, you are acting on an 'empty' variable. You might want to move that

    my ($buffer);
    a little higher on your script.

    Do you get any warnings that tell you you are acting on undefined data? try printing $buffer before and after the split(), because it might tell you the problem is there...

    Update: you say you don't even get 'empty' lines printed, which tells me you never run the for() loop... try to print() @pairs before entering the for()

    hope this helps,