equick has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I was wondering if there is a neater way to write
$perm=400 if $file=~/catalina\.properties/ || $file=~/\.key$/ || $file +=/tomcat-users\.xml/;
For example, using grep with something like:
$perm=400 if grep {/$file/}, qw(catalina.properties \.key$ tomcat-user +s.xml);
Thanks for any help.

Replies are listed 'Best First'.
Re: search a regex list
by jwkrahn (Abbot) on Dec 12, 2010 at 10:38 UTC
    $perm = 400 if $file =~ /catalina\.properties|\.key$|tomcat-users\.xml +/;
    $perm = 400 if grep { $file =~ $_ }, qr/catalina.properties/, qr/\.key +$/, qr/tomcat-users.xml/;

      Thanks, that was exactly what I was looking for. (BTW I had to remove the comma after the curly bracket).

      What does qr do?

        equick:

        You can answer questions like this by referring to the documentation, e.g., perldoc perlop. You may want to review perldoc perldoc to see how to use perldoc, and review perldoc perl to find an overview of the basic documentation. There's also a rumor of a site on the internet called Google that can help you locate answers to simple questions like these.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

Re: search a regex list
by AnomalousMonk (Archbishop) on Dec 12, 2010 at 11:17 UTC

    Also:

    >perl -wMstrict -le "my ($patterns) = map qr{$_}xms, join ' | ', qw(catalina\.properties \.key$ tomcat-users\.xml) ; ;; print $patterns; ;; my $string = 'foo.key'; print 'match' if $string =~ $patterns; " (?msx-i:catalina\.properties | \.key$ | tomcat-users\.xml) match

    Update: A small subtlety: The use of the /m regex modifier in the  qr{$_}xms regex in the example above causes the  $ assertion (see Metacharacters) to match before any embedded newline in the string or before the absolute end of the string. If a match only at the absolute end of string is desired, use  \z instead (see Assertions).

Re: search a regex list
by eff_i_g (Curate) on Dec 12, 2010 at 18:10 UTC
Re: search a regex list
by CountZero (Bishop) on Dec 13, 2010 at 07:19 UTC
    Have a look at Regexp::Keywords, a regexp builder to test against keywords lists. It has lots of options and will be quite useful if your list gets more complicated.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James