perl 在 windows 上传递参数

perl passing parameters on windows

我有以下代码:

$op = shift or die "Usage: rename expr [files]\n";
chomp(@ARGV = <STDIN>) unless @ARGV;
print "$op";

for ( @ARGV )
{
  print "$_";
  $was = $_;
  eval $op;
  die $@ if $@;
  rename ( $was, $_ ) unless $was eq $_;
 }

它在 linux 机器上产生了预期的结果,即当我 运行

  perl massrenamer.pl 's/\.txt/\.txtla/' *.txt

我得到了正确的结果。我尝试在 windows 机器上执行相同的操作,安装了 strawberry perl 就像

  perl massrenamer.pl 's/\.txt/\.txtla/' *.txt

  perl massrenamer.pl "s/\.txt/\.txtla/" *.txt

  perl massrenamer.pl 's/\.txt/\.txtla/' "*.txt"

但我没有得到任何结果。有人可以帮忙吗?

通配符扩展由 shell 完成,但当参数用引号引起来时则不会,在 windows 端口 perl 脚本上有一个模块 Win32::AutoGlob see also this SO question

快速修复:将 (@ARGV) 替换为 (glob "@ARGV")

$op = shift or die "Usage: rename expr [files]\n";
chomp(@ARGV = <STDIN>) unless @ARGV;
print "$op";

for ( glob "@ARGV" )
{
  print "$_";
  $was = $_;
  eval $op;
  die $@ if $@;
  rename ( $was, $_ ) unless $was eq $_;
}