Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: 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 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

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (13)
As of 2016-07-25 21:02 GMT
Find Nodes?
    Voting Booth?
    What is your favorite alternate name for a (specific) keyboard key?

    Results (227 votes). Check out past polls.