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

I know OO style of CGI programming is more accepted and is usually the way that examples of the CGI.pm are done. I have a lot of code that I maintain that is all written in the function-oriented style. Honestly, I didn't even realize that was what it was called until recently. I'm reading a lot of OO style examples that make sense, but I want to understand how to answer my question in function-oriented style.

I need to read in the parameters from a form and return another page based on the values of each parameter. The form elements are all radio buttons, so I don't have to worry about the multivalued null character issue here.

I'm unsure about importing CGI.pm routines into the script namespace and then how to use the param() method. I read something about using use CGI ':cgi-lib'; instead of use CGI qw(:standard) to be able to get the parameter list as a hash using Vars, but I don't understand that.

Something like this?

#!/usr/bin/perl -w # tools.cgi use CGI qw(:standard :cgi); use CGI::Carp qw(fatalsToBrowser); require 'sri.conf'; %params = Vars; #getting the hash with the parameter list as the keys # Print HTTP header and opening HTML tags. print "Content-type: text/html\n\n"; # Print the top of the page &print_file($action_header_file); #this while is just an attempt to get the variable names and values to + print for my understanding, to be replace by other code using these +values while (my ($variable_name, $variable_value ) = each %params) { print "Variable name = $variable_name/n Variable value = $variable +_value\n\n"; } # Print the bottom of the page &print_file($action_footer_file); sub print_file { my($file_name) = @_; #name subroutine variables open(HEADER,"$file_name"); print <HEADER>; close HEADER; }

TIA
~W

Replies are listed 'Best First'.
Re: Function-oriented style CGI accessing form parameters
by Your Mother (Archbishop) on Aug 03, 2005 at 17:41 UTC

    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.
      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.)

Re: Function-oriented style CGI accessing form parameters
by jhourcle (Prior) on Aug 03, 2005 at 17:56 UTC
    I read something about using use CGI ':cgi-lib'; instead of use CGI qw(:standard) to be able to get the parameter list as a hash using Vars, but I don't understand that.

    But in your code, you have:

    use CGI qw(:standard :cgi);

    Try changing it to:

    use CGI ':cgi-lib';

    Update: I should probably explain what this actually does... see use. Basically, arguments after the package name to 'use' are passed in as arguments to import ... items starting with a colon are calls to load specific sets of functions, as opposed to a single function name. You may want to also read up on Exporter, although CGI doesn't actually use it.

      Thanks for the info on use. I had had use CGI ':cgi-lib'; in my code before I posed the question, but it wasn't working so I went back to the code I was used to.
Re: Function-oriented style CGI accessing form parameters
by kwaping (Priest) on Aug 03, 2005 at 16:44 UTC
    Update: Misunderstood the question, sorry! I'll leave this code here anyway, just for kicks.


    Here's a simple example to get you started.
    #!/usr/bin/perl # the usual suspects use warnings; use strict; # CPAN modules use CGI; # create objects my $cgi = CGI->new; # read in form data my %form_data = $cgi->Vars; # output print $form_data{'parameter_name_here'};
    Have fun!
      Thanks, but isn't that in OO style? Maybe I'm really confused, but I was under the impression that OO used the -> while function-oriented (which I am trying to understand) does not.

      Aside: I do not use strict; in my code because no matter what I've tried, I can't get anything to run with that in my scripts on the server set up I have. My unix admins tell me something is wrong with the perl install, but they don't know how to fix it... But I'm soon to get a new web server (woohoo, no more netscape server) with fresh installs of everything (woohoo, no more perl 5.004) so hopefully that will get fixed.