Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

using return to create a module object

by c (Hermit)
on Jul 06, 2002 at 05:20 UTC ( [id://179806]=perlquestion: print w/replies, xml ) Need Help??

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

i am trying to create an object for a module through the following method.

my %args = ( hostname => "router", version = "1" ); my $s = &create_snmp(\%args); sub create_snmp { my $args = shift; my %options; $options{hostname} = $args->{hostname}; $options{version} = $args->{version}; if ($args->{version} == "1") { $options{community} = "public"; } else { $options{username} = "v3user"; $options{password} = "v3pass"; } my $new_session = Net::SNMP->session(\%options); return $new_session; }

This fails and the object is not set. Is this possible and my syntax incorrect, or do I need to look for other ways to correctly return the value?

thanks -c

Replies are listed 'Best First'.
Re: using return to create a module object
by dws (Chancellor) on Jul 06, 2002 at 05:53 UTC
    You have an easy-to-miss typo in the script.   my %args = ( hostname => "router", version = "1" ); should be   my %args = ( hostname => "router", version => "1" ); The typo has the effect of propogating an undefined value into the options being passed to NET::SNMP->session().

    Compiling with -w would have kicked this out right away.

    Hold on there... This won't run at all. You must have made a typo when typing in the code. Please paste in the code you're running, rather than retyping it.

      That shows that trying to retype code to simplify it shouldnt be done after 2am. This time I am pasting the code as it is.
      #!/usr/bin/perl -w use strict; use Net::SNMP; my @devices = qw(2500 7200 7500 12000); my $retries = "1"; my $wait = "5.0"; my $mtu = "1500"; my $version = "1"; my $debug = "0"; my $community = "public"; my $snmp_username = ""; ## v3 my $snmp_authkey = ""; ## v3 my $snmp_authpassword = ""; ## v3 my $snmp_authprotocol = ""; ## v3 my %oid = ( version => ".1.3.6.1.2.1.1.1.0", ); &execute("run","tftp"); sub execute { ## pull source and destination of files my %args = ( s => $_[0], d => $_[1], ); foreach (@devices) { chomp $_; $args{h} = $_; &action(\%args); } } sub action { my $args = shift; ## set up initial parameters for this nodes snmp session(s) my $s = &create_snmp($args); ## grab the ios major revision number my $ios_version = $s->get_request ($oid{version}); ## grab an error if it exists $args->{e} = $s->error; ## close the snmp session $s->close; print "$args->{h} has IOS:\n\n$ios_version\n\n"; } sub create_snmp { ## get hostname to be used into local variable my $args = shift; ## create the hash of version independent options to pass to the met +hod my %snmp_options = ( hostname => "$args->{h}", retries => "$retries", timeout => "$wait", mtu => "$mtu", version => "$snmp_version", debug => "$debug", ); ## declare our variable to be set and returned my $s; ## test for snmpv3 usage if ( $snmp_version eq "snmpv3" ) { $snmp_options{username} = "$snmp_username"; $snmp_options{authkey} = "$snmp_authkey" if ($snmp_authkey); $snmp_options{authpassword} = "$snmp_authpassword" if ($snmp_authpassword); if ($snmp_authprotocol) { $snmp_options{authprotocol} = "$snmp_authprotocol"; $snmp_options{privkey} = "$snmp_authkey"; $snmp_options{privpassword} = "$snmp_authpassword"; } ## initiate session with v3 values $s = Net::SNMP->session(\%snmp_options); } else { ## set the v1/2c specific session traits $snmp_options{community} = $community; ## initiate session with v1/2c values $s= Net::SNMP->session(\%snmp_options); } ## provide our new session information return $s; ## end create_snmp() }

      Thanks for looking into this. -c

        I don't see anything obviously wrong, though it looks odd to be using a device name as a host name. Is that really what you intend?

        You might gain some insight by trying to come up with a small example that demonstrates the problem. Something like

        use strict; use Net::SNMP; my $version = ".1.3.6.1.2.1.1.1.0"; my %snmp_options = ( ... }; $s = NET::SNMP->session(\%snmp_options); print $s->get_request($version); $s->close();
        and then explicitly fill in %snmp_options.

        This approach strips out extraneous detail, and should help you isolate the problem quickly.

        You should get out of the habit of unnecessarily quoting variables - it is bad habit. Here are a couple of corrections:
        my %snmp_options = ( hostname => $args->{h}, retries => $retries, # etc ); # and $snmp_options{username} = $snmp_username;
        Sure, using quotes works for those examples, but it is unnecessary and will one day lead to something like:
        package My::CGI; use strict; use CGI; sub new { my $class = shift; my $foo = 'bar'; my $q = CGI->new(); my $self = { foo => "$foo", cgi => "$q", }; return bless $self, $class; } package main; use strict; my $q = My::CGI->new(); print $q->{foo}, "\n"; print $q->{cgi}->a({href=>'http://foo.bar'},'some link'),"\n"; __END__ yields: bar Can't locate object method "a" via package "CGI=HASH(0x80f8a20)" at ./foo.pl line 26.
        Whoops! What happened? You literalized a reference. Try this code out, then go back and remove the unnecessary quotes inside the $self hash ref.

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)
        
Re: using return to create a module object
by fokat (Deacon) on Jul 06, 2002 at 17:41 UTC
    Does $new_session have some value before the return()?. That bit looks ok to me.

    Regards.

RESOLUTION->Re: using return to create a module object
by c (Hermit) on Jul 07, 2002 at 00:38 UTC
    A lot of learning came out of this thread for me, and I appreciate everyone that has contributed through posts and through /msg. I found that the passing the hash within the subroutine directly corrected the problem. So rather than a reference

    my $new_session = Net::SNMP->session(\%options);

    just passing the hash itself works

    my $new_session = Net::SNMP->session(%options);

    My next step will be taking out those unnecessary quotes and cleaning up the code with the suggestion I've received.

    thanks -c

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-03-29 12:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found