perl - 如何将参数传递给处理每个文件的 File::Find 子例程?

标签 perl file-find

使用 File::Find ,如何将参数传递给处理每个文件的函数?

我有一个遍历目录的 Perl 脚本,以便转换一些 3 channel TIFF JPEG 文件(每个 TIFF 文件 3 个 JPEG 文件)。这可行,但我想将一些参数传递给处理每个文件的函数(缺少使用全局变量)。

这是我尝试传递参数的脚本的相关部分:

use File::Find;

sub findFiles
{
    my $IsDryRun2 = ${$_[0]}{anInIsDryRun2};
}

find ( { wanted => \&findFiles, anInIsDryRun2 => $isDryRun }, $startDir);
$isDryRun是一个标量。 $startDir是一个字符串,一个目录的完整路径。
$IsDryRun2未设置:

Use of uninitialized value $IsDryRun2 in concatenation (.) or string at TIFFconvert.pl line 197 (#1) (W uninitialized) An undefined value was used as if it were already defined. It was interpreted as a "" or a 0, but maybe it was a mistake. To suppress this warning assign a defined value to your variables.



(不带参数的旧调用是:find ( \&findFiles, $startDir);)

测试平台(但生产家庭将是 Linux 机器,Ubuntu 9.1,Perl 5.10,64 位):ActiveState Perl 64 位。 window XP。来自 perl -v: v5.10.0 为 ActiveState 提供的 MSWin32-x64-multi-thread Binary build 1004 [287188] 构建。

最佳答案

您需要创建一个子引用,使用所需的参数调用您想要的子引用:

find( 
  sub { 
    findFiles({ anInIsDryRun2 => $isDryRun });
  },
  $startDir
);

这或多或少是柯里化(Currying)。它只是不是很 curry 。 :)

关于perl - 如何将参数传递给处理每个文件的 File::Find 子例程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2056649/

相关文章:

perl 脚本递归列出目录中的所有文件名

perl -pi -e 语法带有 |或 <

perl - IO::Socket::SSL 在 IIS 8.5 中失败

regex - 为什么当正则表达式与 * 对组匹配时 Perl 很懒惰?

perl - File::Find::Rule 和文件分隔符

Perl 脚本查找 Unix 上所有无主文件和目录 - 如何进一步优化?

perl - 如何在 Perl 中格式化时间戳?

perl - 如何使用Moose为数组属性添加便捷功能?

perl - 为什么 File::Find 不处理我损坏的符号链接(symbolic link)?