nice_iguana has asked for the wisdom of the Perl Monks concerning the following question:
Hello, I am new to Perl and will be very grateful for your patience and assistance.
I am looking for some advice on how to include named args when constructing a new object only if they were specified on the command line.
I am writing a script that uses the Net::SNMP module and I want to have certain optional args, e.g. privpassword and privprotocol, only included when I create the new Net::SNMP object if the user specified a value for them on the command line other than "" (un-used args will be quoted empty, i.e. "").
The script is meant to be called by another program (it's a Cacti plugin FWIW) with position-dependent command line arguments. Here is an example of how my script would be used:
check_snmp.pl 10.0.0.9 admin mypassword https public 2c 161 2500 10 myauthuser myauthpassword sha "" "" "" query portidMy script looks something like this (please note that $user, $pass and $protocol are irrelevant to this question):
use strict; use warnings; use Net::SNMP qw(:snmp); my $address = $ARGV[0]; my $user = $ARGV[1]; my $pass = $ARGV[2]; my $protocol = lc $ARGV[3]; my $snmp_community = $ARGV[4]; my $snmp_version = $ARGV[5]; my $snmp_port = $ARGV[6]; my $snmp_timeout = $ARGV[7]; my $max_oids = $ARGV[8]; my $snmp_username = $ARGV[9]; my $snmp_password = $ARGV[10]; my $snmp_auth_protocol = $ARGV[11]; my $snmp_priv_passphrase = $ARGV[12]; my $snmp_priv_protocol = $ARGV[13]; my $snmp_context = $ARGV[14]; my $action = lc $ARGV[15]; my $type = lc $ARGV[16]; my ( $session, $error ) = Net::SNMP->session( hostname => $address, port => $snmp_port, version => "snmpv" . $snmp_version, timeout => $snmp_timeout, debug => $snmp_debug_mask, username => $snmp_username, authpassword => $snmp_password, authprotocol => $snmp_auth_protocol, privpassword => $snmp_priv_passphrase, privprotocol => $snmp_priv_protocol, );
What I would really like to know is if there are any ways of only including the named arguments when they have been given a value other than "" on the command line?
I tried using an inline if statement, which failed, e.g.
my ( $session, $error ) = Net::SNMP->session( ... if ($snmp_priv_passphrase ne "") { privpassword => $snmp_priv_pas +sphrase, } ... }
I tried using the arguments undefined, which also failed, e.g.
if ($snmp_priv_passphrase ne "") { $snmp_priv_passphrase = undef; } my ( $session, $error ) = Net::SNMP->session( ... }
I know I could put the whole code block inside a series of if statements, so that the named arguments are only included when they have been given a value other than "", but I was wondering if there was a better way of doing it?
Thanks for any help.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Optionally include named arguments when constructing a new object
by boftx (Deacon) on Jun 02, 2014 at 04:49 UTC | |
|
Re: Optionally include named arguments when constructing a new object
by tobyink (Canon) on Jun 02, 2014 at 12:46 UTC | |
|
Re: Optionally include named arguments when constructing a new object
by jellisii2 (Hermit) on Jun 02, 2014 at 13:00 UTC | |
|
Re: Optionally include named arguments when constructing a new object
by nice_iguana (Initiate) on Jun 03, 2014 at 02:43 UTC |