为什么我必须在脚本之前显式添加 perl 才能使 getops 正常运行?
当我像这样运行我的脚本时:
C:\>perl script.pl -f file
它工作正常。但是,如果我这样做:
C:\>script.pl -f file
那么我不会收到任何错误,但是 getopts
不会将任何内容分配给 $opt_f
这在 perl 5.8 Windows XP 上工作得很好,但是它不适用于 Windows 7 上的 perl 5.12。 没有安装任何其他版本的 perl
(它是新的操作系统版本)。
代码:
use Getopt::Std;
our ($opt_f);
getopts('f:');
print "input file is: $opt_f \n";
print "$0\n
运行时使用:
C:\> perl get.pl -f sadf
input file is: sadf
get.pl
运行时不使用:
C:\>get.pl -f sadf
input file is:
什么都没有!
编辑:已修复,这个问题是重复的...... 如何制作 Perl 脚本识别 Win32 cmd 控制台中的参数?
那篇文章的 OP 弄清楚了。
我必须做同样的事情,但还要在 gui 中重新创建 assoc(除了在 reg 中和在命令行上使用 ftype 之外)。
When i run my script like so:
C:\>perl script.pl -f file
It works fine. But, if I just do:
C:\>script.pl -f file
then I don't get any errors but getopts
doesn't assign anything to $opt_f
This works just fine on perl 5.8 Windows XP, but it doesn't work on perl 5.12 on Windows 7.
There aren't any other versions of perl
installed (its a new OS build).
Code:
use Getopt::Std;
our ($opt_f);
getopts('f:');
print "input file is: $opt_f \n";
print "$0\n
Run with:
C:\> perl get.pl -f sadf
input file is: sadf
get.pl
Run without:
C:\>get.pl -f sadf
input file is:
Nothing!
EDIT: fixed and this question was a repeat...
How do I make Perl scripts recognize parameters in the Win32 cmd console?
The OP of that post figured it out.
I had to do the same but also recreate the assoc in the gui (in addition to in the reg and on the command line with ftype.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
首先,查看:
获取 RHS 上的字符串,然后调用:
确保
%*
在那里。如果没有,请以管理员身份运行
cmd.exe
shell,并调用另请参阅
ftype /?
。First, look at:
Take the string on the RHS, and invoke:
Make sure
%*
is there.If not, run a
cmd.exe
shell as administrator, and invokeSee also
ftype /?
.当您将代码作为
perl script.pl -f file
调用时,您将显式运行perl
可执行文件并向其传递文件名和要解析的选项。但是,当您将其作为script.pl -f file
调用时,您会要求登录 shell 运行该文件,它将在没有任何其他信息的情况下将其解析为 shell 脚本 - 这是不是你想要的,因为你的文件不是 bash 脚本,而是 perl 脚本!通常,此类信息(使用什么程序来解析脚本)在所谓的 shebang 行中给出。如果您将其添加到脚本的顶部,它应该正确运行:(
或者也许
#!/bin/env perl
,如果您希望env
程序找到< code>perl 位于您的$PATH
中)。When you invoke your code as
perl script.pl -f file
, you are explicitly running theperl
executable and passing it a filename and options to parse. But when you invoke it asscript.pl -f file
, you are asking your login shell to run the file, which it will parse as a shell script in the absence of any other information -- this is not what you want, as your file is not a bash script, but a perl script!Normally such information (what program to use to parse the script) is given in what is called a shebang line. If you add this to the top of your script, it should run properly:
(or perhaps
#!/bin/env perl
, if you want theenv
program to findperl
in your$PATH
for you).在“script.pl”调用不起作用的 Windows7 系统上,Perl 脚本与什么(准确地说是 .pl 扩展名)相关联?
另外,您可以尝试运行(带或不带“perl”前缀):
use Data::Dumper;
打印数据::Dumper->Dump([$*, $0, $1])
What are Perl scripts associated with (.pl extension to be precise) on the Windows7 system where the "script.pl" call doesn't work?
Also, could you please try running (with and without "perl " prefix):
use Data::Dumper;
print Data::Dumper->Dump([$*, $0, $1])
已修复,这个问题是重复的... 如何让 Perl 脚本识别 Win32 cmd 控制台中的参数?
那篇文章的 OP 解决了这个问题。
我必须做同样的事情,但还要在 gui 中重新创建 assoc(除了在 reg 中以及使用 ftype 在命令行上)。
感谢大家的帮助!
fixed and this question was a repeat... How do I make Perl scripts recognize parameters in the Win32 cmd console?
The OP of that post figured it out.
I had to do the same but also recreate the assoc in the gui (in addition to in the reg and on the command line with ftype.)
Thanks everyone for your help!
如果您使用关键字
perl fileName.pl -f checkfile
(例如),那么您将显式调用解释器。现在,如果您想执行相同的命令但没有 perl 关键字,那么您需要将 shebang 添加到您的代码中。检查您的 Windows perl 安装。它应该是这样的:
#!/perl/bin/perl.
现在会发生什么,有了这个新行,命令行将读取第一个 2 个字节(#!)脚本并找出它是一个 perl 脚本,并且知道在哪里可以找到解释器并调用它(因为您提供了它的路径)。因此您的脚本将作为 perl 脚本执行。
If you are using the key word
perl fileName.pl -f checkfile
(for ex) then you are explicitly invoking the interpreter.Now, if you want to execute the same command but without the perl key word, then you will need to add the shebang to your code. Check your Windows perl installation. it should be something like this:
#!/perl/bin/perl.
What is going to happen now, with this new line, the command line will read the 1st 2 bytes(#!) of the script and figure out that it is a perl script and will know where to find the interpreter and will invoke it(since you provided the path to it). Hence your script will be executed as a perl script.
您不必添加“perl”前缀...为此
,您需要更改 PATH 环境变量,下面是更改 PATH 环境变量的命令
这会将当前工作目录附加到目录列表中PATH 环境变量。一旦我们这样做了,我们现在就可以在没有“perl”前缀的情况下运行
You don't have to add "perl" prefix as..
In order to do so you need to change the PATH environment variable, below is the command to change PATH environment variable
This will append the current working directory to the list of directories in the PATH environment variable. Once we do this we can now run without "perl" prefix