NAME
File::Find::Rex - Combines simpler File::Find interface with support for regular expression search criteria.
VERSION
Version 1.00
DESCRIPTION
This module provides an easy to use object oriented interface to File::Find
and adds the ability to filter results using regular expressions.
Features include:
Object oriented interface
Find results returned as array or via a callback subroutine
Regular expression matching
Option to ignore directory listings in output (i.e., just files)
Option to ignore hidden files
Option to scope query using file last modified date
Caller provided context passed when using callback subroutine
SYNOPSIS
# Example 1: Simplest use-case - finds all files in present working # directory.
use File::Find::Rex;
my $source = ".";
my $rex = new File::Find::Rex;
my @files = $rex->query($source);
foreach (@files)
{
say $_;
}
# Example 2: Regex use-case - finds all files in present working # directory that start with the letter 'b' or 'B'.
my @files = $rex->query($source, qr/^b/i);
foreach (@files) {
say $_;
}
# Example 3: Setting find options - sets options to perform recursive # query, ignore hidden files, and ignore directory entries in results.
$rex->set_option('ignore_dirs', 1);
$rex->set_option('ignore_hidden', 1);
$rex->set_option('recursive', 1);
my @files = $rex->query($source);
foreach (@files) {
say $_;
}
# Example 4: Callback method - results returned via callback method # instead of array, and options are passed to constructor.
my %options = (
recursive => 1,
ignore_dirs => 1,
ignore_hidden => 1,
);
$rex = new File::Find::Rex(\%options, \&callback);
$rex->query($source);
sub callback {
say shift;
}
CONSTRUCTOR
File::Find::Rex->new(<options>, <callback>)
The constructor takes two optional arguments:
options is a hash reference containing key-value pairs specifying find options. See the Options section for the list of available options.
callback is a code reference to a subroutine that is called for each find result. If passed, query
results are returned via the callback method instead of return value.
SUBROUTINES/METHODS
query([source], <regexp>, <context>)
The query method takes one required and two optional arguments:
source is the directory or file path to start search. A directory path is typically passed; however, support for file path is offered to simplify application logic by providing consistent call semantics and behavior in scenarios where input can be a single file or a collection.
regex is a regular expression for constraining query result to matching filenames. The regular expression is tested on jstu the filename - basename and extension. The file's directory path is not evaluated. If passed, ref type expected is Regexp
, which can be accomplished using regexp-like quoted string using qr
. For more information see http://perldoc.perl.org/functions/qr.html.
context enables the caller have an arbitrary value or reference passed to the callback method. It can be used, for example, by an object to get a reference back to itself when callback is used to get results. This option is only applicable when a callback is used.
set_option([option], <value>)
The set_option method takes one required and one optional argument:
option specifies the option to set. See Options section for list of options.
value specifies the option value. See Options section for the values settable for each option. If this argument is not set then option value is set to undef, which has same effect as unsetting it.
is_ignore_dirs
Returns ignore_dirs option setting. 0: disabled, 1: enabled.
is_ignore_hidden
Returns ignore_hidden option setting. 0: disabled, 1: enabled.
get_last_modified_earliest
Returns last_modified_earliest epoch time value. If disabled, undef is returned.
get_last_modified_latest
Returns last_modified_latest epoch time value. If disabled, undef is returned.
is_recursive
Returns recursive option setting. 0: disabled, 1: enabled.
OPTIONS
File::Find::Rex
utilizes the options listed here to control find behavior and to filter results. Options are set by passing a hash reference to the construtor and by using the set_option
method. When passing options constructor, each key-value pair maps to the option name and its value.
ignore_dirs (default: disabled)
If set, this option suppresses listing directory entries in results. I.e., just files are returned.
To enable this option, set its value to 1. To disable set to 0 or undef, or do not create a hash entry for it when passing options to construtor.
ignore_hidden (default: disabled)
If set, this option suppresses listing hidden files in results.
To enable this option, set its value to 1. To disable, set to 0 or undef, or do not create a hash entry for it when passing options to construtor.
ignore_symlink (default: disabled)
If set, this option suppresses listing symbolic linked files in results.
To enable this option, set its value to 1. To disable, set to 0 or undef, or do not create a hash entry for it when passing options to construtor.
last_modified_latest (default: disabled)
If set, this option scopes query to files having file last modified timestamp values less than or equal to the option value specified. Timestamp values are in epoch datetime format, meaning an integer value is expected.
To enable this option, set its value to an epoch timestamp. To disable, set to undef or do not create a hash entry for it when passing options to construtor.
last_modified_earliest (default: disabled)
If set, this option scopes query to files having file last modified timestamp values greater than or equal to the option value specified. Timestamp values are in epoch datetime format, meaning an integer value is expected.
To enable this option, set its value to an epoch timestamp. To disable, set to undef or do not create a hash entry for it when passing options to construtor.
recursive (default: disabled)
If set, this option includes all subdirectories under the source directory specified in query
call. This option has no affect on results if the source specified is a file.
To enable this option, set its value to 1. To disable, set to 0 or undef, or do not create a hash entry for it when passing options to construtor.
CALLBACK
If the caller passes an optional callback subroutine reference to the constructor then query
results are returned via the callback instead being returned as an array. The callback method is called for each file found (as it is discovered) instead of waiting for the query to complete to return results.
callback([file], <context>)
The callback subroutine is called for each file found, and arguments are passed in the order shown:
file is the fully qualified path to a file or directory found.
context is the optional, caller defined value or reference passed to query
call.
DEPENDENCIES
This module has the following depenencies:
Carp 1.29
Cwd 3.40
File::Basename 2.84
File::Find 1.23
File::Spec 3.40
SUPPORT
You can find documentation for this module with the perldoc command.
perldoc File::Find::Rex
You can also look for information at:
RT: CPAN's request tracker (report bugs here)
AnnoCPAN: Annotated CPAN documentation
CPAN Ratings
Search CPAN
GitHub
BUGS AND LIMITATIONS
Please report any bugs or feature requests to bug-file-find-rex at rt.cpan.org
, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=File-Find-Rex. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
SEE ALSO
AUTHOR
Roland Ayala, <rolanday at cpan.org>
LICENSE AND COPYRIGHT
Copyright 2018 Roland Ayala.
This program is free software; you can redistribute it and/or modify it under the terms of the the Artistic License (2.0). You may obtain a copy of the full license at:
http://www.perlfoundation.org/artistic_license_2_0
Any use, modification, and distribution of the Standard or Modified Versions is governed by this Artistic License. By using, modifying or distributing the Package, you accept this license. Do not use, modify, or distribute the Package, if you do not accept this license.
If your Modified Version has been derived from a Modified Version made by someone other than you, you are nevertheless required to ensure that your Modified Version complies with the requirements of this license.
This license does not grant you the right to use any trademark, service mark, tradename, or logo of the Copyright Holder.
This license includes the non-exclusive, worldwide, free-of-charge patent license to make, have made, use, offer to sell, sell, import and otherwise transfer the Package with respect to any patent claims licensable by the Copyright Holder that are necessarily infringed by the Package. If you institute patent litigation (including a cross-claim or counterclaim) against any party alleging that the Package constitutes direct or contributory patent infringement, then this Artistic License to you shall terminate on the date that such litigation is filed.
Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.