Comparing Multiple Strings In Perl

后端 未结 5 1657
广开言路
广开言路 2021-01-17 23:15

I have my code like this:

if ( $var eq \"str1\" || $var eq \"str2\" || $var eq \"str3\" )
{
...
}

Is there anyways to optimize this. I want

5条回答
  •  滥情空心
    2021-01-17 23:58

    In Perl 5.10 or better:

    if ($var ~~ [qw( str1 str2 str3 )]) { ...} 
    

    The ~~ operator does a smart match between its arguments.

提交回复
热议问题