Dir-Iterate
view release on metacpan or search on metacpan
lib/Dir/Iterate.pm view on Meta::CPAN
use File::Spec;
=item mapdir { ... } $path1[, $path2...]
The block is called for each file, folder, or other filesystem entity under the
given path(s). The full path to the object is in $_. The return value or
values of the block are collected together and returned in a list.
=cut
sub mapdir(&@) {
my($closure, @paths) = @_;
my @results;
File::Find::find(
{
wanted => sub {
local $_ = $File::Find::fullname;
push @results, $closure->();
},
lib/Dir/Iterate.pm view on Meta::CPAN
}
=item grepdir { ... } $path1[, $path2...]
The block is called for each file, folder, or other filesystem entity under the
given path(s). The full path to the object is in $_. If the return value of
the block is true, the full path will be in the list returned by the method.
=cut
sub grepdir(&@) {
my $predicate = shift;
unshift @_, sub { $predicate->() ? $_ : () };
goto &mapdir;
}
=back 4
=head1 EXPORTS
C<mapdir> and C<grepdir> by default.
t/002_mapdir.t view on Meta::CPAN
use strict;
use warnings;
use Test::More;
use Dir::Iterate;
sub test_it(&$);
sub load_manifest;
my $num_tests = 4;
my @manifest = load_manifest;
plan tests => $num_tests * @manifest + 1;
ok(scalar @manifest, "Prepping: got the manifest");
test_it { 1 } "Single value";
test_it { 0, 1 } "Multiple values";
test_it { () } "No value";
test_it { -e } "Getting files";
sub test_it(&$) {
my($block, $description) = @_;
my @dir_results = mapdir { $block->() } '.';
my @reg_results = map { $block->() } @manifest;
my %dir_results = map { $_ => 1 } @dir_results;
my %reg_results = map { $_ => 1 } @reg_results;
for my $file(@manifest) {
is(
t/003_grepdir.t view on Meta::CPAN
use strict;
use warnings;
use Test::More;
use Dir::Iterate;
sub test_it(&$);
sub load_manifest;
my $num_tests = 5;
my @manifest = load_manifest;
plan tests => $num_tests * @manifest + 1;
ok(scalar @manifest, "Prepping: got the manifest");
test_it { 1 } "All";
test_it { 0 } "None";
test_it { -s % 2 } "Mix";
test_it { -d } "Is directory";
test_it { -f } "Is file";
sub test_it(&$) {
my($pred, $description) = @_;
my @dir_results = grepdir { $pred->() } '.';
my @reg_results = grep { $pred->() } @manifest;
my %dir_results = map { $_ => 1 } @dir_results;
my %reg_results = map { $_ => 1 } @reg_results;
for my $file(@manifest) {
is(
( run in 0.237 second using v1.01-cache-2.11-cpan-49f99fa48dc )