File-Wildcard-0.11/0000755000175000017500000000000011156505000012552 5ustar ivorivorFile-Wildcard-0.11/t/0000755000175000017500000000000011156505000013015 5ustar ivorivorFile-Wildcard-0.11/t/04_append.t0000644000175000017500000000205510502761730014766 0ustar ivorivor# -*- perl -*- # t/04_append.t - Tests for multiwildcard append and prepend use strict; use Test::More tests => 6; my $debug = $ENV{FILE_WILDCARD_DEBUG} || 0; #01 BEGIN { use_ok('File::Wildcard'); } my $mods = File::Wildcard->new( debug => $debug ); #02 isa_ok( $mods, 'File::Wildcard', "return from new" ); #03 ok( !$mods->next, 'Empty object gives no files' ); $mods->append( path => 'lib///Wildcard.pm' ); $mods->append( path => './//04*.t' ); my @found = map { lc $_ } $mods->all; #04 is_deeply( \@found, [ qw( lib/file/wildcard.pm t/04_append.t ) ], 'Appended wildcards' ); $mods->prepend( path => './//04*.t' ); $mods->prepend( path => 'lib///Wildcard.pm' ); @found = map { lc $_ } $mods->all; #05 is_deeply( \@found, [ qw( lib/file/wildcard.pm t/04_append.t ) ], 'Prepended wildcards' ); $mods->append( path => 'lib/File///' ); $mods->match(qr{ \Alib/File/(.*rd)\.pm\z }xms); @found = map { lc $_ } $mods->all; #06 is_deeply( \@found, [qw( lib/file/wildcard.pm)], 'Append with match' ); File-Wildcard-0.11/t/03_absolute.t0000644000175000017500000001215310565423153015337 0ustar ivorivor# -*- perl -*- # t/03_absolute.t - Absolute file spec test use strict; use Test::More; BEGIN { if ( $^O =~ /vms/i ) { plan skip_all => "Cannot test absolute POSIX files on this platform"; } else { plan tests => 19; } #01 use_ok('File::Wildcard'); } use File::Spec; my $debug = $ENV{FILE_WILDCARD_DEBUG} || 0; my $temp = File::Spec->tmpdir . '/File-Wildcard-test'; $temp =~ s!\\!/!g; # for Windows silly slash direction # Just in case the temp directory is lying around... if ( -e $temp ) { my $wcrm = File::Wildcard->new( path => "$temp///", ellipsis_order => "inside-out" ); for ( $wcrm->all ) { if ( -d $_ ) { rmdir $_; } else { 1 while unlink $_; } } } mkdir $temp; mkdir "$temp/abs"; mkdir "$temp/abs/foo"; mkdir "$temp/abs/bar"; open FOO, ">$temp/abs/foo/lish.tmp"; close FOO; open FOO, ">$temp/abs/bar/drink.tmp"; close FOO; # Force the case sensitivity for absolute files # as it says in the docs my $sens = Filesys::Type::case($temp) ne 'sensitive'; my $mods = File::Wildcard->new( path => "$temp/abs/foo/lish.tmp", case_insensitive => $sens, debug => $debug ); #02 isa_ok( $mods, 'File::Wildcard', "return from new" ); #03 like( $mods->next, qr"$temp/abs/foo/lish.tmp"i, 'Simple case, no wildcard' ); #04 ok( !$mods->next, 'Only found one file' ); my ( $junk, @chunks ) = split m'/', "$temp/abs/*/*.tmp"; $mods = File::Wildcard->new( path => \@chunks, case_insensitive => $sens, debug => $debug, absolute => 1, sort => 1 ); #05 isa_ok( $mods, 'File::Wildcard', "return from new" ); my @found = $mods->all; SKIP: { skip 'This test unreliable on Windows', 1 if $^O =~ /win/i; #06 is_deeply( \@found, [ "$temp/abs/bar/drink.tmp", "$temp/abs/foo/lish.tmp" ], 'Wildcard in filename' ); } $mods = File::Wildcard->new( path => "$temp///*.tmp", case_insensitive => $sens, debug => $debug, sort => 1 ); #07 isa_ok( $mods, 'File::Wildcard', "(ellipsis) return from new" ); @found = $mods->all; #08 is_deeply( \@found, [ "$temp/abs/bar/drink.tmp", "$temp/abs/foo/lish.tmp" ], 'Ellipsis found tmp files' ); $mods = File::Wildcard->new( path => "$temp///", case_insensitive => $sens, debug => $debug, sort => 1 ); #09 isa_ok( $mods, 'File::Wildcard', "(ellipsis) return from new" ); @found = $mods->all; #10 is_deeply( \@found, [ "$temp/", "$temp/abs/", "$temp/abs/bar/", "$temp/abs/bar/drink.tmp", "$temp/abs/foo/", "$temp/abs/foo/lish.tmp", ], 'Recursive directory search (normal)' ); $mods = File::Wildcard->new( path => "$temp///", case_insensitive => $sens, debug => $debug, sort => sub { $_[1] cmp $_[0] } ); #11 isa_ok( $mods, 'File::Wildcard', "(ellipsis) return from new" ); @found = $mods->all; #12 is_deeply( \@found, [ "$temp/", "$temp/abs/", "$temp/abs/foo/", "$temp/abs/foo/lish.tmp", "$temp/abs/bar/", "$temp/abs/bar/drink.tmp", ], 'Recursive directory search (custom sort)' ); $mods = File::Wildcard->new( path => "$temp///", case_insensitive => $sens, debug => $debug, sort => 1, ellipsis_order => 'breadth-first' ); #13 isa_ok( $mods, 'File::Wildcard', "(ellipsis) return from new" ); @found = $mods->all; # Note that breadth-first skips the topmost level # I have not found an easy way round this. #14 is_deeply( \@found, [ "$temp/abs/", "$temp/abs/bar/", "$temp/abs/foo/", "$temp/abs/bar/drink.tmp", "$temp/abs/foo/lish.tmp", ], 'Recursive directory search (breadth-first)' ); # Append absolute bug $mods = File::Wildcard->new( debug => $debug, sort => 1 ); $mods->append( path => "$temp///*.tmp" ); @found = $mods->all; #15 is_deeply( \@found, [ "$temp/abs/bar/drink.tmp", "$temp/abs/foo/lish.tmp" ], "Appended absolute" ); $mods = File::Wildcard->new( path => "$temp///", case_insensitive => $sens, debug => $debug, sort => 1, ellipsis_order => 'inside-out' ); #16 isa_ok( $mods, 'File::Wildcard', "(ellipsis) return from new" ); @found = $mods->all; #17 is_deeply( \@found, [ "$temp/abs/bar/drink.tmp", "$temp/abs/bar/", "$temp/abs/foo/lish.tmp", "$temp/abs/foo/", "$temp/abs/", "$temp/", ], 'Recursive directory search (inside-out)' ); $mods->append( path => "$temp///" ); @found = $mods->all; #18 is_deeply( \@found, [ "$temp/abs/bar/drink.tmp", "$temp/abs/bar/", "$temp/abs/foo/lish.tmp", "$temp/abs/foo/", "$temp/abs/", "$temp/", ], 'Append to absolute' ); # Tidy up after tests for (@found) { if ( -d $_ ) { rmdir $_; } else { 1 while unlink $_; } } rmdir $temp; #19 ok( !-e $temp, "Test has tidied up after itself" ); File-Wildcard-0.11/t/pod.t0000644000175000017500000000030010502757740013773 0ustar ivorivor# -*- perl -*- # t/pod.t - Generic Test::Pod testing module use Test::More; eval "use Test::Pod 1.00"; plan skip_all => "Test::Pod 1.00 required for testing POD" if $@; all_pod_files_ok(); File-Wildcard-0.11/t/06_fwf.t0000644000175000017500000000114510502761731014303 0ustar ivorivor# -*- perl -*- # t/06_fwf.t - File::Wildcard::Find tests use strict; use Test::More tests => 6; #01 BEGIN { use_ok('File::Wildcard::Find'); } eval { findbegin('lib/File/Wildcard.pm') }; #02 ok( !$@, "findbegin didn't croak" ); #03 like( findnext, qr'lib/File/Wildcard.pm'i, 'Simple case, no wildcard' ); #04 ok( !findnext, 'Only found one file' ); my @all = findall('.///Wildcard.pm'); #05 is( scalar(@all), 2, "Two files found" ); my @found = sort map { lc $_ } @all; #06 is_deeply( \@found, [qw( blib/lib/file/wildcard.pm lib/file/wildcard.pm )], 'Ellipsis found blib and lib modules' ); File-Wildcard-0.11/t/01_basic.t0000644000175000017500000000671610502761725014611 0ustar ivorivor# -*- perl -*- # t/01_basic.t - basic tests use strict; use Test::More tests => 35; my $dbf; my $debug = open $dbf, '>', 'debug.out'; if ( $ENV{FILE_WILDCARD_DEBUG} ) { close $dbf; $dbf = \*STDERR; } BEGIN { local $ENV{MODULE_OPTIONAL_SKIP} = 1; #01 use_ok('File::Wildcard'); } # Run in both case sensitive and insensitive mode for my $insens ( 0, 1 ) { my $mods = File::Wildcard->new( path => 'lib/File/Wildcard.pm', case_insensitive => $insens, debug_output => $dbf, debug => $debug ); #02 isa_ok( $mods, 'File::Wildcard', "return from new" ); #03 like( $mods->next, qr'lib/File/Wildcard.pm'i, 'Simple case, no wildcard' ); #04 ok( !$mods->next, 'Only found one file' ); my @dirs = split m'/', 'lib/File/Wildcard.*'; $mods = File::Wildcard->new( path => \@dirs, absolute => 0, case_insensitive => $insens, debug_output => $dbf, debug => $debug ); #05 isa_ok( $mods, 'File::Wildcard', "return from new" ); #06 like( $mods->next, qr'lib/File/Wildcard\.pm'i, 'Simple asterisk' ); #07 ok( !$mods->next, 'Only found one file' ); $mods = File::Wildcard->new( path => 'lib/File/Wild????.pm', case_insensitive => $insens, debug_output => $dbf, debug => $debug ); #08 isa_ok( $mods, 'File::Wildcard', "return from new" ); #09 like( $mods->next, qr'lib/File/Wildcard\.pm'i, 'single char wildcards' ); #10 ok( !$mods->next, 'Only found one file' ); $mods = File::Wildcard->new( path => 'lib/F*/Wildcard.pm', case_insensitive => $insens, debug_output => $dbf, debug => $debug ); my $match = $mods->match; my @capts = $match =~ /\(.+?\)/; #11 is( scalar(@capts), 1, "Captures from regexp" ); #12 isa_ok( $mods, 'File::Wildcard', "return from new" ); my @found = map { lc $_ } $mods->all; #13 is_deeply( \@found, [qw( lib/file/wildcard.pm )], 'Wildcard further back in path' ); $mods = File::Wildcard->new( path => './//Wildcard.pm', case_insensitive => $insens, debug_output => $dbf, debug => $debug, sort => 1 ); #14 isa_ok( $mods, 'File::Wildcard', "(ellipsis) return from new" ); @found = map { lc $_ } $mods->all; #15 is_deeply( \@found, [qw( blib/lib/file/wildcard.pm lib/file/wildcard.pm )], 'Ellipsis found blib and lib modules' ); # play it again, Sam $mods->reset; @found = map { lc $_ } $mods->all; #16 is_deeply( \@found, [qw( blib/lib/file/wildcard.pm lib/file/wildcard.pm )], 'Ellipsis found blib and lib modules' ); $mods = File::Wildcard->new( path => './//Wildcard.pm', case_insensitive => $insens, debug_output => $dbf, debug => $debug, exclude => qr/^blib/, sort => 1 ); #17 isa_ok( $mods, 'File::Wildcard', "(ellipsis) return from new" ); @found = map { lc $_ } $mods->all; #18 is_deeply( \@found, [qw( lib/file/wildcard.pm )], 'Ellipsis found lib, blib excluded' ); } undef $dbf; unlink 'debug.out'; File-Wildcard-0.11/t/pod_coverage.t0000644000175000017500000000036710502761732015657 0ustar ivorivor# -*- perl -*- # t/pod_coverage.t - Generic Test::Pod::Coverage testing module use Test::More; eval "use Test::Pod::Coverage 1.00"; plan skip_all => "Test::Pod::Coverage 1.00 required for testing POD coverage" if $@; all_pod_coverage_ok(); File-Wildcard-0.11/t/02_derived.t0000644000175000017500000000225310502761725015143 0ustar ivorivor# -*- perl -*- # t/02_derived.t - Wildcards with captures use strict; use Test::More tests => 6; my $debug = $ENV{FILE_WILDCARD_DEBUG} || 0; #01 BEGIN { use_ok('File::Wildcard'); } my $mods = File::Wildcard->new( path => './//*.pm', derive => ['$1/$2.tmp'], debug => $debug, sort => 1 ); #02 isa_ok( $mods, 'File::Wildcard', "return from new" ); my @found = map { [ map { lc $_ } @$_ ] } $mods->all; #03 is_deeply( \@found, [ [qw( blib/lib/file/wildcard.pm blib/lib/file/wildcard.tmp )], [qw( blib/lib/file/wildcard/find.pm blib/lib/file/wildcard/find.tmp)], [qw( lib/file/wildcard.pm lib/file/wildcard.tmp)], [qw( lib/file/wildcard/find.pm lib/file/wildcard/find.tmp)], ], 'Returned expected derived list' ); $mods = File::Wildcard->new( path => [ split m'/', 'lib/File/Wild????.*' ], derive => ['Playing$1.$2'], debug => $debug ); #04 isa_ok( $mods, 'File::Wildcard', "return from new" ); @found = map { lc $_ } @{ $mods->next }; #05 is_deeply( \@found, [qw( lib/file/wildcard.pm playingcard.pm )], 'Multiple patterns in the same component' ); #06 ok( !$mods->next, 'Only one match' ); File-Wildcard-0.11/t/05_symlink.t0000644000175000017500000000215110502761731015204 0ustar ivorivor# -*- perl -*- # t/05_symlink.t - Symbolic link processing use strict; use Test::More; use Cwd; BEGIN { eval "symlink cwd, 't/sym_up'"; if ($@) { plan skip_all => "Symbolic links not available: $@"; } else { plan tests => 8; } #01: use_ok('File::Wildcard'); } my $debug = $ENV{FILE_WILDCARD_DEBUG} || 0; my $mods = File::Wildcard->new( path => './//*sym*', sort => 1, exclude => qr{/\.}, debug => $debug ); $mods->match(qr/sym/); #02 isa_ok( $mods, 'File::Wildcard', "return from new" ); #03 like( $mods->next, qr't/05_symlink.t'i, 'Found the test itself' ); #04 like( $mods->next, qr't/sym_up'i, 'Sym link' ); #05 ok( !$mods->next, 'And no more' ); $mods = File::Wildcard->new( path => 't///Changes', sort => 1, debug => $debug ); #06 ok( !$mods->next, 'Nothing found' ); $mods = File::Wildcard->new( path => 't///Changes', sort => 1, follow => 1, debug => $debug ); #07 like( $mods->next, qr't/sym_up/Changes'i, 'Followed sym link' ); #08 ok( !$mods->next, 'Nothing more' ); END { unlink 't/sym_up'; } File-Wildcard-0.11/lib/0000755000175000017500000000000011156505000013320 5ustar ivorivorFile-Wildcard-0.11/lib/File/0000755000175000017500000000000011156505000014177 5ustar ivorivorFile-Wildcard-0.11/lib/File/Wildcard.pm0000644000175000017500000005510011156504127016300 0ustar ivorivor package File::Wildcard; use strict; our $VERSION = '0.11'; =head1 NAME File::Wildcard - Enhanced glob processing =head1 SYNOPSIS use File::Wildcard; my $foo = File::Wildcard->new(path => "/home/me///core"); while (my $file = $foo->next) { unlink $file; } =head1 DESCRIPTION When looking at how various operating systems do filename wildcard expansion (globbing), VMS has a nice syntax which allows expansion and searching of whole directory trees. It would be nice if other operating systems had something like this built in. The best Unix can manage is through the utility program C. This module provides this facility to Perl. Whereas native VMS syntax uses the ellipsis "...", this will not fit in with POSIX filenames, as ... is a valid (though somewhat strange) filename. Instead, the construct "///" is used as this cannot syntactically be part of a filename, as you do not get three concurrent filename separators with nothing between (three slashes are used to avoid confusion with //node/path/name syntax). You don't have to use this syntax, as you can do the splitting yourself and pass in an arrayref as your path. The module also forms a B for the whole of the wildcard string, and binds a series of back references ($1, $2 etc.) which are available to construct new filenames. =head2 new Cnew( $wildcard, [,option => value,...]);> my $foo = File::Wildcard->new( path => "/home/me///core"); my $srcfnd = File::Wildcard->new( path => "src///*.cpp", match => qr(^src/(.*?)\.cpp$), derive => ['src/$1.o','src/$1.hpp']); This is the constructor for File::Wildcard objects. At a simple level, pass a single wildcard string as a path. For more complicated operations, you can supply your own match regexp, or use the derive option to specify regular expression captures to form the basis of other filenames that are constructed for you. The $srcfnd example gives you object files and header files corresponding to C++ source files. Here are the options that are available: =over 4 =item C This is the input parameter that specifies the range of files that will be looked at. This is a glob spec which can also contain the ellipsis '///' (it could contain more than one ellipsis, but the benefit of this is questionable, and multiple ellipsi would cause a performance hit). Note that the path can be relative or absolute. B will do the right thing, working out that a path starting with '/' is absolute. In order to recurse from the current directory downwards, specify './//foo'. As an alternative, you can supply an arrayref with the path constituents already split. If you do this, you need to tell B if the path is absolute. Include an empty string for an ellipsis. For example: 'foo///bar/*.c' is equivalent to ['foo','','bar','*.c'] You can also construct a File::Wildcard without a path. A call to B will return undef, but paths can be added using the append and prepend methods. =item C This is ignored unless you are using a pre split path. If you are passing a string as the path, B will work out whether the path is absolute or relative. Pass a true value for absolute paths. If your original filespec started with '/' before you split it, specify absolute => 1. B is not required for Windows if the path contains a drive specification, e.g. C:/foo/bar. =item C By default, the module will use L to determine whether the file system of your wildcard is defined. This is an optional module (see L), and File::Wildcard will guess at case sensitivity based on your operating system. This will not always be correct, as the file system might be VFAT mounted on Linux or ODS-5 on VMS. Specifying the option C explicitly forces this behaviour on the wildcard. Note that File::Wildcard will use the file system of the current working directory if the path is not absolute. If the path is absolute, you should specify the case_sensitivity option explicitly. =item C You can provide a regexp to apply to any generated paths, which will cause any matching paths not to be processed. If the root of a directory tree matches, no processing is done on the entire tree. This option can be useful for excluding version control repositories, e.g. exclude => qr/.svn/ =item C Optional. If you do not specify a regexp, you get all the files that match the glob; in addition, B will set up a regexp for you, to provide a capture for each wildcard used in the path. If you do provide a match parameter, this will be used instead, and will filter the results. =item C Supply an arrayref with a list of derived filenames, which will be constructed for each matching file. This causes B to return an arrayref instead of a scalar. =item C If given a true value indicates that symbolic links are to be followed. Otherwise, the symbolic link target itself is presented, but the ellipsis will not traverse the link. This module detects a looping symlink that points to a directory higher up, and will only present the tree once. =item C This can take one of the following values: normal, breadth-first, inside-out. The default option is normal. This controls how File::Wildcard handles the ellipsis. The default is a normal depth first search, presenting the name of each containing directory before the contents. The inside-out order presents the contents of directories first before the directory, which is useful when you want to remove files and directories (all O/S require directories to be empty before rmdir will work). See t/03_absolute.t as this uses inside-out order to tidy up after the test. Breadth-first is rarely needed (but I do have an application for it). Here, the whole directory contents is presented before traversing any subdirectories. Consider the following tree: a/ a/bar/ a/bar/drink a/foo/ a/foo/lish breadth-first will give the following order: qw(a/ a/bar/ a/foo/ a/bar/drink a/foo/lish). normal gives the order in which the files are listed. inside-out gives the following: qw(a/bar/drink a/bar/ a/foo/lish a/foo/ a/). =item C By default, globbing returns the list of files in the order in which they are returned by the dirhandle (internally). If you specify sort => 1, the files are sorted into ASCII sequence (case insensitively if we are operating that way). If you specify a CODEREF, this will be used as a comparison routine. Note that this takes its operands in @_, not in $a and $b. =item C and C You can enable a trace of the internal states of File::Wildcard by setting debug to a true value. Set debug_output to an open filehandle to get the trace in a file. If you are submitting bug reports for File::Wildcard, attaching debug trace files would be very useful. debug_output defaults to STDERR. =back =head2 match my $foo_re = $foo->match; $foo->match('bar/core'); This is a get and set method that gives access to the match regexp that the File::Wildcard object is using. It is possible to change the regex on the fly in the middle of a search (though I don't know why anyone would want to do this). =head2 append $foo->append(path => '/home/me///*.tmp'); appends a path to an object's todo list. This will be globbed after the object has finished processing the existing wildcards. =head2 prepend $srcfnd->prepend(path => $include_file); This is similar to append, but prepends the path to the todo list. In other words, the current wildcard operation is interrupted to serve the new path, then the previous wildcard operation is resumed when this is exhausted. =head2 next while (my $core = $foo->next) { unlink $core; } my ($src,$obj,$hdr) = @{$srcfnd->next}; The C method is an iterator, which returns successive files. Returns matching files if there was no derive option passed to new. If there was a derive option, returns an arrayref containing the matching filespec and all derived filespecs. The derived filespecs do not have to exist. Note that C maintains an internal cursor, which retains context and state information. Beware if the contents of directories are changing while you are iterating with next; you may get unpredictable results. If you are intending to change the contents of the directories you are scanning (with unlink or rename), you are better off deferring this operation until you have processed the whole tree. For the pending delete or rename operations, you could always use another File::Wildcard object - see the spike example below: =head2 all my @cores = $foo->all; C returns an array of matching files, in the simple case. Returns an array of arrays if you are constructing new filenames, like the $srcfnd example. Beware of the performance and memory implications of using C. The method will not return until it has read the entire directory tree. Use of the C method is not recommended for traversing large directory trees and whole file systems. Consider coding the traversal using the iterator C instead. =head2 reset C causes the wildcard context to be set to re-read the first filename again. Note that this will cause directory contents to be re-read. Note also that this will cause the path to revert to the original path specified to B. Any additional paths appended or prepended will be forgotten. =head2 close Release all directory handles associated with the File::Wildcard object. An object that has been closed will be garbage collected once it goes out of scope. Wildcards that have been exhausted are automatically closed, (i.e. C was used, or c returned undef). Subsequent calls to C will return undef. It is possible to call C after C on the same File::Wildcard object, which will cause it to be reopened. =head1 EXAMPLES =over 4 =item * B my $todo = File::Wildcard->new; ... $todo->append(path => $file); ... while (my $file = $todo->next) { ... } You can use an empty wildcard to store a list of filenames for later processing. The order in which they will be seen depends on whether append or prepend is used. =item * B my $wc_args = File::Wildcard->new; $wc_args->append(path => $_) for @ARGV; while ($wc_args->next) { ... } On Unix, file wildcards on the command line are globbed by the shell before perl sees them, unless the wildcards are escaped or quoted. This is not true of other operating systems. MS-DOS does no globbing at all for example. File::Wildcard gives you the bonus of elliptic globbing with '///'. =back =head1 CAVEAT This module takes POSIX filenames, which use forward slash '/' as a path separator. All operating systems that run Perl can manage this type of path. The module is not designed to work with B file specs. If you want to write code that is portable, convert native filespecs to the POSIX form. There is of course no difference on Unix platforms. =head1 BUGS Please report bugs to http://rt.cpan.org =head1 AUTHOR Ivor Williams ivorw-file-wildcard010 at xemaps.com =head1 COPYRIGHT This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The full text of the license can be found in the LICENSE file included with this module. =head1 SEE ALSO glob(3), L, L. =cut use Params::Validate::Dummy qw(); use Module::Optional qw(Params::Validate :all); package Filesys::Type::Dummy; use strict; sub case { return 'insensitive' if $^O =~ /win|dos/i; return 'lower' if $^O =~ /vms/i; return 'sensitive'; } package File::Wildcard; use Module::Optional qw(Filesys::Type); use File::Spec; use Carp; sub new { my $pkg = shift; my %par = validate( @_, { derive => 0, path => { type => SCALAR | ARRAYREF, optional => 1 }, follow => 0, absolute => 0, match => { type => SCALARREF, optional => 1 }, exclude => { type => SCALARREF, optional => 1 }, sort => { type => SCALAR | CODEREF | UNDEF, optional => 1 }, ellipsis_order => { type => SCALAR, regex => qr/(normal|breadth-first|inside-out)/, optional => 1, }, case_insensitive => { type => SCALAR, optional => 1 }, debug => { type => SCALAR, optional => 1 }, debug_output => 0, } ); $par{ellipsis_order} ||= 'normal'; my $path = $par{path}; # $par{path} is about to be chopped up ( $par{path}, $par{absolute} ) = $pkg->_split_path( @par{qw/path absolute follow/} ); if ( exists( $par{path} ) && !defined $par{case_insensitive} ) { my $fspath = $par{absolute} ? $path : File::Spec->curdir; my $fscase = eval { Filesys::Type::case($fspath) } || Filesys::Type::Dummy::case; $par{case_insensitive} = $fscase eq 'sensitive'; } $par{debug_output} ||= \*STDERR if $par{debug}; unless ( exists $par{match} ) { my $match_re = $par{absolute} ? '^/' : '^'; for ( @{ $par{path} } ) { my $comp = quotemeta $_; $comp =~ s!((?:\\\?)+)!'(.{'.(length($1)/2).'})'!eg; $comp =~ s!\\\*!([^/]*)!g; $match_re .= ( $comp || '(.*?)' ) . '/'; } $match_re =~ s!/$!\$!; $par{match} = $par{case_insensitive} ? qr/$match_re/i : qr/$match_re/; } bless \%par, $pkg; } sub _debug { my ( $self, $mess ) = @_; return unless $self->{debug}; my $dbug = $self->{debug_output}; print $dbug $mess; } sub next { my $self = shift; $self->_set_state( state => 'initial' ) unless exists $self->{state}; while ( !exists $self->{retval} ) { $self->_debug( "In state " . $self->{state} . "\n" ); my $method = "_state_" . $self->{state}; $self->$method; } $self->_debug( "Returned " . ( $self->{retval} || 'undef' ) . "\n" ); my $rv = $self->{retval}; delete $self->{retval}; $rv; } sub all { my $self = shift; my @out; while ( my $match = $self->next ) { push @out, $match; } @out; } sub close { my $self = shift; delete $self->{stack}; delete $self->{dir}; delete $self->{seen_symlink}; $self->_set_state( state => 'finished' ); } sub reset { my $self = shift; $self->close; $self->_set_state( state => 'initial' ); } sub _derived { my $self = shift; return $self->{resulting_path} unless exists $self->{derive}; my @out = ( $self->{resulting_path} ); my $re = $self->{match}; $self->{resulting_path} =~ /$re/; for ( @{ $self->{derive} } ) { push @out, eval(qq("$_")); } \@out; } sub match { my $self = shift; my ($new_re) = validate_pos( @_, { type => SCALARREF, optional => 1 } ); $new_re ? ( $self->{match} = $new_re ) : $self->{match}; } sub append { my $self = shift; my %par = validate( @_, { path => { type => SCALAR | ARRAYREF }, follow => 0, absolute => 0, } ); my %new; @new{qw/ path_remaining absolute follow /} = $self->_split_path( @par{qw/ path absolute follow /} ); $new{state} = 'nextdir'; $new{resulting_path} = $new{absolute} ? '/' : ''; unshift @{ $self->{state_stack} }, \%new; $self->_pop_state if !$self->{state} || ( $self->{state} eq 'finished' ); } sub prepend { my $self = shift; my %par = validate( @_, { path => { type => SCALAR | ARRAYREF }, follow => 0, absolute => 0, } ); $self->_push_state; my ( $pr, $abs, $fol ) = $self->_split_path( @par{qw/ path absolute follow /} ); $self->{path_remaining} = $pr; $self->{absolute} = $abs; $self->{follow} = $fol; $self->{resulting_path} = $self->{absolute} ? '/' : ''; $self->_set_state( state => 'nextdir' ); } sub _split_path { my $self = shift; my ( $path, $abs, $follow ) = validate_pos( @_, 0, 0, 0 ); return ( $path, $abs, $follow ) if !defined($path) || ref $path; $path =~ s!//!/!g; $abs = $path =~ s!^/!!; $path =~ s!^\./!/!; my @out = split m(/), $path, -1; #/ (syntax highlighting) shift @out if $out[0] eq ''; pop @out if $out[-1] eq ''; ( \@out, $abs, $follow ); } sub _set_state { my $self = shift; my %par = validate( @_, { state => { type => SCALAR }, dir => { type => GLOBREF | CODEREF, optional => 1 }, wildcard => 0, } ); $self->{$_} = $par{$_} for keys %par; } sub _push_state { my $self = shift; $self->_debug( "Push state: " . $self->{state} . " resulting_path: " . $self->{resulting_path} . " Wildcard: " . ( $self->{wildcard} || '' ) . " path_remaining: " . join( '/', @{ $self->{path_remaining} } ) . "\n" ); push @{ $self->{state_stack} }, { map { $_, ( ref( $self->{$_} ) eq 'ARRAY' ) ? [ @{ $self->{$_} } ] : $self->{$_} } qw/ state path_remaining dir resulting_path / }; } sub _pop_state { my $self = shift; $self->{state_stack} ||= []; my $newstate = @{ $self->{state_stack} } ? pop( @{ $self->{state_stack} } ) : { state => 'finished', dir => undef }; $self->{$_} = $newstate->{$_} for keys %$newstate; $self->_debug( "Pop state to " . $self->{state} . " resulting_path: " . $self->{resulting_path} . " Wildcard: " . ( $self->{wildcard} || '' ) . " path_remaining: " . join( '/', @{ $self->{path_remaining} } ) . "\n" ); } sub _state_initial { my $self = shift; $self->{resulting_path} = $self->{absolute} ? '/' : ''; $self->{path_remaining} = [ @{ $self->{path} } ]; $self->_set_state( state => 'nextdir' ); } sub _state_finished { my $self = shift; $self->{retval} = undef; # Autovivification optimises this away :( } sub _state_nextdir { my $self = shift; unless ( @{ $self->{path_remaining} } ) { $self->_debug("Exhaused path\n"); my $re = $self->{match}; $self->{retval} = $self->_derived if ( -e $self->{resulting_path} ) && ( $self->{resulting_path} =~ /$re/ ); $self->_pop_state; return; } my $pathcomp = shift @{ $self->{path_remaining} }; $self->_debug("Path component '$pathcomp'\n"); if ( $pathcomp eq '' ) { my $order = $self->{ellipsis_order}; $self->_set_state( state => ( $order eq 'inside-out' ) ? 'nextdir' : 'ellipsis' ); if ( $order ne 'breadth-first' ) { $self->_push_state; $self->_set_state( state => ( $order eq 'inside-out' ) ? 'ellipsis' : 'nextdir' ); } } elsif ( $pathcomp !~ /\?|\*/ ) { $self->{resulting_path} .= $pathcomp; my $rp = $self->{resulting_path}; if ( exists( $self->{exclude} ) && $rp =~ /$self->{exclude}/ ) { $self->_pop_state; return; } my $sl = readlink $rp; if ($sl) { my $slpath = File::Spec->rel2abs( $sl, $rp ); if ( exists $self->{seen_symlink}{$slpath} ) { $self->_pop_state; return; } $self->{seen_symlink}{$slpath}++; $self->{path_remaining} = [] unless $self->{follow}; } $self->{resulting_path} .= '/' if -d $self->{resulting_path}; } else { my $wcdir; if ( !opendir $wcdir, $self->{resulting_path} || '.' ) { $self->_pop_state; return; } my $wc_re = quotemeta $pathcomp; $wc_re =~ s!((?:\\\?)+)!'(.{'.(length($1)/2).'})'!eg; $wc_re =~ s!\\\*!([^/]*)!g; my %newstate = ( state => 'wildcard', dir => $wcdir, wildcard => $self->{case_insensitive} ? qr(^$wc_re$)i : qr(^$wc_re$) ); if ( $self->{sort} ) { my @wcmatch = grep { ( $_ ne '.' ) && ( $_ ne '..' ) && ( $self->{case_insensitive} ? /$wc_re/i : /$wc_re/ ) } readdir($wcdir); if ( $^O =~ /vms/i ) { s/\.dir$// for @wcmatch; } @wcmatch = ( ref( $self->{sort} ) eq 'CODE' ) ? ( sort { &{ $self->{sort} }( $a, $b ) } @wcmatch ) : $self->{case_insensitive} ? ( sort { lc($a) cmp lc($b) } @wcmatch ) : ( sort @wcmatch ); if ( $self->{exclude} ) { @wcmatch = grep { ( $self->{path_remaining} . $_ ) !~ /$self->{exclude}/ } @wcmatch; } $newstate{state} = 'wildcard_sorted'; $newstate{dir} = sub { my $fil = ( shift @wcmatch ) || ''; my $rem = join ' ', @wcmatch; $self->_debug("wildcard_sorted yields $fil remaining $rem\n"); return $fil; }; } $self->_set_state(%newstate); } } sub _state_wildcard { my $self = shift; my $fil = '.'; my $re = $self->{wildcard}; while ( ( $fil eq '.' ) || ( $fil eq '..' ) || ( $fil !~ /$re/ ) || ( exists( $self->{exclude} ) && ( $self->{resulting_path} . $fil =~ /$self->{exclude}/ ) ) ) { $fil = readdir $self->{dir}; return $self->_pop_state unless defined $fil; } $fil =~ s/.dir$// if $^O =~ /vms/i; $self->_push_state; unshift @{ $self->{path_remaining} }, $fil; $self->_set_state( state => 'nextdir' ); } sub _state_wildcard_sorted { my $self = shift; my $fil = &{ $self->{dir} }; return $self->_pop_state unless $fil; $self->_push_state; unshift @{ $self->{path_remaining} }, $fil; $self->_set_state( state => 'nextdir' ); } sub _state_ellipsis { my $self = shift; if ( $self->{ellipsis_order} eq 'breadth-first' ) { unshift @{ $self->{path_remaining} }, '*', ''; $self->_set_state( state => 'nextdir' ); $self->_push_state; splice @{ $self->{path_remaining} }, 1, 1; } else { unshift @{ $self->{path_remaining} }, '*', ''; $self->_set_state( state => 'nextdir' ); } } 1; #this line is important and will help the module return a true value __END__ File-Wildcard-0.11/lib/File/Wildcard/0000755000175000017500000000000011156505000015730 5ustar ivorivorFile-Wildcard-0.11/lib/File/Wildcard/Find.pm0000644000175000017500000000265610502761506017170 0ustar ivorivorpackage File::Wildcard::Find; use strict; BEGIN { use Exporter (); use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $finder); $VERSION = '0.01'; @ISA = qw(Exporter); @EXPORT = qw(findbegin findnext findall); @EXPORT_OK = qw(findbegin findnext findall $finder); %EXPORT_TAGS = ( all => \@EXPORT_OK ); } use File::Wildcard; sub findbegin { $finder = File::Wildcard->new( path => shift ); } sub findnext { $finder->next; } sub findall { my $allfinder = File::Wildcard->new( path => shift ); $allfinder->all; } 1; __END__ =head1 NAME File::Wildcard::Find - Simple interface to File::Wildcard =head1 SYNOPSIS use File::Wildcard::Find; findbegin( "/home/me///core"); while (my $file = findnext()) { unlink $file; } =head1 DESCRIPTION L provides a comprehensive object interface that allows you to do powerful processing with wildcards. Some consider this too unwieldy for simple tasks. The module File::Wildcard::Find provides a straightforward interface. Only a single wildcard stream is accessible, but this should be sufficient for one liners and simple applications. =head1 FUNCTIONS =head2 findbegin This takes 1 parameter, a path with wildcards as a string. See L for details of what can be passed. =head2 findnext Iterator that returns successive matches, then undef. =head2 findall Returns a list of all matches File-Wildcard-0.11/LICENSE0000644000175000017500000005010110502757740013572 0ustar ivorivorTerms of Perl itself a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or b) the "Artistic License" --------------------------------------------------------------------------- The General Public License (GPL) Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc. 675 Mass Ave, Cambridge, MA 02139, USA. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Library General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS --------------------------------------------------------------------------- The Artistic License Preamble The intent of this document is to state the conditions under which a Package may be copied, such that the Copyright Holder maintains some semblance of artistic control over the development of the package, while giving the users of the package the right to use and distribute the Package in a more-or-less customary fashion, plus the right to make reasonable modifications. Definitions: - "Package" refers to the collection of files distributed by the Copyright Holder, and derivatives of that collection of files created through textual modification. - "Standard Version" refers to such a Package if it has not been modified, or has been modified in accordance with the wishes of the Copyright Holder. - "Copyright Holder" is whoever is named in the copyright or copyrights for the package. - "You" is you, if you're thinking about copying or distributing this Package. - "Reasonable copying fee" is whatever you can justify on the basis of media cost, duplication charges, time of people involved, and so on. (You will not be required to justify it to the Copyright Holder, but only to the computing community at large as a market that must bear the fee.) - "Freely Available" means that no fee is charged for the item itself, though there may be fees involved in handling the item. It also means that recipients of the item may redistribute it under the same conditions they received it. 1. You may make and give away verbatim copies of the source form of the Standard Version of this Package without restriction, provided that you duplicate all of the original copyright notices and associated disclaimers. 2. You may apply bug fixes, portability fixes and other modifications derived from the Public Domain or from the Copyright Holder. A Package modified in such a way shall still be considered the Standard Version. 3. You may otherwise modify your copy of this Package in any way, provided that you insert a prominent notice in each changed file stating how and when you changed that file, and provided that you do at least ONE of the following: a) place your modifications in the Public Domain or otherwise make them Freely Available, such as by posting said modifications to Usenet or an equivalent medium, or placing the modifications on a major archive site such as ftp.uu.net, or by allowing the Copyright Holder to include your modifications in the Standard Version of the Package. b) use the modified Package only within your corporation or organization. c) rename any non-standard executables so the names do not conflict with standard executables, which must also be provided, and provide a separate manual page for each non-standard executable that clearly documents how it differs from the Standard Version. d) make other distribution arrangements with the Copyright Holder. 4. You may distribute the programs of this Package in object code or executable form, provided that you do at least ONE of the following: a) distribute a Standard Version of the executables and library files, together with instructions (in the manual page or equivalent) on where to get the Standard Version. b) accompany the distribution with the machine-readable source of the Package with your modifications. c) accompany any non-standard executables with their corresponding Standard Version executables, giving the non-standard executables non-standard names, and clearly documenting the differences in manual pages (or equivalent), together with instructions on where to get the Standard Version. d) make other distribution arrangements with the Copyright Holder. 5. You may charge a reasonable copying fee for any distribution of this Package. You may charge any fee you choose for support of this Package. You may not charge a fee for this Package itself. However, you may distribute this Package in aggregate with other (possibly commercial) programs as part of a larger (possibly commercial) software distribution provided that you do not advertise this Package as a product of your own. 6. The scripts and library files supplied as input to or produced as output from the programs of this Package do not automatically fall under the copyright of this Package, but belong to whomever generated them, and may be sold commercially, and may be aggregated with this Package. 7. C or perl subroutines supplied by you and linked into this Package shall not be considered part of this Package. 8. Aggregation of this Package with a commercial distribution is always permitted provided that the use of this Package is embedded; that is, when no overt attempt is made to make this Package's interfaces visible to the end user of the commercial distribution. Such use shall not be construed as a distribution of this Package. 9. The name of the Copyright Holder may not be used to endorse or promote products derived from this software without specific prior written permission. 10. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. The End File-Wildcard-0.11/README0000644000175000017500000003026110565422631013447 0ustar ivorivorNAME File::Wildcard - Enhanced glob processing SYNOPSIS use File::Wildcard; my $foo = File::Wildcard->new(path => "/home/me///core"); while (my $file = $foo->next) { unlink $file; } DESCRIPTION When looking at how various operating systems do filename wildcard expansion (globbing), VMS has a nice syntax which allows expansion and searching of whole directory trees. It would be nice if other operating systems had something like this built in. The best Unix can manage is through the utility program "find". This module provides this facility to Perl. Whereas native VMS syntax uses the ellipsis "...", this will not fit in with POSIX filenames, as ... is a valid (though somewhat strange) filename. Instead, the construct "///" is used as this cannot syntactically be part of a filename, as you do not get three concurrent filename separators with nothing between (three slashes are used to avoid confusion with //node/path/name syntax). You don't have to use this syntax, as you can do the splitting yourself and pass in an arrayref as your path. The module also forms a regular expression for the whole of the wildcard string, and binds a series of back references ($1, $2 etc.) which are available to construct new filenames. new "File::Wildcard-"new( $wildcard, [,option => value,...]);> my $foo = File::Wildcard->new( path => "/home/me///core"); my $srcfnd = File::Wildcard->new( path => "src///*.cpp", match => qr(^src/(.*?)\.cpp$), derive => ['src/$1.o','src/$1.hpp']); This is the constructor for File::Wildcard objects. At a simple level, pass a single wildcard string as a path. For more complicated operations, you can supply your own match regexp, or use the derive option to specify regular expression captures to form the basis of other filenames that are constructed for you. The $srcfnd example gives you object files and header files corresponding to C++ source files. Here are the options that are available: "path" This is the input parameter that specifies the range of files that will be looked at. This is a glob spec which can also contain the ellipsis '///' (it could contain more than one ellipsis, but the benefit of this is questionable, and multiple ellipsi would cause a performance hit). Note that the path can be relative or absolute. new will do the right thing, working out that a path starting with '/' is absolute. In order to recurse from the current directory downwards, specify './//foo'. As an alternative, you can supply an arrayref with the path constituents already split. If you do this, you need to tell new if the path is absolute. Include an empty string for an ellipsis. For example: 'foo///bar/*.c' is equivalent to ['foo','','bar','*.c'] You can also construct a File::Wildcard without a path. A call to next will return undef, but paths can be added using the append and prepend methods. "absolute" This is ignored unless you are using a pre split path. If you are passing a string as the path, new will work out whether the path is absolute or relative. Pass a true value for absolute paths. If your original filespec started with '/' before you split it, specify absolute => 1. absolute is not required for Windows if the path contains a drive specification, e.g. C:/foo/bar. "case_insensitive" By default, the module will use Filesys::Type to determine whether the file system of your wildcard is defined. This is an optional module (see Module::Optional), and File::Wildcard will guess at case sensitivity based on your operating system. This will not always be correct, as the file system might be VFAT mounted on Linux or ODS-5 on VMS. Specifying the option "case_insensitive" explicitly forces this behaviour on the wildcard. Note that File::Wildcard will use the file system of the current working directory if the path is not absolute. If the path is absolute, you should specify the case_sensitivity option explicitly. "exclude" You can provide a regexp to apply to any generated paths, which will cause any matching paths not to be processed. If the root of a directory tree matches, no processing is done on the entire tree. This option can be useful for excluding version control repositories, e.g. exclude => qr/.svn/ "match" Optional. If you do not specify a regexp, you get all the files that match the glob; in addition, new will set up a regexp for you, to provide a capture for each wildcard used in the path. If you do provide a match parameter, this will be used instead, and will filter the results. "derive" Supply an arrayref with a list of derived filenames, which will be constructed for each matching file. This causes next to return an arrayref instead of a scalar. "follow" If given a true value indicates that symbolic links are to be followed. Otherwise, the symbolic link target itself is presented, but the ellipsis will not traverse the link. This module detects a looping symlink that points to a directory higher up, and will only present the tree once. "ellipsis_order" This can take one of the following values: normal, breadth-first, inside-out. The default option is normal. This controls how File::Wildcard handles the ellipsis. The default is a normal depth first search, presenting the name of each containing directory before the contents. The inside-out order presents the contents of directories first before the directory, which is useful when you want to remove files and directories (all O/S require directories to be empty before rmdir will work). See t/03_absolute.t as this uses inside-out order to tidy up after the test. Breadth-first is rarely needed (but I do have an application for it). Here, the whole directory contents is presented before traversing any subdirectories. Consider the following tree: a/ a/bar/ a/bar/drink a/foo/ a/foo/lish breadth-first will give the following order: qw(a/ a/bar/ a/foo/ a/bar/drink a/foo/lish). normal gives the order in which the files are listed. inside-out gives the following: qw(a/bar/drink a/bar/ a/foo/lish a/foo/ a/). "sort" By default, globbing returns the list of files in the order in which they are returned by the dirhandle (internally). If you specify sort => 1, the files are sorted into ASCII sequence (case insensitively if we are operating that way). If you specify a CODEREF, this will be used as a comparison routine. Note that this takes its operands in @_, not in $a and $b. "debug" and "debug_output" You can enable a trace of the internal states of File::Wildcard by setting debug to a true value. Set debug_output to an open filehandle to get the trace in a file. If you are submitting bug reports for File::Wildcard, attaching debug trace files would be very useful. debug_output defaults to STDERR. match my $foo_re = $foo->match; $foo->match('bar/core'); This is a get and set method that gives access to the match regexp that the File::Wildcard object is using. It is possible to change the regex on the fly in the middle of a search (though I don't know why anyone would want to do this). append $foo->append(path => '/home/me///*.tmp'); appends a path to an object's todo list. This will be globbed after the object has finished processing the existing wildcards. prepend $srcfnd->prepend(path => $include_file); This is similar to append, but prepends the path to the todo list. In other words, the current wildcard operation is interrupted to serve the new path, then the previous wildcard operation is resumed when this is exhausted. next while (my $core = $foo->next) { unlink $core; } my ($src,$obj,$hdr) = @{$srcfnd->next}; The "next" method is an iterator, which returns successive files. Returns matching files if there was no derive option passed to new. If there was a derive option, returns an arrayref containing the matching filespec and all derived filespecs. The derived filespecs do not have to exist. Note that "next" maintains an internal cursor, which retains context and state information. Beware if the contents of directories are changing while you are iterating with next; you may get unpredictable results. If you are intending to change the contents of the directories you are scanning (with unlink or rename), you are better off deferring this operation until you have processed the whole tree. For the pending delete or rename operations, you could always use another File::Wildcard object - see the spike example below: all my @cores = $foo->all; "all" returns an array of matching files, in the simple case. Returns an array of arrays if you are constructing new filenames, like the $srcfnd example. Beware of the performance and memory implications of using "all". The method will not return until it has read the entire directory tree. Use of the "all" method is not recommended for traversing large directory trees and whole file systems. Consider coding the traversal using the iterator "next" instead. reset "reset" causes the wildcard context to be set to re-read the first filename again. Note that this will cause directory contents to be re-read. Note also that this will cause the path to revert to the original path specified to new. Any additional paths appended or prepended will be forgotten. close Release all directory handles associated with the File::Wildcard object. An object that has been closed will be garbage collected once it goes out of scope. Wildcards that have been exhausted are automatically closed, (i.e. "all" was used, or c returned undef). Subsequent calls to "next" will return undef. It is possible to call "reset" after "close" on the same File::Wildcard object, which will cause it to be reopened. EXAMPLES * The spike my $todo = File::Wildcard->new; ... $todo->append(path => $file); ... while (my $file = $todo->next) { ... } You can use an empty wildcard to store a list of filenames for later processing. The order in which they will be seen depends on whether append or prepend is used. * Shell style globbing my $wc_args = File::Wildcard->new; $wc_args->append(path => $_) for @ARGV; while ($wc_args->next) { ... } On Unix, file wildcards on the command line are globbed by the shell before perl sees them, unless the wildcards are escaped or quoted. This is not true of other operating systems. MS-DOS does no globbing at all for example. File::Wildcard gives you the bonus of elliptic globbing with '///'. CAVEAT This module takes POSIX filenames, which use forward slash '/' as a path separator. All operating systems that run Perl can manage this type of path. The module is not designed to work with native file specs. If you want to write code that is portable, convert native filespecs to the POSIX form. There is of course no difference on Unix platforms. BUGS Please report bugs to http://rt.cpan.org AUTHOR Ivor Williams ivorw-file-wildcard010 at xemaps.com COPYRIGHT This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The full text of the license can be found in the LICENSE file included with this module. SEE ALSO glob(3), File::Find, File::Find::Rule. File-Wildcard-0.11/Changes0000644000175000017500000000262211156503327014061 0ustar ivorivorRevision history for Perl module File::Wildcard 0.11 Fri Mar 13 2009 Stop warnings and errors for readdir looking under files. 0.10 Fri Feb 16 2007 Fix append absolute bug & add new test for it 0.09 Sat Sep 18 2006 Fix false positive in t/04_append on solaris (sort order not predictable) 0.08 Mon Apr 17 2006 New simple interface File::Wildcard::Find 0.07 Sat Dec 31 2005 Fixed bug: appending absolute paths didn't work New option: exclude for tree pruning 0.06 Sun Sep 25 2005 Reviewed kwalitee and coverage. Added pod and pod_coverage tests. Case sensitivity: use Filesys::Type as an optional module. Debug logging to a file handle. 0.05 Fri Sep 9 2005 Changed to use Module::Optional to remove the dependency on Params::Validate. Fixed symlink support, and added 'follow' parameter. 0.04 Thu Jul 29 2004 Added functionality to sort directories, and to process the ellipsis in different orders. Fix VMS: dirhandles return .dir on the end of each directory. Make tests able to deal with case insensitive O/S. 0.03 Sun Jul 11 2004 A major revision of the API. Instead of the user supplying a regex as 'path', the user supplies a globbing string. Added append and prepend methods. 0.02 Thu Jun 25 2004 Fixed errors in the POD 0.01 Wed Jun 24 2004 Initial version. Works on Unix. File-Wildcard-0.11/Makefile.PL0000644000175000017500000000135311156504016014534 0ustar ivorivoruse ExtUtils::MakeMaker; # See lib/ExtUtils/MakeMaker.pm for details of how to influence # the contents of the Makefile that is written. eval "require Params::Validate"; print < 'File::Wildcard', VERSION_FROM => 'lib/File/Wildcard.pm', # finds $VERSION AUTHOR => 'I. Williams (ivorw-file-wildcard at xemaps.com)', ABSTRACT_FROM => 'lib/File/Wildcard.pm', PREREQ_PM => { 'Module::Optional' => 0, }, LICENSE => 'perl', ); File-Wildcard-0.11/META.yml0000644000175000017500000000070611156505000014026 0ustar ivorivor--- #YAML:1.0 name: File-Wildcard version: 0.11 abstract: Enhanced glob processing license: perl author: - I. Williams (ivorw-file-wildcard at xemaps.com) generated_by: ExtUtils::MakeMaker version 6.44 distribution_type: module requires: Module::Optional: 0 meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.3.html version: 1.3 File-Wildcard-0.11/MANIFEST0000644000175000017500000000042710502762504013716 0ustar ivorivorMANIFEST LICENSE README Changes lib/File/Wildcard.pm lib/File/Wildcard/Find.pm t/01_basic.t t/02_derived.t t/03_absolute.t t/04_append.t t/05_symlink.t t/06_fwf.t t/pod.t t/pod_coverage.t Makefile.PL META.yml Module meta-data (added by MakeMaker)