macos 如何在 Mac OS X 上运行 Perl 脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2623336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 06:52:46  来源:igfitidea点击:

How can I run a Perl script on Mac OS X?

perlmacos

提问by Jeff

How do I run a Perl script on OS X?

如何在 OS X 上运行 Perl 脚本?

I honestly can't find the answer anywhere! Presumably I have to run a command in Terminal but what?

老实说,我在任何地方都找不到答案!大概我必须在终端中运行一个命令,但是什么?

(I know this is a real basic and stupid question)

(我知道这是一个真正的基本和愚蠢的问题)

回答by codaddict

You can run your Perlscript by invoking the Perlinterpreter and giving your file as input:

您可以Perl通过调用Perl解释器并将文件作为输入来运行脚本:

perl myprogram.pl

回答by Jeff Catania

The easiest way to run a perl script is with the option:

运行 perl 脚本的最简单方法是使用以下选项:

perl myprogram.pl

However, you may find it more useful to add a shebang line at the top of the perl file.

但是,您可能会发现在 perl 文件顶部添加 shebang 行更有用。

#!/usr/bin/perl
print "Hello World!\n";

In order to executethis script, you need to add execute permissions to your program. Run:

为了执行这个脚本,你需要为你的程序添加执行权限。跑:

chmod +x myprogram.pl

Now, in order to run your script, you can simply type:

现在,为了运行您的脚本,您只需键入:

./myprogram.pl

回答by DVK

A good tutorial on Perl in OSX can be found here:

可以在此处找到有关 OSX 中 Perl 的好教程:

http://www.mactech.com/articles/mactech/Vol.18/18.09/PerlforMacOSX/index.html

http://www.mactech.com/articles/mactech/Vol.18/18.09/PerlforMacOSX/index.html

A generic documentation on executing Perl code is of course perldoc perlrun.

关于执行 Perl 代码的通用文档当然是perldoc perlrun

To answer your question directly:

要直接回答您的问题:

You can run a perl script on any Unix system by either having the code evaluated and executed from command line:

通过从命令行评估和执行代码,您可以在任何 Unix 系统上运行 perl 脚本:

perl -e 'print "Hello World\n"';

Or you can save your Perl script to a file (customarily having .plextension, say script1.pland with the first line being #!/usr/bin/perl) and then you can execute it as any Unix program (after setting proper execute permissions)

或者你可以将你的 Perl 脚本保存到一个文件中(通常有.pl扩展名,比如script1.pl第一行是#!/usr/bin/perl),然后你可以像任何 Unix 程序一样执行它(在设置适当的执行权限之后)

/path/to/script/script1.pl

You can also execute a script from a file by running perl interpreter as the command and giving the script as a parameter (in this case execute permissions to the script are not needed):

您还可以通过运行 perl 解释器作为命令并将脚本作为参数从文件中执行脚本(在这种情况下不需要脚本的执行权限):

perl /path/to/script/script1.pl