in reply to Need help with File::Find

Out with the old and in with the new
use File::Find::Rule; my $ext = "html|php"; my $dirs = "(?:cgi-bin|log)"; my $base = "c:/blub"; my @files = find(not => rule( directory => name => qr/^$dirs/ => prune => ), file => name => qr/\.(?:$ext)\z/, in => $base);
See the File::Find::Rule docs for more info on this fabulous module.
HTH

_________
broquaint

update: code now corresponds to question
update 2: added grouping to $dirs missing from original data
update 3: fixed code re runrig's comments. sigh this just ain't my node

Replies are listed 'Best First'.
Re: Re: Need help with File::Find
by tachyon (Chancellor) on Feb 26, 2003 at 15:50 UTC

    Your RE binds to /cgi-bin and just log not /log. You need a (?: ) in there...

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Re: Need help with File::Find
by runrig (Abbot) on Feb 26, 2003 at 19:58 UTC
    A few problems:
    • Your extensions don't match just the end of the file name.
    • You're missing a '=>' after the second 'name'.
    • The pruning rule doesn't seem to work in combination with the first 'file => name' rule (update: if the 'file => name' rule comes after the 'not ( ... prune )' rule), then the procedural answer works).
    • The dirs still are not right. you shouldn't have a leading slash, and you probably ought to anchor the regex.

    I couldn't get the prune to work with the procedural interface, so here's the OO style answer:

    #!/usr/bin/perl use strict; use warnings; use File::Find::Rule; use constant FFR => "File::Find::Rule"; my $ext = qr/\.(?:html|php)$/; my $dirs = qr/^(?:cgi-bin|log)$/; my $base = "c:/blub"; my @files = FFR->or( FFR->directory->name($dirs)->prune->discard, FFR->file->name($ext), )->in($base); print "$_\n" for @files;
    Update: The procedural interface works if you put the first 'file => name ...' rule AFTER the 'not ... => prune' rule (just like it is above in the OO interface).