【发布时间】:2012-02-07 16:38:09
【问题描述】:
我正在构建一个依赖 Moose 的 Perl 应用程序。 Moose 对象需要完成的一项任务是使用 File::Find 用文件列表填充属性。我在弄清楚如何使用 find 的 \&wanted 代码引用以使我能够保持对 Moose 对象的 $self 版本的访问时遇到了一些麻烦。
到目前为止,我有这个:
#!/usr/bin/perl
package MyMoose;
use Modern::Perl;
use Moose;
use File::Find;
use FindBin qw($Bin);
### Attribute to hold the file list
has "file_list" => (
is => 'rw',
isa => 'ArrayRef',
default => sub {[]}
);
### Method to populate the file list
sub update_file_list {
my $self = shift;
find(\&wanted, $Bin);
}
### File::Find wanted sub
sub wanted {
### This won't work, but shows what I'd like to do
# my $self = shift;
# ### Do some filtering
# push @{$self->file_list}, $File::Find::name;
}
1;
######################################################################
### Main package to test the object.
package main;
use Data::Dumper;
run_main() unless caller();
sub run_main {
my $m = MyMoose->new();
$m->update_file_list();
print Dumper $m->file_list;
}
它运行,但显然没有组合文件列表。这就是我想要弄清楚的部分。
使用 File::Find 的正确方法是什么,以便在处理过程中访问 Moose 对象?
【问题讨论】:
-
与您的问题没有直接关系,但您可能想看看the
recursemethod inPath::Class::Dir,它可以用来代替File::Find。