I'm new to PerlMonks and fairly new to PERL. Long story short, I'm trying to clean up and speed up code and am struggling with Find::File::Rule. I read other posts here and on StackOverflow that helped, but I can't get to that last little bit.

The story: I want to build a list of directories or files, but exclude some based on patterns.

It started with:

find ( { no_chdir => 0, wanted => sub { return unless -d; # skip files; we're tagging folders return if $File::Find::name =~ /\/\./; return if $File::Find::name =~ /\./; return if $File::Find::name =~ /\.store$/i; return if $File::Find::name =~ /\/LOG(\/|$)/i; return if $File::Find::name =~ /\.go\./i; return if $File::Find::name =~ /\/cache(\/|$)/i; return if $File::Find::name =~ /\.store$/i; return if $File::Find::name =~ /\/AVCHD(\/|$)/i; push ( @fileList, $File::Find::name ); # get name with path }}, $tagLocation );

Hideous, but it worked. So I cleaned it up to:

our @dirExclusions = qw( \. \/LOG(\/|$) \/cache(\/|$) \/AVCHD(\/|$) ); # call regex only once our $dirExclusionsQR = join( '|', @dirExclusions ); # compile the expression $dirExclusionsQR = qr{$dirExclusionsQR}i; my @fileList3; find ( { no_chdir => 0, wanted => sub { return unless -d; # skip files; we're tagging folders return if $File::Find::name =~ $dirExclusionsQR; push ( @fileList3, $File::Find::name ); }}, $tagLocation );

Much better, and I get the benefit from qr//. Life is good. Now I want to make it more readable and perhaps faster with "File::Find::Rule" and ->name()->prune. So:

our @dirExclusions = qw( \. \/LOG(\/|$) \/cache(\/|$) \/AVCHD(\/|$) ); our @dirExclusionsQR = map { qr/$_/i } @dirExclusions; my $rule = File::Find::Rule->new; $rule->or( $rule->new->directory->name( @dirExclusionsQR )->prune->discard, $rule->new->directory); my @fileList2; @fileList2 = $rule->in($tagLocation);

This version doesn't return anything. I'll spare you the variants of putting qr inside @dirExclusions, using q() instead of qw(), taking off the '\/' (on the assumption File::Find::Rule wasn't matching on the full path), and such.

Next attempt: use a string, as in @fileList3 above.

our @dirExclusions = qw( \. \/LOG(\/|$) \/cache(\/|$) \/AVCHD(\/|$) ); our $dirExclusionsQR = join( '|', @dirExclusions ); $dirExclusionsQR = qr{$dirExclusionsQR}i; my $rule = File::Find::Rule->new; $rule->or( $rule->new->directory->name( $dirExclusionsQR )->prune->discard, $rule->new->directory); my @fileList2; @fileList2 = $rule->in($tagLocation);

Null output. How about typing it in directly?

$rule->or( $rule->new->directory ->name( qr/(\.|\/LOG(\/|$)|\/cache(\/|$)|\/AVCHD(\/|$))/i ) ->prune->discard, $rule->new->directory);

Doesn't work either.


Finally, out of desperation:

our @de = qw (*.* LOG cache AVCHD); my $rule = File::Find::Rule->new; $rule->or( $rule->new->directory->name( @de )->prune->discard, $rule->new->directory); my @fileList2; @fileList2 = $rule->in($tagLocation);

It does work, but I cannot use regex and I don't think the wildcards are compiled. And it doesn't solve my regex issue. What am I missing?

Thanks!


In reply to Formatting Regex for File::Find::Rule by springgem

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.