On Mon, Dec 17, 2001 at 11:36:30AM +1000, Lorne wrote:
> I don't understand this line..
> 
> $session = Net::SNMP->session(-hostname => "10.0.0.100", -community =>
> "public");

The parameters are a hash: -hostname=>'10.0.0.100' is the same as:
'-hostname','10.0.0.100'... 

so you could pass:
$session = Net::SNMP->session('-hostname','10.0.0.100','-community','public');

and SNMP->session would be none the wiser, however => makes the
parameters more legible to users.

The variables are read into the array  @_ as soon as they get into a
function. then to render it useful and allow the params to be passed in
any order, it'll do something like this:

%_=@_;

now parameters can be accessed like this =
print "hostname: $_{-hostname}\n"; # NB " used here since I interpolate.

> Particularly, the way data is presented to the function..
> 
> I.E : -data => "text"

an advantage of => is that data to the left of it is wrapped in quotes, 
so you don't have to.

The - prefix means it's a private variable, IIRC. meaning people who
feel the need to alter this value after creating the object have been
given due warning that the Author is no longer responsible for your
actions, if you need to alter these values do it through the (Mutator)
methods provided.

> Also, why the use of the "shift" as in:
> (from the original example)
> 
>    ($session, $error) = Net::SNMP->session(
>       -hostname  => shift || '10.0.0.100',
>       -community => shift || 'public',
>       -port      => shift || 161
>    );

Using || in such examples is a bit of a gamble, due to the low
precedence of , and ||. 

What you appear to be doing is saying you should have 3 variables,
in the absence of 1 you will not have any of the following either.

So the permutations you expect are:

hostname, community, port
hostname, community
hostname
none

perhaps better to establish your parameters before calling:

# Set up defaults.
my %params= ( -hostname=>'10.0.0.100', -community=>'public', -port=>'161');

# Based on params repopulate the hash.
for (0..$#_){ # @_ is the array of passed params or @ARGV for commandlines
        $params{ (qw/ -hostname -community -port )[$_] } = $_[$_];
}

If you are running this from the commandline, consider using GetOptions,
if not consider using a hash as SNMP does, it's good practice.

-- 
 Frank Booth - Consultant
Parasol Solutions Limited.
(www.parasolsolutions.com)

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to