Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Argument problem when using a perl script

by halfcountplus (Hermit)
on Apr 12, 2016 at 14:11 UTC ( [id://1160225]=note: print w/replies, xml ) Need Help??


in reply to Argument problem when using a perl script

I'm not familiar with `Monitoring::Plugin` but I presume if you don't use the -k argument, then this:

my $condition = $oracle_connector->opts->condition;

Returns an empty string. In that case you just need to add another condition.

my $where = ""; $where = "WHERE status = '$condition'" if $condition;

This is an atypical use of `if` (with the clauses reversed) but it works; it's the same as:

if ($condition) { $where = "WHERE status = '$condition'" }

Anyway, now `$where` will be empty if `$condition` is. I don't know if the blank space between `CONNECTOR.$table` and `$where` will then adversely affect your query; if so you need to put that space into the `if` block instead:

$where = " WHERE status = '$condition'"

Notice the space before the "WHERE".

Replies are listed 'Best First'.
Re^2: Argument problem when using a perl script
by graff (Chancellor) on Apr 12, 2016 at 22:28 UTC
    Note that if a valid/actionable return from  $oracle_connector->opts->condition could be the string "0", then we wouldn't want this to evaluate to false (and skip the "where" clause). Also, in case the return from that function call might contain an apostrophe, we'd want to use a placeholder for the value in the SQL statement:
    my $condition = $oracle_connector->opts->condition; my @bind_values = (); my $where = ""; if ( $condition =~ /\S/ ) { push @bind_values, $condition; $where = " WHERE status = ?"; } my $sth = $db->prepare("SELECT COUNT(*) from CONNECTOR.$table$where"); $sth->execute( @bind_values ); # works as intended if @bind_values is + empty
    (updated code to add missing sigil)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2024-04-18 09:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found