in reply to Multiple CGI Objects w/ Same Name

Let me try to clarify what LadyD is talking about. The script is a state machine style CGI. In some states she needs to get data in and out of a text file.

Simple, I thought, just use Data::Dumper to get strings to write out and then read and eval them to get them back. LadyD decided to read the CGI docs and found the CGI::save method which saves form input to a filehandle. The data can be retrieved when you create a new CGI object by saying $q = new CGI (FILEHANDLE). The problem is that by the time she knows which file to open, she has already had to use info from the CGI object.

I suggested she do something like this:

use CGI; use strict; my $q = new CGI; if ($q->param('state') eq 'foo') { DoFoo($q); } else { ErrorOut('Say what?') } sub DoFoo { my $q = shift; # do some magic to pick filehandle open (FILEHANDLE, "<the_file") or die "Sadness: $!\n"; my $p = new CGI ('FILEHANDLE'); foreach ($p->param()) { # $q->param($_) = $p->param($_) Bad old code # I sure whish I could do that sort of thing, I make this erro +r too often. $q->param( -name=>$_, -values=>$p->param($_) ); } # do more stuff } sub ErrorOut { die shift; }

Please note that this code is totally untested off the top of my head type material. The important bit is in the DoFoo subroutine, especially the for loop.

So, is it bad to have two CGI objects in one script? Should she just use Data::Dumper like everyone else? Should I just butt out?

Updated I fix my bad code. $q->param($_) = $p->param($_) should be $q->param( -name=>$_, -values=>$p->param($_) );


TGI says moo