in reply to Function-oriented style CGI accessing form parameters

The only trick is letting perl know where the Vars() is coming from. As kwaping, showed, you can do that by using the CGI object. You can also import it, as the POD says with ":cgi-lib"

use CGI qw(header start_html Vars pre); use Data::Dumper; print header(), start_html(); my $ref = Vars(); print pre( Dumper( $ref ) ); # or just fully qualify it + use CGI ":standard"; # ... + my %params = CGI::Vars(); print pre( Dumper( \%params ) ); # or what the POD suggests use CGI qw(:standard :cgi-lib); print pre( Dumper( [ Vars() ] ) );
update: ins'd a bit and added the ":cgi-lib" example too.

Replies are listed 'Best First'.
Re^2: Function-oriented style CGI accessing form parameters
by hmbscully (Scribe) on Aug 03, 2005 at 18:25 UTC
    Thanks. The Data::Dumper bits threw me at first, but since I don't have that module, I'll just ignore those lines.

    I assume (dangerous, I know), that I can print the key value pairs with:

    while (my ($variable_name, $variable_value ) = each %params) { print "Variable name = $variable_name/n Variable value = $variable +_value\n\n"; }

    Yes?

    UPDATE
    my test/understanding code now looks like this:

    #!/usr/bin/perl -wT # tools.cgi use strict; use CGI qw(header start_html Vars pre); use CGI::Carp qw(fatalsToBrowser); print header(); print "These are the values submitted:\n\n"; my %params = Vars(); my $count = keys %params; print $count; while (my ($variable_name, $variable_value ) = each %params) { print "Variable name = $variable_name/n Variable value = $variable +_value\n\n"; }
    I tried
    use CGI qw(:standard :cgi-lib); and
    use CGI ":standard"; and both of those threw errors and wouldn't execute. But this third option will run, but gives an output of
    These are the values submitted: 0
    even though I know there are values. I passed and printed them successfully with
    foreach $sri_variable( $cgi->param){ print "$sri_variable:\n"; foreach $variable_value( $cgi->param($sri_variable) ){ print " $variable_value\n"; } }

      Sounds like you're doing something weird in there; you should probably check your error logs or do the full use CGI::Carp qw(fatalsToBrowser warningsToBrowser);. All those examples should work fine.

      Another thing you might consider is the Vars() idiom is a little strange today. There is no reason to avoid the param() function in favor of it. Your code above becomes (with a little HTML formatting help):

      print '<ul>'; for my $sri_variable( param() ) { print '<li>', $sri_variable; print '<ul>'; for my $variable_value( param($sri_variable) ){ print li($variable_value); } print '</ul></li>'; } print '</ul>';

      Make sure to use strict; use warnings; too. It looks like you're not. (update: fixed bad HTML nesting.)

        I usually use it, but it had gotten left out this time, so I put the use CGI::Carp qw(fatalsToBrowser warningsToBrowser); in and ended up with "begin failed--compilation aborted at /actapps/suitespot/cgi-bin/sri/tools.cgi line 9."

        Line 9 is the CGI::Carp line.

        LOL.
        I just can't win today.