Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Are there reasons to instantiate the CGI object more than once?

by mortis (Pilgrim)
on Dec 14, 2001 at 22:45 UTC ( [id://132029]=note: print w/replies, xml ) Need Help??


in reply to Are there reasons to instantiate the CGI object more than once?

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://132029]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-04-16 16:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found