Perl 使用File::Find模块时没有输出

Perl 使用File::Find模块时没有输出,perl,closures,subroutine,Perl,Closures,Subroutine,我想计算modify\u time介于$atime和$btime之间的文件总数。这是我代码的一部分,但它没有返回任何内容。怎么了 sub mtime_between { my $mtime=0; my $counts=0; $mtime = (stat $File::Find::name)[9] if -f $File::Find::name; if ($mtime > $atime and $mtime < $btime) { ret

我想计算
modify\u time
介于
$atime
$btime
之间的文件总数。这是我代码的一部分,但它没有返回任何内容。怎么了

sub mtime_between {
    my $mtime=0;
    my $counts=0;
    $mtime = (stat $File::Find::name)[9] if -f $File::Find::name;
    if ($mtime > $atime and $mtime < $btime) {
        return sub { print ++$counts,"$File::Find::name\n"};
    }

您不应该返回函数

检查

find()
按给定的顺序对给定的
@目录进行深度优先搜索。对于找到的每个文件或目录,它调用
&want
子例程

在想要的功能中,你应该直接做你想做的事情。返回函数引用将不起作用,这就是您遇到问题的原因

所以你实际上想要更像:

#!/usr/bin/env perl

use strict;
use warnings;
use feature qw{say};
use File::Find;
use Data::Dumper;

my ($atime, $btime) = (1461220840, 1561220844);

sub findFilesEditedBetweenTimestamps {
    my ($atime, $btime, $path) = @_;
    my $count = 0;
    my @files = ();

    my $mtime_between = sub {
        my $mtime = 0;
        $mtime = (stat $File::Find::name)[9] if -f $File::Find::name;
        if ($mtime > $atime and $mtime < $btime) {
            push @files, $File::Find::name;
            $count++;
        }
        return;
    };

    find ($mtime_between, $path);

    say "Found a total of $count files";
    say "Files:";
    print Dumper(@files);
}

findFilesEditedBetweenTimestamps($atime, $btime, "./");

您不应该返回函数

检查

find()
按给定的顺序对给定的
@目录进行深度优先搜索。对于找到的每个文件或目录,它调用
&want
子例程

在想要的功能中,你应该直接做你想做的事情。返回函数引用将不起作用,这就是您遇到问题的原因

所以你实际上想要更像:

#!/usr/bin/env perl

use strict;
use warnings;
use feature qw{say};
use File::Find;
use Data::Dumper;

my ($atime, $btime) = (1461220840, 1561220844);

sub findFilesEditedBetweenTimestamps {
    my ($atime, $btime, $path) = @_;
    my $count = 0;
    my @files = ();

    my $mtime_between = sub {
        my $mtime = 0;
        $mtime = (stat $File::Find::name)[9] if -f $File::Find::name;
        if ($mtime > $atime and $mtime < $btime) {
            push @files, $File::Find::name;
            $count++;
        }
        return;
    };

    find ($mtime_between, $path);

    say "Found a total of $count files";
    say "Files:";
    print Dumper(@files);
}

findFilesEditedBetweenTimestamps($atime, $btime, "./");

如前所述,
所需
子例程返回的值被忽略。对于某些人来说,从回调返回回调可能太远了

这可能很有趣。我已经使用该模块使修改时间的提取更具可读性,因此
$atime
$btime
可以用可读字符串而不是历元值表示

除非您愿意,否则无需为
wanted
函数编写单独的子例程——您可以在
find
调用中使用匿名子例程。如果节点不是文件,则最简单的方法是从
所需的
子例程
返回

#!/usr/bin/env perl

use strict;
use warnings 'all';

use File::Find;
use File::stat;
use Time::Piece;

sub count_files_between_times {

    my ($from, $to, $path) = @_;
    my $count = 0;

    find(sub {
        my $st = stat($_) or die $!;
        return unless -f $st;
        my $mtime = $st->mtime;
        ++$count if $mtime >= $fromand $mtime <= $to;
    }, $path);

    print "Found a total of $count files\n";
}

my ($from, $to) = map {
    Time::Piece->strptime($_, '%Y-%m-%dT%H:%M:%S')->epoch;
} '2016-04-19T00:00:00', '2019-04-22T00:00:00';

count_files_between_times($from, $to, '/usr');
或者,如果您愿意,您可以一次添加一条语句

use File::Find::Rule ();

sub count_files_between_times {

    my ($from, $to, $path) = @_;

    my $rule = File::Find::Rule->new;
    $rule->file;
    $rule->mtime(">= $from");
    $rule->mtime("<= $to");
    my @files = $rule->in($path);

    printf "Found a total of %d files\n", scalar @files;
}
使用File::Find::Rule();
次计数\u次之间的文件\u{
my($from,$to,$path)=@;
my$rule=文件::查找::规则->新建;
$rule->file;
$rule->mtime(“>=$from”);

$rule->mtime(“如前所述,
所需
子例程返回的值被忽略。从回调返回回调可能对某些人来说太远了

这可能很有趣。我使用了该模块使修改时间的提取更具可读性,因此
$atime
$btime
可以用可读字符串而不是历元值表示

除非您愿意,否则无需为
想要的
函数编写单独的子例程——您可以在
查找
调用中使用匿名子例程。如果节点不是文件,则最简单的方法是从
想要的
子例程中
返回

#!/usr/bin/env perl

use strict;
use warnings 'all';

use File::Find;
use File::stat;
use Time::Piece;

sub count_files_between_times {

    my ($from, $to, $path) = @_;
    my $count = 0;

    find(sub {
        my $st = stat($_) or die $!;
        return unless -f $st;
        my $mtime = $st->mtime;
        ++$count if $mtime >= $fromand $mtime <= $to;
    }, $path);

    print "Found a total of $count files\n";
}

my ($from, $to) = map {
    Time::Piece->strptime($_, '%Y-%m-%dT%H:%M:%S')->epoch;
} '2016-04-19T00:00:00', '2019-04-22T00:00:00';

count_files_between_times($from, $to, '/usr');
或者,如果您愿意,您可以一次添加一条语句

use File::Find::Rule ();

sub count_files_between_times {

    my ($from, $to, $path) = @_;

    my $rule = File::Find::Rule->new;
    $rule->file;
    $rule->mtime(">= $from");
    $rule->mtime("<= $to");
    my @files = $rule->in($path);

    printf "Found a total of %d files\n", scalar @files;
}
使用File::Find::Rule();
次计数\u次之间的文件\u{
my($from,$to,$path)=@;
my$rule=文件::查找::规则->新建;
$rule->file;
$rule->mtime(“>=$from”);

$rule->mtime(“我可能错了,但我认为你不能用find中的通缉子例程的返回值做任何事情。你需要在通缉子例程中做一些事情。要么向全局数组添加一些东西,要么打印一些东西,要么删除一些东西,随便什么。我可能错了,但我认为你不能用通缉子例程的返回值做任何事情查找中的ubroutin。你需要在你想要的子例程中执行一些操作。要么向全局数组中添加一些内容,要么打印一些内容,要么删除一些内容,不管是什么。thx,如果我只想得到不是每个文件的总文件数。如何操作?编辑,你需要回调中的一个变量来计算总文件数。thx,如果我只想得到总文件数不是每个文件的所有文件数。如何做?编辑后,您需要从回调中取出一个变量来计算总数。