jerrygarciuh has asked for the wisdom of the Perl Monks concerning the following question:

I am using a form parsing routine I picked up here at PM without success. I believe I must have misunderstood what it is doing I thought it was doing the equivalent of my $variable = $q->param( 'variable' ); for each param it receives but when I try to print values 2 things happen:
1)The script dies if the variable wasn't declared with my. I thought the  foreach my $str (@names) was taking care of the declarations.
2) The params are undef when I try to print them (even inside the parse_form sub. However, if I add lines like
my $date = $q->param( 'date' ); my $filet = $q->param( 'filet');
I get my params and their values. Will someone please point out whatever it is that I fail to comprehend about this process.
TIA
jg
#!/usr/local/bin/perl -wT use strict; use lib qw(/home/nolaflash/www/local_mods); use CGI ':standard'; use CGI::Carp qw/fatalsToBrowser /; use CGI::Pretty qw( :html3 ); my $q = CGI->new(); if ( $q->param() ) { parse_form(); } sub parse_form { my $sr; my @names=$q->param; foreach my $str (@names) { $sr->{$str} =$q->param($str); } print $q->header,start_html," @names $action $time $date filet = $ +filet",end_html; exit; }
_____________________________________________________
It's not my tree.

Replies are listed 'Best First'.
(jeffa) Re: Lost Values from parse_form Sub
by jeffa (Bishop) on Mar 10, 2002 at 15:39 UTC
    print $q->header,start_html," @names $action $time $date filet = $file +t",end_html;
    You have 4 undeclared variables, don't you mean to use those as hash keys instead?
    print $q->header,start_html, $sr->{action}, $sr->{time}; # etc.
    But, why are you even copying from $q->param?

    UPDATE:
    Sometimes i use grep to grab out things like submit buttons, say that i have a form with multiple submits whose names are prefaced with 'go_':

    my @choice = grep /go_/, $q->param;
    No need to copy the contents of CGI::param() to an array, loop through it copying each value to hash ... grep!

    UPDATE UPDATE: and if you are saying "hey, that only gets the names, what about the values?"

    my %choice = map {$_ => $q->param($_)} grep /go_/, $q->param();

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      OK I appreciate the simplicity of the importing of the param info into the hash, and I see now that I was receiving the values but not accessing them correctly. This leads me to a couple of questions:
      • Was I making an anonymous hash with my sub? That would explain why the variables were undef, because they were really vars, just hash keys right?
      • So how would I add a line(s) so that the sub would automatically translate param( 'this' ) into $this and assign the value it received? I guess I should use map on the hash keys and values somehow? I realize the 'right' way is probably just to use the hash as it is, but I really like having $foo = "bar" to work with.
      Any help greatly appreciated.
      TIA
      jg
      _____________________________________________________
      It's not my tree.
        First question: no, the hash was not anonymous, you named it $sr - it is, however, a hash reference.

        Second question: don't do that!!

        The right way is indeed to use a hash, but here is the wrong way just for you: (and notice no use strict)

        use CGI; # don't use ':standard' unless you don't want OO CGI.pm my $q = CGI->new(); $$_ = $q->param($_) for $q->param(); print "$foo and $bar\n";
        This works when run like so:
        $ perl foo.cgi "foo=baz&bar=qux"
        baz and qux
        
        But using symbolic references is bad practice. Please, for your sanity down the road, stick with hashes to store 'dynamic' variable names.

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)