Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

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

by jerrygarciuh (Curate)
on Dec 14, 2001 at 21:15 UTC ( [id://132000]=perlquestion: print w/replies, xml ) Need Help??

jerrygarciuh has asked for the wisdom of the Perl Monks concerning the following question:

I am incorporating a file upload sub that I just completed into a larger script. My question is, is there ever a reason to do a second my $q = CGI->new(); ?
TIA
jg
_____________________________________________________
If it gets a little bit out of hand sometimes, don't let it fool you into thinkin' you don't care.TvZ

Replies are listed 'Best First'.
Re: Are there reasons to instantiate the CGI object more than once?
by mortis (Pilgrim) on Dec 14, 2001 at 22:45 UTC
    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; }
Re: Are there reasons to instantiate the CGI object more than once?
by clintp (Curate) on Dec 15, 2001 at 01:23 UTC
    Grepping through our library of code I found a couple of examples, one's listed below. The authentication sequence for every CGI script sent the CGI query off to a module that did the validation and returned the authentication object:
    my $q=new CGI; ..... $token=valid_login($q) redirect_to_login($q) unless $token;
    During one of our programs we needed to masquerade as another user temporarily, so we created a fake CGI object complete with cookies in order to get something from the authentication object:
    my $fakeq=new CGI; $fakeq->{'.cookies'}->{ID1}=$cookieobj1; $fakeq->{'.cookies'}->{ID2}=$cookieobj2; ..... $ftokenobj=valid_login($fakeq) die "Should not happen!" unless $ftokenobj;
    Yes, yes we violated encapsulation by shoving in the cookies directly (couldn't think of a better way at the time) but it did what we wanted: we could re-use valid_login's code without having to alter the interface at all.
Re: Are there reasons to instantiate the CGI object more than once?
by bmccoy (Beadle) on Dec 14, 2001 at 23:50 UTC
    Sure -- you can create another object that takes as its params a file -- you get a pre-loaded, stateful CGI object.

    In mod_perl, in some cases you need to create lexically scoped CGI objects rather than a global one (or pass the CGI object into a sub as a reference).

    -- Brett

    Go not to the Elves for counsel, for they will say both no and yes

Re: Are there reasons to instantiate the CGI object more than once?
by drifter (Scribe) on Dec 14, 2001 at 22:14 UTC
    No, you can use the same object for it. You can create another but afaik there is no need for it.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-04-25 08:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found