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.


In reply to Re: Access an imported constant via a variable that holds the name of it by ikegami
in thread Access an imported constant via a variable that holds the name of it by brotherandy

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.