简体   繁体   English

为什么必须在脚本之前显式添加perl才能使getops正常运行?

[英]Why do I have to explicitly add perl before the script for getops to run properly?

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 那么我没有收到任何错误,但是getopts没有为$opt_f分配任何$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). 这在perl 5.8 Windows XP上可以正常工作,但是在Windows 7上的perl 5.12上却不能工作。没有安装任何其他版本的perl (它是新的OS版本)。

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? 编辑:固定,此问题是重复的... 我如何使Perl脚本识别Win32 cmd控制台中的参数?

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.) 我必须做同样的事情,但还要在gui中重新创建assoc(除了在reg和带有ftype的命令行中)。

First, look at: 首先,看一下:

C:\> assoc .pl
.pl=Perl

Take the string on the RHS, and invoke: 在RHS上获取字符串,然后调用:

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

Make sure %* is there. 确保%*在那里。

If not, run a cmd.exe shell as administrator, and invoke 如果不是,请以管理员身份运行cmd.exe Shell,然后调用

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

See also ftype /? 另请参见ftype /? .

When you invoke your code as perl script.pl -f file , you are explicitly running the perl executable and passing it a filename and options to parse. 当您将代码作为perl script.pl -f file调用时,您将显式运行perl可执行文件,并向其传递文件名和要解析的选项。 But when you invoke it as script.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! 但是,当您将其作为script.pl -f file调用时,您是在要求登录外壳程序运行该文件,如果没有其他信息,它将被解析为外壳程序脚本–这不是您想要的,因为文件不是bash脚本,而是perl脚本!

Normally such information (what program to use to parse the script) is given in what is called a shebang line. 通常,此类信息(用于解析脚本的程序)在所谓的shebang行中给出。 If you add this to the top of your script, it should run properly: 如果将其添加到脚本顶部,则它应该可以正常运行:

#!/usr/bin/perl

(or perhaps #!/bin/env perl , if you want the env program to find perl in your $PATH for you). (或者,如果您希望env程序在$PATH为您找到perl ,则可能是#!/bin/env perl )。

If you are using the key word perl fileName.pl -f checkfile (for ex) then you are explicitly invoking the interpreter. 如果您使用关键字perl fileName.pl -f checkfile (例如ex),那么您将显式调用解释器。

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. 现在,如果要执行相同的命令但没有perl关键字,则需要将shebang添加到代码中。 Check your Windows perl installation. 检查Windows perl安装。 it should be something like this: #!/perl/bin/perl. 它应该是这样的: #!/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). 现在会发生什么,在此新行中,命令行将读取脚本的第一个2个字节(#!),并确定这是一个perl脚本,并且知道在哪里可以找到解释器并调用它(因为您提供了它的路径)。 Hence your script will be executed as a perl script. 因此,您的脚本将作为perl脚本执行。

What are Perl scripts associated with (.pl extension to be precise) on the Windows7 system where the "script.pl" call doesn't work? Windows7系统上的“ script.pl”调用不起作用,哪些Perl脚本与(确切地说是.pl扩展名)相关联?

Also, could you please try running (with and without "perl " prefix): 另外,您可以尝试运行(带有和不带有“ perl”前缀):

use Data::Dumper; 使用Data :: Dumper; print Data::Dumper->Dump([$*, $0, $1]) 打印数据:: Dumper-> Dump([$ *,$ 0,$ 1])

fixed and this question was a repeat... How do I make Perl scripts recognize parameters in the Win32 cmd console? 已修复,此问题重复了。。 如何使Perl脚本识别Win32 cmd控制台中的参数?

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.) 我必须做同样的事情,但还要在gui中重新创建assoc(除了在reg和带有ftype的命令行中)。

Thanks everyone for your help! 谢谢大家的帮助!

You don't have to add "perl" prefix as.. 您不必添加“ perl”前缀作为..

In order to do so you need to change the PATH environment variable, below is the command to change PATH environment variable 为此,您需要更改PATH环境变量,下面是更改PATH环境变量的命令

PATH=$PATH:$(pwd)

This will append the current working directory to the list of directories in the PATH environment variable. 这会将当前工作目录附加到PATH环境变量中的目录列表中。 Once we do this we can now run without "perl" prefix 完成此操作后,我们现在可以在没有“ perl”前缀的情况下运行

$ hello.pl

Hello world

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM