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

Hi,

I have been reading up on lexical and package variables but this branch is just out of my reach.

I am submitting a basic form to a cgi - the problem is the perl cgi is outdated and i dont know how to translate the code into lexical. The refernced $key values keep throwing a undefined global at $Form data so i threw a few 'my's at it. now the erorrs are syntax at "$FORM_DATA{" around lines 60. Where might I go from here?

#! /usr/bin/perl -Tw use strict; use warnings; use CGI::Carp qw(fatalsToBrowser), qw(warningsToBrowser); my ($webmaster, $user); our($FORM_DATA, $change_text); $webmaster = "http\:\/\/www.mysite.com\/"; &parseform(*change_text); print "Content-type: text/plain\r\n\r\n"; # $user = $change_text{'user'}; print "\$user:\n$user\n\n"; exit(0); sub parseform { local (*FORM_DATA) = @_; my ( $reqmet, $qst, $keyval, $key, $val, $keyvalpears); $reqmet = $ENV{'REQUEST_METHOD'}; if ($reqmet eq "GET") { print "This type of request is not accepted please update via +the authenticated edit form at $webmaster"; } elsif($reqmet eq "POST"){ read (STDIN, $qst, $ENV{'CONTENT_LENGTH'}); } else { &reterr (500, "Server Error", "Server uses unsupported method"); } print "\$qst:\n$qst\n\n"; @keyvalpears = split (/&/, $qst); print "\@keyvalpears:\n@keyvalpears\n\n"; foreach $keyval (@keyvalpears) { ($key, $val) = split (/=/, $keyval); $val =~ tr/+/ /; $val =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg; # if defined no need to use anymore re my? if(defined($FORM_DATA{$key})) { $FORM_DATA{$key} = join ("\0", $FORM_DATA{$key}, $val); }else{ $FORM_DATA{$key} = $val; } print "\$FORM_DATA val :\n$FORM_DATA{$val}\n\n"; } } sub reterr { my ($status, $keyword, $message) = @_; print "Content-type: text/html", "\r\n\r\n"; print "Status: ", $status, " ", $keyword, "\n\n"; print <<End_of_Error; <HTML> <HEAD> <TITLE>CGI Program - Unexpected Error</TITLE> </HEAD> <BODY> <H1>$keyword</H1> <HR>$message<HR> Please contact $webmaster for more information. </BODY> </HTML> End_of_Error exit(1); }

Replies are listed 'Best First'.
Re: globbed variable in parse cgi form - beyond me
by ikegami (Patriarch) on Jun 27, 2011 at 00:05 UTC
    Don't recreate CGI!

      thanks ikegami,

      it was not my intention to recreate cgi - i was just following the instructions as it were.

      Basically I need to be doing something along these lines then(see code), although the input element names are being returned where the values are not. yet...

      #! /usr/bin/perl -w use strict; use warnings; use CGI; use CGI::Carp qw(fatalsToBrowser), qw(warningsToBrowser); my ($webmaster, $user, $name, @names, $value, @values, $query); $query = CGI->new; $webmaster = "http\:\/\/www.mysite.com\/"; @names= $query->param; foreach $name(@names){ $value = $query->param('$name'); push(@values, $value); } print "Content-type: text/plain\r\n\r\n"; print "names @names \n\n"; print "values: @values \n\n"; exit(0);

        '$name' creates the 5 character string "$","n","a","m","e". Double-quoted strings interpolate, but no need for that either. Use ->param($name)

        May I also recommend that you don't declare your variables until you need them? No need to make their scope larger than needed.

        #! /usr/bin/perl -w use strict; use warnings; use CGI; use CGI::Carp qw( fatalsToBrowser warningsToBrowser ); my $query = CGI->new; my $webmaster = "http://www.mysite.com/"; my @names = $query->param; my @values; foreach my $name (@names) { my $value = $query->param($name); push(@values, $value); } print "Content-Type: text/plain\r\n\r\n"; print "names @names\n\n"; print "values: @values\n\n";
Re: globbed variable in parse cgi form - beyond me
by Anonymous Monk on Jun 27, 2011 at 01:11 UTC

      How does mentioning Catalyst help somebody who is struggling with their first CGI program, and has problems with when variables interpolate and when not? Catalyst may or may not be a fantastic framework, but it is at least a few steps away for somebody just starting to learn Perl.

        How does mentioning Catalyst help somebody who is struggling with their first CGI program

        FWIW, when I posted this "SEE ALSO" section, it was only apparent the OP did not know CGI, not apparent the OP was just starting to learn perl

        Mentioning Catalyst, including offering a walkthrough, demonstrates how Catalyst makes life easier