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 portid

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


In reply to Optionally include named arguments when constructing a new object by nice_iguana

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.