in reply to Fastest way to test membership in a constant set
Use a restricted hash and use exists directly, rather than via a subroutine. This will be far faster than any other method.
#! perl -slw use strict; use Hash::Util qw[ lock_hash ]; my %set = map{ $_ => 1 } qw[ age old years ]; lock_hash %set; for my $unknown ( qw[ age sex old young years minutes] ) { print "$unknown ", exists $set{ $unknown } ? 'exists' : ' does not exist'; } $set{ fred } = 1; __END__ c:\test>junk3 age exists sex does not exist old exists young does not exist years exists minutes does not exist Attempt to access disallowed key 'fred' in a restricted hash at c:\tes +t\junk3.pl line 15.
|
|---|