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;
}
|