Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CGI->param() vs. $ENV{'QUERY_STRING'}, how to handle them in one file?

Status
Not open for further replies.

cyan01

Programmer
Joined
Mar 13, 2002
Messages
143
Location
US
Hi, CGI experts,

I have two pieces of CGI perl code left from 2 ex-coworkers. One piece of code used %ENV to handle all cgi variables and the other used CGI.pm. Now, I have to merge them together but have run into some deadlock. It seems to me that

1) if I used CGI.pm, then I could not use %ENV;
and
2) if I used %ENV, then I could not use CGI.pm.

Could anyone here confirm this and explain why? Or did I simply get a wrong conclusion, which means %ENV and CGI.pm CAN work together in the same piece of code?

Thank you for reading this and any help is highly appreciated!
 
%ENV is the environment hash, CGI.pm reads from this. You should still be able to explicitly read from $ENV{'VAR_NAME'}, but if you're using CGI.pm, you can use param to read from the QUERY_STRING, or CONTENT_LENGTH

HTH
--Paul

Nancy Griffith - songstress extraordinaire,
and composer of the snipers anthem "From a distance ...
 
Thank you, Paul. Sorry I have not replied to your post earlier.

I understand that CGI.pm parses %ENV first. But please take a look at the following sample code:

HTML code:

Code:
<HTML>
<body>
<center>
<FORM METHOD="POST" ACTION="/cgi-bin/cyantest.pl">
<table>
<tr> <th align=left>Enter a string</th> </tr>
<tr> <td><INPUT TYPE="text" NAME="input"></td> </tr>
<tr><td> <INPUT TYPE="submit" NAME="submit" VALUE="Go"></FORM> </td></tr>
</table>
</center>
</BODY>
</HTML>

PERL code -- cyantest.pl
Code:
#!/usr/local/bin/perl

use CGI;

MAIN:
{
  print "Content-type: text/html\n\n";

  ######################################################################################
  # Note:
  # 1. if parse_form() gets called first, then $cgi->param('input') return a NULL string
  # 2. if "$cgi = new CGI" gets called first, then %FORM can not be set in parse_form()
  ######################################################################################
  parse_form();
  $cgi = new CGI;
  my $action = $in{'action'};

  my $inputENV = $FORM{'input'};
  print "<h3>\$inputENV = #-$inputENV-#</h3>\n";

  my $inputCGIpm = $cgi->param('input');
  print "<h3>\$inputCGIpm = #-$inputCGIpm-#</h3>\n";
}

sub parse_form()
{
  # Get the input
  my $buffer;
  read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
  # Split the name-value pairs
  @pairs = split(/&/, $buffer);

  foreach $pair (@pairs)
  {
    ($name, $value) = split(/=/, $pair);
    $FORM{$name} = $value;
  } # end of foreach $pair (@pairs)
} # end of parse_form()

As you can see here, in the example above, since parse_form() is called before "$cgi = new CGI", $cgi->param('input') returns a NULL value. But if you switched the calling order, then parse_form would not work!

Could you or anyone in this forum give me a help?
 
You should be able to replace that horrible parse_form subroutine quite easily, using CGI.pm's cgi-lib-compatible parsing routine (Look for "Fetching the Parameter List as a Hash" in the CGI.pm docs):
Code:
# remove this line - parse_form(); 
%FORM = $cgi->Vars;
That shouldn't interfere with the param() method and vice-versa.
 
The problem I am having is that I have two pieces of perl CGI code, one piece is using parse_form while the other is using CGI.pm. And worse, I am not supposed to make any changes in parse_form, I can only make changes in the codes that call parse_form.

I wonder if there is a way for me to keep both parse_form and $cgi->param() in one source code?

Thanks!
 
Technically, with what I've posted, you aren't changing parse_form at all, you're changing the "codes that call parse_form" so that they don't call parse_form anymore ;)

Seriously, though. You're much better off not using a parse_form sub like that. Here's a few articles on why - one two
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top