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

Hello Guys

maybe it is impossible or at least i have no clue how to solve this myself :)

I am trying to load a config file in yaml style (which works). However in the yaml file are some constants which i want to get "translated" by Net::SNMP.

I tried it with an has before which worked a treat

my $snmpConf = ( '.1.3.6.1.4.1.1918.2.13.10.30.80.1.110.2' => { type => INTEGER + , 'value' => '1' }, );

This is the line in the yaml file

.1.3.6.1.4.1.1918.2.13.10.30.80.1.110.2: type: INTEGER value: "1"
Reading the values via the oid by snmpget is no problem at all.

However afaik Net::SNMP translates the bareword INTEGER into an integer value... right?
But when i now want to set an variable via snmp it needs to know the type and then it seems like it doesn't get translated to the value anymore.

foreach $oid ( keys $snmpConf) { $snmp_session->set_request (-varbindlist => [$oid, $snmpConf->{$oid}{t +ype}, $snmpConf->{$oid}{value}]); ....
Results in
ERROR: The ASN.1 type "OCTET_STRING" is unknown

So my question is, how can i, if at all, treat $snmpConf->{$oid}{type} like an Constant so it gets translated to the integer value?

tia :)
Mike

Replies are listed 'Best First'.
Re: Use hash variable as constant
by kcott (Archbishop) on Jun 27, 2014 at 15:50 UTC
Re: Use hash variable as constant
by Virus2500 (Initiate) on Jun 27, 2014 at 12:37 UTC
    Ok i think i figured it out after reading a lot more.
    Please correct me if i am wrong.
    Constants are like subroutines?!
    So even though i can't access the value directly i can get them via eval.

    so i added:
    my $typeValue = eval("$type()");
    and now i am using $typeValue rather than $type itself.

    Is my understanding of this issue correct?

    thanks Mike
      "Is my understanding of this issue correct?"

      Short answer: I suspect not.

      Longer answer:

      Are you talking about the constant pragma? If so, read the doco: you seem to have some (partly) correct, as well as wrong, ideas about this.

      If you're referring to something else, please specify.

      -- Ken

Re: Use hash variable as constant
by RonW (Parson) on Jun 27, 2014 at 16:34 UTC

    Update: No. Assuming the YML file had INTEGER in it, then $snmpConf->{$oid}{type} will just be the string "INTEGER". I think your idea to eval it should work.

    Maybe:

    ($snmpConf->{$oid}{type} + 0);
      Wow thanks to all for your great support.

      I will dig trough the documentation and source a little bit more.
      I kinda a thought this might be something "well known" and i am just not aware of it :)

      However the method i posted with eval is working a treat (if anyone is having the same issue in the future.)

      thanks again and br Mike