File::...::Rule(3pm)

๐Ÿ“ฆ
version 0.0313 ยท 5 versions
, in debian bookworm, debian trixie, ubuntu jammy, ubuntu noble, ubuntu resolute, ubuntu stonking

๐ŸŒธ NAME ๐ŸŒธ

okay so bestie, this is File::Find::Object::Rule(3pm) from the User Contributed Perl Documentation โ€” the Alternative interface to File::Find::Object. That's the whole vibe: it's a friendlier way to tell your computer which files and directories you want. ๐Ÿ’–

๐ŸŒŸ VERSION ๐ŸŒŸ

version 0.0313

๐Ÿฆ‹ SYNOPSIS ๐Ÿฆ‹

  use File::Find::Object::Rule;
  # find all the subdirectories of a given directory
  my @subdirs = File::Find::Object::Rule->directory->in( $directory );

  # find all the .pm files in @INC
  my @files = File::Find::Object::Rule->file()
                              ->name( '*.pm' )
                              ->in( @INC );

  # as above, but without method chaining
  my $rule =  File::Find::Object::Rule->new;
  $rule->file;
  $rule->name( '*.pm' );
  my @files = $rule->in( @INC );

๐Ÿ’… DESCRIPTION ๐Ÿ’…

File::Find::Object::Rule is a friendlier interface to File::Find::Object. It lets you build rules that specify the desired files and directories. Slay.

โš ๏ธ WARNING: This module is a fork of version 0.30 of File::Find::Rule (which has been unmaintained for several years as of February, 2009), and may still have some bugs due to its reliance on File::Find'isms. As such it is considered Alpha software. Please report any problems with File::Find::Object::Rule to its RT CPAN Queue. ๐Ÿซถ

๐ŸŽ€ METHODS ๐ŸŽ€

๐Ÿ” new ๐Ÿ”

A constructor. You need not invoke new manually unless you wish to, as each of the rule-making methods will auto-create a suitable object if called as class methods. โœจ

๐Ÿ“œ finder ๐Ÿ“œ

The File::Find::Object finder instance itself.

๐Ÿ“œ my @rules = @{$ffor->rules()}; ๐Ÿ“œ

The rules to match against. For internal use only.

๐Ÿ’˜ Matching Rules ๐Ÿ’˜

๐ŸŽฏ name( @patterns ) ๐ŸŽฏ

Specifies names that should match. May be globs or regular expressions.

$set->name( '*.mp3', '*.ogg' ); # mp3s or oggs
$set->name( qr/\.(mp3|ogg)$/ ); # the same as a regex
$set->name( 'foo.bar' );        # just things named foo.bar

๐Ÿ”ฅ -X tests ๐Ÿ”ฅ

Synonyms are provided for each of the -X tests. See "-X" in perlfunc for details. None of these methods take arguments.

  Test | Method               Test |  Method
 ------|-------------        ------|----------------
   -r  |  readable             -R  |  r_readable
   -w  |  writeable            -W  |  r_writeable
   -w  |  writable             -W  |  r_writable
   -x  |  executable           -X  |  r_executable
   -o  |  owned                -O  |  r_owned
       |                           |
   -e  |  exists               -f  |  file
   -z  |  empty                -d  |  directory
   -s  |  nonempty             -l  |  symlink
       |                       -p  |  fifo
   -u  |  setuid               -S  |  socket
   -g  |  setgid               -b  |  block
   -k  |  sticky               -c  |  character
       |                       -t  |  tty
   -M  |  modified                 |
   -A  |  accessed             -T  |  ascii
   -C  |  changed              -B  |  binary

Though some tests are fairly meaningless as binary flags (modified, accessed, changed), they have been included for completeness.

# find nonempty files
$rule->file,
     ->nonempty;

๐Ÿ“Š stat tests ๐Ÿ“Š

The following stat based methods are provided: dev, ino, mode, nlink, uid, gid, rdev, size, atime, mtime, ctime, blksize, and blocks. See "stat" in perlfunc for details.

Each of these can take a number of targets, which will follow Number::Compare semantics.

$rule->size( 7 );         # exactly 7
$rule->size( ">7Ki" );    # larger than 7 * 1024 * 1024 bytes
$rule->size( ">=7" )
     ->size( "<=90" );    # between 7 and 90, inclusive
$rule->size( 7, 9, 42 );  # 7, 9 or 42

๐Ÿค any( @rules ) / or( @rules ) ๐Ÿค

Allows shortcircuiting boolean evaluation as an alternative to the default and-like nature of combined rules. any and or are interchangeable.

# find avis, movs, things over 200M and empty files
$rule->any( File::Find::Object::Rule->name( '*.avi', '*.mov' ),
            File::Find::Object::Rule->size( '>200M' ),
            File::Find::Object::Rule->file->empty,
          );

๐Ÿšซ none( @rules ) / not( @rules ) ๐Ÿšซ

Negates a rule. (The inverse of any.) none and not are interchangeable.

 # files that aren't 8.3 safe
 $rule->file
      ->not( $rule->new->name( qr/^[^.]{1,8}(\.[^.]{0,3})?$/ ) );

โœ‚๏ธ prune โœ‚๏ธ

Traverse no further. This rule always matches.

๐Ÿ—‘๏ธ discard ๐Ÿ—‘๏ธ

Don't keep this file. This rule always matches.

๐Ÿง  exec( \&subroutine( $shortname, $path, $fullname ) ) ๐Ÿง 

Allows user-defined rules. Your subroutine will be invoked with parameters of the name, the path you're in, and the full relative filename. In addition, $_ is set to the current short name, but its use is discouraged since as opposed to File::Find::Rule, File::Find::Object::Rule does not cd to the containing directory.

Return a true value if your rule matched.

# get things with long names
$rules->exec( sub { length > 20 } );

๐Ÿ”Ž ->grep( @specifiers ); ๐Ÿ”Ž

Opens a file and tests it each line at a time.

For each line it evaluates each of the specifiers, stopping at the first successful match. A specifier may be a regular expression or a subroutine. The subroutine will be invoked with the same parameters as an ->exec subroutine.

It is possible to provide a set of negative specifiers by enclosing them in anonymous arrays. Should a negative specifier match the iteration is aborted and the clause is failed. For example:

$rule->grep( qr/^#!.*\bperl/, [ sub { 1 } ] );

Is a passing clause if the first line of a file looks like a perl shebang line.

โฌ‡๏ธ maxdepth( $level ) โฌ‡๏ธ

Descend at most $level (a non-negative integer) levels of directories below the starting point.

May be invoked many times per rule, but only the most recent value is used.

โฌ†๏ธ mindepth( $level ) โฌ†๏ธ

Do not apply any tests at levels less than $level (a non-negative integer).

โž• extras( \%extras ) โž•

Specifies extra values to pass through to "File::File::find" as part of the options hash.

For example this allows you to specify following of symlinks like so:

my $rule = File::Find::Object::Rule->extras({ follow => 1 });

May be invoked many times per rule, but only the most recent value is used.

๐Ÿšถ relative ๐Ÿšถ

Trim the leading portion of any path found

๐Ÿšซ not_* ๐Ÿšซ

Negated version of the rule. An effective shortand related to ! in the procedural interface.

$foo->not_name('*.pl');

$foo->not( $foo->new->name('*.pl' ) );

๐Ÿ”ฎ Query Methods ๐Ÿ”ฎ

๐Ÿ“‚ in( @directories ) ๐Ÿ“‚

Evaluates the rule, returns a list of paths to matching files and directories.

๐Ÿ start( @directories ) ๐Ÿ

Starts a find across the specified directories. Matching items may then be queried using "match". This allows you to use a rule as an iterator.

my $rule = File::Find::Object::Rule->file->name("*.jpeg")->start( "/web" );
while ( my $image = $rule->match ) {
    ...
}

๐ŸŽฏ match ๐ŸŽฏ

Returns the next file which matches, false if there are no more.

๐Ÿงฉ Extensions ๐Ÿงฉ

Extension modules are available from CPAN in the File::Find::Object::Rule namespace. In order to use these extensions either use them directly:

use File::Find::Object::Rule::ImageSize;
use File::Find::Object::Rule::MMagic;

# now your rules can use the clauses supplied by the ImageSize and
# MMagic extension

or, specify that File::Find::Object::Rule should load them for you:

use File::Find::Object::Rule qw( :ImageSize :MMagic );

For notes on implementing your own extensions, consult File::Find::Object::Rule::Extending

๐Ÿ“ Further examples ๐Ÿ“

๐Ÿช Finding perl scripts ๐Ÿช

my $finder = File::Find::Object::Rule->or
 (
  File::Find::Object::Rule->name( '*.pl' ),
  File::Find::Object::Rule->exec(
                         sub {
                             if (open my $fh, $_) {
                                 my $shebang = <$fh>;
                                 close $fh;
                                 return $shebang =~ /^#!.*\bperl/;
                             }
                             return 0;
                         } ),
 );

Based upon this message http://use.perl.org/comments.pl?sid=7052&cid=10842

๐Ÿ™ˆ ignore CVS directories ๐Ÿ™ˆ

my $rule = File::Find::Object::Rule->new;
$rule->or($rule->new
               ->directory
               ->name('CVS')
               ->prune
               ->discard,
          $rule->new);

Note here the use of a null rule. Null rules match anything they see, so the effect is to match (and discard) directories called 'CVS' or to match anything.

๐Ÿ’ธ TWO FOR THE PRICE OF ONE ๐Ÿ’ธ

File::Find::Object::Rule also gives you a procedural interface. This is documented in File::Find::Object::Rule::Procedural

๐Ÿ“ฆ EXPORTS ๐Ÿ“ฆ

๐Ÿ”Ž find

๐Ÿ“ rule

๐Ÿงช Tests ๐Ÿงช

  • accessed โ€” Corresponds to -A.
  • ascii โ€” Corresponds to -T.
  • atime โ€” See "stat tests".
  • binary โ€” Corresponds to -b.
  • blksize โ€” See "stat tests".
  • block โ€” Corresponds to -b.
  • blocks โ€” See "stat tests".
  • changed โ€” Corresponds to -C.
  • character โ€” Corresponds to -c.
  • ctime โ€” See "stat tests".
  • dev โ€” See "stat tests".
  • directory โ€” Corresponds to -d.
  • empty โ€” Corresponds to -z.
  • executable โ€” Corresponds to -x.
  • exists โ€” Corresponds to -e.
  • fifo โ€” Corresponds to -p.
  • file โ€” Corresponds to -f.
  • gid โ€” See "stat tests".
  • ino โ€” See "stat tests".
  • mode โ€” See "stat tests".
  • modified โ€” Corresponds to -M.
  • mtime โ€” See "stat tests".
  • nlink โ€” See "stat tests".
  • r_executable โ€” Corresponds to -X.
  • r_owned โ€” Corresponds to -O.
  • nonempty โ€” A predicate that determines if the file is empty. Uses -s.
  • owned โ€” Corresponds to -o.
  • r_readable โ€” Corresponds to -R.
  • r_writeable โ€” Corresponds to -W.
  • r_writable โ€” Corresponds to -W.
  • rdev โ€” See "stat tests".
  • readable โ€” Corresponds to -r.
  • setgid โ€” Corresponds to -g.
  • setuid โ€” Corresponds to -u.
  • size โ€” See stat tests.
  • socket โ€” Corresponds to -S.
  • sticky โ€” Corresponds to -k.
  • symlink โ€” Corresponds to -l.
  • uid โ€” See "stat tests".
  • tty โ€” Corresponds to -t.
  • writable() โ€” Corresponds to -w.

๐Ÿ’€ BUGS

Okay so real talk bestie, this module is built on qr// compiled regexes, which means it needs perl version 5.005_03 or newer. That's the floor, no getting around it. ๐Ÿซถ

And here's the sad one: right now there is literally no way to remove a clause from a rule object. Like, once a clause is in there, it's in there. If this ever becomes a big enough deal it'll get fixed, but for now? It stays. ๐Ÿ’…


๐Ÿ‘‘ AUTHOR

Richard Clamp <richardc@unixbeard.net> โ€” and he got input from this use.perl discussion:

http://use.perl.org/~richardc/journal/6467

Extra proofreading and input came from Kake, Greg McCarroll, and Andy Lester andy@petdance.com. Slay, collaborators. โœจ

And the whole thing was ported to use File::Find::Object as File::Find::Object::Rule by Shlomi Fish. Iconic behavior honestly. ๐Ÿฆ‹


Copyright (C) 2002, 2003, 2004, 2006 Richard Clamp. All Rights Reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. ๐ŸŒˆ


๐ŸŒธ SEE ALSO

  • File::Find::Object
  • Text::Glob
  • Number::Compare
  • find(1)

If you wanna know about the procedural interface, go check out File::Find::Object::Rule::Procedural. And if you've got an idea for a neat extension, the vibe you're looking for is File::Find::Object::Rule::Extending. ๐Ÿ’–

Also!! The Path::Class::Rule Xs SEE ALSO has a whole review of many directory traversal modules on CPAN, including File::Find::Object::Rule and File::Find::Rule (which this module is based on). Obsessed. Go read it. ๐Ÿ‘‘


๐Ÿ™ˆ KNOWN BUGS

The tests don't run successfully when directly inside an old Subversion checkout, and it's all because of those .svn directories showing up. So annoying. ๐Ÿ˜ญ

But bestie, don't panic โ€” ./Build disttest or ./Build distruntest run fine. ๐Ÿซถ


๐Ÿ’œ SUPPORT

๐ŸŒˆ Websites

The following websites have more information about this module, and may be of help to you. As always, in addition to those websites please use your favorite search engine to discover more resources.

  • MetaCPAN

A modern, open-source CPAN search engine, useful to view POD in HTML format.

<https://metacpan.org/release/File-Find-Object-Rule>

  • RT: CPAN's Bug Tracker

The RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN.

<https://rt.cpan.org/Public/Dist/Display.html?Name=File-Find-Object-Rule>

  • CPANTS

The CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution.

<http://cpants.cpanauthors.org/dist/File-Find-Object-Rule>

  • CPAN Testers

The CPAN Testers is a network of smoke testers who run automated tests on uploaded CPAN distributions.

<http://www.cpantesters.org/distro/F/File-Find-Object-Rule>

  • CPAN Testers Matrix

The CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms.

<http://matrix.cpantesters.org/?dist=File-Find-Object-Rule>

  • CPAN Testers Dependencies

The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution.

<http://deps.cpantesters.org/?module=File::Find::Object::Rule>


๐Ÿฆ‹ Bugs / Feature Requests

Please report any bugs or feature requests by email to "bug-file-find-object-rule at rt.cpan.org", or through the web interface at

<https://rt.cpan.org/Public/Bug/Report.html?Queue=File-Find-Object-Rule>

You will be automatically notified of any progress on the request by the system. No ghosting, fr fr. โœจ


๐ŸŽ€ Source Code

The code is open to the world, and available for you to hack on. Please feel free to browse it and play with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull from your repository :) ๐Ÿ’–

<https://github.com/shlomif/http://bitbucket.org/shlomif/perl-file-find-object-rule>

git clone git://github.com/shlomif/http://bitbucket.org/shlomif/perl-file-find-object-rule.git

๐Ÿ’– AUTHORS

  • Richard Clamp <richardc@unixbeard.net>
  • Andy Lester andy@petdance.com.

๐Ÿ’€ BUGS

Please report any bugs or feature requests on the bugtracker website

<https://github.com/shlomif/http://bitbucket.org/shlomif/perl-file-find-object-rule/issues>

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature. That's the love language of maintainers, no cap. ๐Ÿซถ


This software is copyright (c) 2021 by Richard Clamp.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. ๐ŸŒˆ


And that's the whole page bestie!! โœจ Sourced from perl v5.32.1, dated 2021-09-04, and it's the File::...::Rule(3pm) manual page. Go forth and find some files. ๐Ÿ’…๐Ÿฆ‹๐ŸŽ€
๐Ÿ“ฆ where this page comes from