in reply to Access an imported constant via a variable that holds the name of it

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.

Replies are listed 'Best First'.
Re^2: Access an imported constant via a variable that holds the name of it
by brotherandy (Initiate) on Apr 08, 2005 at 17:23 UTC
    Excellent, thanks ikegami!