Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
638 views
in Technique[技术] by (71.8m points)

perl - @ARGV is empty using ActivePerl in Windows 7

I have the following Perl script. I am trying to run it in Windows 7 using ActivePerl:

#!c:Perl64inperl.exe -w

use strict;

my $mp3splt_exe = 'c:Program Files (x86)mp3spltmp3splt.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
";

(as you can see, I am trying to create a wrapper for mp3splt :-) )

When I run it like this:

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

I get this:

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

So, first of all, when I print @ARGV, nothing gets printed, and second of all, when I assign $filename = $ARGV[0], $filename is undef, so I get the warning.

So... what am I doing wrong? Why isn't the commandline parameter being passed to the script?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As others have pointed out perl blah.pl asdf works, while blah.pl asdf fails. This is because when you run the perl script directly, Windows realizes it must call perl, and uses the rule perl "%1", which only passes the script name to perl, not any of the parameters.

To fix this, you have to tell windows to use the rule perl "%1" %*

How to do that can be a little tedious:

Option 1

According to perlmonks, you should be able to use assoc and ftype on the commandline. In fact, if you type help ftype, it tells you how to setup perl:

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

To run assoc requires cmd run as administrator on Window 7.

However, this didn't work for me. Windows ignored the association. I had to modify the registry. This may be due to the misguided advice to run the Default Programs utility on Win 7, which lets you specify the program to use for given file extensions. Unlike XP, this will not allow you to specify multiple command options (to be used in the right-click menu) -- it will only allow you to specify the program that is used when you double-click on a file (or run foo.pl from commandline).

Option 2

Modify the registry: HKEY_CLASSES_ROOT

If you've used the assoc/ftype commands, you may have entries for perl or PerlScript. As I said earlier, these will be ignored. Look for pl_auto_file, and drill down to the command:

HKCRpl_auto_fileshellopencommand

Here the (Default) should be set to something like: "C:Perlinperl.exe" "%1"

Add the missing %* on the end of that and you should be good to go: "C:Perlinperl.exe" "%1" %*

No reboot necessary.

Option 3

If you're lazy and trusting, you can try using this as a reg file, and importing it into your registry:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOTpl_auto_fileshellopencommand]
@=""C:\Perl\bin\perl.exe" "%1" %*"

This should be sufficient to make blah.pl asdf work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...