is the same asif ($_ eq 'A' || $_ eq 'B' || $_ eq 'C') { }
is the same asif ({A =>1, B =>1, C =>1}->{$_}) { }
if (/^(A|B|C)$/) { }
my $chr = $_; grep { $chr eq $_ } qw( a b c );
Benchmarking, it seems the anonymous hash method is slightly faster than ||, but slower than /^()$/, which is also more terse and probably uses less memory. Regex wins again!
Editor's note: $ should be \z in regex as pointed out by ysth and ikegami. Also see ikegami's response for regex improvement and benchmark contestment.
Edit: Added diotalevi's grep suggestion for extra TMTOWTDI fun.
|
|---|