Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <ol> <li><p>Use <code>List::MoreUtils qw{any}</code></p> <pre><code>use List::MoreUtils qw{any}; if ( any { $var eq $_ } 'str1', 'str2', 'str3' ) { ... } </code></pre> <p>This might be faster than using <code>grep</code> because <code>List::MoreUtils::any</code> finishes early when it finds a match whereas <code>grep</code> might build a complete list of matches. I say 'might' because Perl could conceivably optimise <code>if (grep ...</code>. It might not. But <code>List::MoreUtils::any</code> does finish early, and it's more descriptive than the <code>if (grep ...</code> idiom.</p></li> <li><p>Make a hash that has keys of all the strings you want to match</p> <pre><code>my %matcher; @matcher{qw{str1 str2 str3}} = (); if ( exists $matcher{$var} ) { ... } </code></pre> <p>This has the disadvantage of a set-up time and the cost of the memory used for the hash, but the advantage is that the match time is more like O(log N). So if you have lots of different values of <code>$var</code> that you want to test then it could be faster overall.</p></li> <li><p>Make a regex that matches all of your strings</p> <pre><code>if ( $var =~ m/^str[123]$/so ) { ... } </code></pre> <p>OK, so this is fine if your strings are literally <code>qw{str1 str2 str3}</code>, but what if it is a list of arbitrary strings?</p> <p>You could use <a href="https://metacpan.org/module/Regexp%3a%3aAssemble" rel="nofollow">Regexp::Assemble</a> to fuse together a list of regexps into a single optimised regexp.</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload