For a POST request, once you construct the first CGI.pm object, it reads the POST data from stdin. So if you construct a second CGI.pm object (using the default constructor), your program will probbaly hang while the second CGI.pm object attempts to read the post data from stdin.

For a GET, I beleive you can construct multiple CGI.pm objects without this blocking issue.

You can use an alternate constructor for CGI.pm to create more than one without worrying about blocking by passing an empty anon hash:

use strict; use warnings; use CGI; ... # first one, processes POST data if present my $q = new CGI(); ... # passing the anon hash causes this CGI.pm object # to read its params from the hash instead of # the environment my $q2 = new CGI({});
Why you would do it is another issue. I have found that a second, temporary CGI.pm object is useful for doing things like generating URLs that need lots of query string data for either HTTP redirects, or for creating links in generated HTML. CGI.pm is good at escaping and decoding the data, so why not make use of it?
use strict; use warnings; use CGI; ... my $q = new CGI(); ... if( $someRedirectCondition ) { my $q2 = new CGI({}); $q2->param('copy' => $q->param('copy') ); $q2->param('foo' => 'bar'); $q2->param('qux' => 'baz'); ... my $uri = "http://some.domain/and/a/path?" . $q2->query_string(); print $q2->redirect( -uri => $uri ); exit 0; }

In reply to Re: Are there reasons to instantiate the CGI object more than once? by mortis
in thread Are there reasons to instantiate the CGI object more than once? by jerrygarciuh

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.