9

我有以下 Perl 脚本。我正在尝试使用 ActivePerl 在 Windows 7 中运行它:

#!c:\Perl64\bin\perl.exe -w

use strict;

my $mp3splt_exe = 'c:\Program Files (x86)\mp3splt\mp3splt.exe';

my $mp3splt_args = '-o "@n @f" -g "r%[@o @N]"  -f -t 6.0';

print @ARGV;
my $filename = $ARGV[0];

print "$mp3splt_exe $mp3splt_args $filename\n";

(如您所见,我正在尝试为 mp3splt 创建一个包装器 :-))

当我这样运行它时:

C:\Program Files (x86)\mp3splt>run_mp3splt.pl a

我明白了:

Use of uninitialized value $filename in concatenation (.) or string at C:\Program Files (x86)\mp3splt\run_mp3splt.pl line 12.
c:\Program Files (x86)\mp3splt\mp3splt.exe -o "@n @f" -g "r%[@o @N]"  -f -t 6.0

因此,首先,当 I 时print @ARGV,什么都没有打印,其次,当我分配时$filename = $ARGV[0]$filenameis undef,所以我收到警告。

所以......我做错了什么?为什么没有将命令行参数传递给脚本?

4

4 回答 4

32

正如其他人指出的那样perl blah.pl asdf,虽然blah.pl asdf失败了。这是因为当你直接运行 perl 脚本时,Windows 意识到它必须调用 perl,并使用 rule perl "%1",它只将脚本名称传递给 perl,而不传递任何参数。

要解决这个问题,您必须告诉 Windows 使用该规则。perl "%1" %*

如何做到这一点可能有点乏味:

选项1

根据perlmonks,您应该能够在命令行上使用assocand ftype。事实上,如果你输入help ftype,它会告诉你如何设置 perl:

assoc .pl=PerlScript
ftype PerlScript=perl.exe %1 %*

运行assoc需要 cmd在 Window 7 上以管理员身份运行。

但是,这对我不起作用。Windows 忽略了该关联。我不得不修改注册表。这可能是由于Default Programs在 Win 7 上运行该实用程序的错误建议,它允许您指定用于给定文件扩展名的程序。与 XP 不同,这将不允许您指定多个命令选项(用于右键单击菜单)——它只允许您指定双击文件时使用的程序(或运行 foo .pl 来自命令行)。

选项 2

修改注册表: HKEY_CLASSES_ROOT

如果您使用过 assoc/ftype 命令,您可能有perl或的条目PerlScript。正如我之前所说,这些将被忽略。查找pl_auto_file,并深入到command

HKCR\pl_auto_file\shell\open\command

这里(Default)应该设置为: "C:\Perl\bin\perl.exe" "%1"

在最后添加缺少%*的内容,您应该很高兴:"C:\Perl\bin\perl.exe" "%1" %*

无需重新启动。

选项 3

如果您懒惰且信任他人,可以尝试将其用作 reg 文件,并将其导入注册表:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\pl_auto_file\shell\open\command]
@="\"C:\\Perl\\bin\\perl.exe\" \"%1\" %*"

这应该足以使blah.pl asdf工作。

于 2011-09-14T01:38:25.687 回答
7

我遇到的问题是,如果我在 Win7 上执行:

perl myprog.pl a b c  

程序正确获取了参数(在@ARGV中),但如果我执行:

myprog.pl a b c 

程序不会接收参数。

我在网上搜索了一个解决方案,很快发现这不是 ActiveState perl 问题,而更可能是 Windows(Win7)中的文件类型关联问题(感谢 PerlMonks 网站)。

然而,所有解决方案都会改变

assoc .pl=Perl  

ftype Perl="C:\Perl\bin\perl.exe" "%1" %* 

没有为我解决这个难题。我确实注意到 assoc .pl 没有以某种方式使用,因为如果我添加 assoc .plx=Perl 并将我的程序重命名为 myprog.plx

myprog.plx a b c 

工作完美!

所以我在微软论坛上读到了这个问题,提到了 Win7“功能”默认程序,我找到了解决问题的方法:

单击开始按钮 打开默认程序,然后单击“默认程序”。

选择“将文件类型或协议与程序关联”并选择“.pl”并单击“更改程序”。已经有一个 Perl 命令行解释器被指定为推荐程序,但我点击了浏览并自己选择了 Perl.exe。关闭“关联文件类型...”屏幕后,

myprog.pl a b c 

像魅力一样执行,我的程序正确检索了所有参数。

希望有帮助...

于 2012-10-11T12:10:57.167 回答
2

Windows 8.1中Perl ARGV问题解决方案:

HKEY_CLASSES_ROOT\Applications\perl.exe\shell\open\command = "C:\Perl\bin\perl.exe" "%1" %*

无需重新启动。

于 2014-02-15T23:35:47.667 回答
-3

我很确定 Windows 7 不理解 shebang 行。如果你用 运行它会发生什么perl run_mp3splt.pl a

于 2010-11-20T19:44:54.740 回答