Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: CGI.pm OO vs. FO

by kutsu (Priest)
on Sep 09, 2005 at 20:41 UTC ( #490747=note: print w/ replies, xml ) Need Help??


in reply to CGI.pm OO vs. FO

I'd use OO when your going to need to call multiple params at different times/different lexical scopes (the creation of a global cgi variable to control when and where you need certain params), something like the code that follows:

use CGI; my $cgi = $cgi->new; sqlstuff(); email_someone(); sub sqlstuff { my @locations = $cgi->param('locations'); my $sql = q(select * from table where server like '%someserver%' and + ); $sql .= sprintf "(%s)", join " or ", map { location='$_'" } @locatio +ns; #thanks to [blokhead] for the above #see below for explaination #if needed in existing %IN hash (with assuming other fields) $IN{location} = [ @locations ]; .... do stuff ... } sub email_someone { my $someone = $cgi->params('someone'); .... do emailing stuff here ... }

There are other ways to do this, but having seen your whole script this looks like a good stepping point.

As for your actually code Vars returns a hash while param returns an array (or scalar depending on how it's called) so if you use $cgi->Vars you should use $where .= "$_" for values %IN; (if you have multiple locations from checkboxes you want param as Vars will combine locations into a single string)

Update: Replied to CB questions and sql creation explaination:
sprintf "(%s)" : meaning insert string between ()
join " or " : returns a joined string (using delimiter "\sor\s" for inserting
map { location ='$_'"} @locations; : sets location='$location[0]..[$#location] as the array for join to join

"Cogito cogito ergo cogito sum - I think that I think, therefore I think that I am." Ambrose Bierce


Comment on Re: CGI.pm OO vs. FO
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2014-12-28 17:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (182 votes), past polls