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


In reply to Re: Multiple CGI Objects w/ Same Name by TGI
in thread Multiple CGI Objects w/ Same Name by LadyD

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.