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

Argument problem when using a perl script

by Jeylox (Initiate)
on Apr 12, 2016 at 13:50 UTC ( [id://1160223]=perlquestion: print w/replies, xml ) Need Help??

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

Hi i'm seeking the perl wisdom for a short problem A nagios will launch my script every hour and sometime i will need the where but not always. My problem is that when i don't use a condition, the script use a SQL request with a where empty but what i want is not to use a where when i launch the script without the argument.
my $oracle_connector = Monitoring::Plugin->new( shortname => 'File attente STATUS =', usage => 'Usage : %s [ -t|--table=<threshold> ] [ -k|--condition=<threshold +> ] [ -c|--critical=<threshold> ] [ -w|--warning=<threshold> ]', ); $oracle_connector->add_arg( spec => 'condition|k=s', help => 'Condition for the request', label => 'REQUEST', required => 0, #Argument not required ); my $condition = $oracle_connector->opts->condition; my $where = "WHERE status = '$condition'"; my $sth = $db->prepare("SELECT COUNT (*) from CONNECTOR.$table $where +");

Replies are listed 'Best First'.
Re: Argument problem when using a perl script
by halfcountplus (Hermit) on Apr 12, 2016 at 14:11 UTC

    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".

      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: perlquestion [id://1160223]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-24 08:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found