Multiple CGI objects

brian_d_foy on 2003-01-02T08:51:31

I got out of CGI programming a long time ago, but I have a new use for CGI.pm---creating query strings. I could probably do this with a different module that is not as heavyweight as CGI.pm, but I would have to think extra to do that.

I remember several discussions a long time ago, in a newsgroup far, far away, about why anyone would ever need more than one CGI object in a program at a time. Well, in the program I needed today, I had nine of them. Yes, that object-oriented interface is there for a reason.

But, since I really just want the query string, I combine my latest, favorite technique with an older favorite technique---the do {} statement. I scope the CGI object to the do block and extract the query string at the end of the block, making it the return value that shows up in $query. Everything is wrapped up in a nice, little package.

my $query = do {
	my $cgi = CGI->new();

$cgi->param( 'TopOne', 1 ); $cgi->param( 'userid', 'comdog' ); $cgi->param( 'password', 'nope!' ); $cgi->param( 'Attempt', 0 );

$cgi->query_string; };