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

I'm looking to use an imported constant via a variable that holds it's name. So Net::SNMP exports a constant named INTEGER - I want to be able to use/access that constant via a variable that holds the name of it. With the below code I get
Inside snmpset OID:1.3.6.1.4.1.9.9.68.1.2.2.1.2.22, HOST:1.1.1.1, STRI +NG:mystring, TYPE:INTEGER, VALUE:1 ERROR (1.3.6.1.4.1.9.9.68.1.2.2.1.2.22 1.1.1.1 mystring): Unknown ASN +.1 type [INTEGER].
but it works fine when I call snmpset this way:
snmpset("1.3.6.1.4.1.9.9.68.1.2.2.1.2.22","1.1.1.1","mystring",INTEGER +,1); Outputs: Inside snmpset OID:1.3.6.1.4.1.9.9.68.1.2.2.1.2.22, HOST:1.1.1.1, STRI +NG:mystring, TYPE:2, VALUE:1
But statically putting INTEGER in there stops it from being modular - I'd have to make a function for each variable type I would need to set. How would I pass $bah in such a way that it was actually passing the constant?
#!/usr/bin/perl use Net::SNMP; my $bah = "INTEGER"; snmpset("1.3.6.1.4.1.9.9.68.1.2.2.1.2.22","1.1.1.1","mystring",$bah,1) +; sub snmpset { my $oid = shift || die "No oid passed to snmpset\n"; my $host = shift || die "No host passed to snmpset\n"; my $string = shift || die "No community string passed to snmpset\n"; my $type = shift || die "No value type passed to snmpset\n"; my $value = shift || die "No value passed to snmpset\n"; print "Inside snmpset OID:$oid, HOST:$host, STRING:$string, TYPE:$ty +pe, VALUE:$value\n"; my ($session, $error) = Net::SNMP->session( -timeout => 5, -hostname => $host, -community => $string, -port => shift || 161 ); if (!defined($session)) { printf("ERROR ($oid $host $string): %s.\n", $error); #exit 1; return -1; } my $result = $session->set_request( -varbindlist => [$oid,$type,$value] ); if (!defined($result)) { printf("ERROR ($oid $host $string): %s.\n", $session->error); $session->close; return -1; } $session->close; return $result->{$oid}; }

Replies are listed 'Best First'.
Re: Access an imported constant via a variable that holds the name of it
by ikegami (Patriarch) on Apr 08, 2005 at 16:25 UTC

    How about

    die("Bad type\n") unless Net::SNMP->can($bah); $bah = Net::SNMP->$bah();

    Update: Since the types (and nothing else) are listed in @{$Net::SNMP::Message::EXPORT_TAGS{'types'}}, here are some safer alternatives:

    %is_valid_type = map { $_ => 1 } ( @{$Net::SNMP::Message::EXPORT_TAGS{'types'}} ); die("Bad type\n") unless $is_valid_type{$bah}; $bah = Net::SNMP::Message->$bah();

    or (better suited if you do numerous lookups):

    %type_map = map { $_ => Net::SNMP::Message->$_() } ( @{$Net::SNMP::Message::EXPORT_TAGS{'types'}} ); $bah = $type_map{$bah} or die("Bad type\n");

    or (supports numerical and string types):

    %type_map = map { my $type_num = Net::SNMP::Message->$_(); ( $_ => $_, $_ => $type_num ) } ( @{$Net::SNMP::Message::EXPORT_TAGS{'types'}} ); $bah = $type_map{$bah} or die("Bad type\n");

    None of this is tested.

      Excellent, thanks ikegami!
Re: Access an imported constant via a variable that holds the name of it
by nobull (Friar) on Apr 08, 2005 at 16:46 UTC
    Clearly this can be done with symrefs...
    my $bah = "INTEGER"; snmpset("1.3.6.1.4.1.9.9.68.1.2.2.1.2.22","1.1.1.1","mystring",&$bah,1 +);
    ...but symrefs are nasty things so why not...
    my $bah = INTEGER; snmpset("1.3.6.1.4.1.9.9.68.1.2.2.1.2.22","1.1.1.1","mystring",$bah,1) +;
    ...or...
    my $bah = \&INTEGER; snmpset("1.3.6.1.4.1.9.9.68.1.2.2.1.2.22","1.1.1.1","mystring",&$bah,1 +);
    ...?