Cn-Util
view release on metacpan or search on metacpan
lib/Cn/Util.pm view on Meta::CPAN
die "on_match must be a code reference"
if defined $args{on_match} && ref $args{on_match} ne 'CODE';
die "Unrecognized characters in tests: $args{tests}"
if $args{tests} =~ /[^rwxoRWXOezsfdlpSbcugkTB]/;
die "cpu must be a positive integer, got '$args{cpu}'"
unless $args{cpu} =~ /\A[1-9][0-9]*\z/;
my $re = eval { qr/$args{regex}/ }
or die "Invalid regex '$args{regex}': $@";
# Compile the tests once. The first test stats the path; every later test
# reuses the stat buffer via the special '_' filehandle, so N tests cost
# one stat(2) syscall instead of N (~2.4x faster for 4 tests).
# -l needs lstat, so it is hoisted to the front, and the test after it
# must re-stat the real path (otherwise '_' holds lstat data, or -l _
# would be a fatal error).
my @t = split //, $args{tests};
@t = ((grep { $_ eq 'l' } @t), (grep { $_ ne 'l' } @t));
my @parts;
my $fresh = 1; # 1 = the next test must stat the path itself rather than reuse '_'
for my $t (@t) {
if ($t eq 'l') { push @parts, '-l $_[0]'; $fresh = 1 }
elsif ($fresh) { push @parts, "-$t \$_[0]"; $fresh = 0 }
lib/Cn/Util.pm view on Meta::CPAN
if ($args{recursive}) {
# Walk everything strictly below $start, depth first, handing each
# match to $sink. _dir_level returns a directory's subdirectories
# without the symlinks among them, so the walk does not follow links;
# but opendir follows $start itself, so a dir that is a symlink to a
# directory is listed through the link.
#
# File::Find did this walk up to 0.024. Over ~/perl5 (279,000
# entries, warm cache, 2026-11-09) it took 1.06 s of user CPU to this
# loop's 0.43 s, for the same system time: the two make the same stat
# and lstat calls. It also lstat()ed its starting path, so a symlinked
# dir needed listing by hand before it, and on Windows it rewrote the
# separators of that path, which the "\" conversion above is for.
my $find = sub {
my ($start, $sink) = @_;
my @todo = ($start);
push @todo, _dir_level(pop @todo, $re, $tester, $join, $sink) while @todo;
return;
};
if ($args{cpu} == 1) {
$find->($target_dir, $emit);
( run in 3.067 seconds using v1.01-cache-2.11-cpan-eb134014566 )