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

时间:2023-01-18 01:08:44

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)

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

3 个解决方案

#1


68  

You can run your Perl script by invoking the Perl interpreter and giving your file as input:

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

perl myprogram.pl

#2


16  

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 execute this 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

#3


14  

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 .pl extension, say script1.pl and 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

#1


68  

You can run your Perl script by invoking the Perl interpreter and giving your file as input:

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

perl myprogram.pl

#2


16  

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 execute this 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

#3


14  

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 .pl extension, say script1.pl and 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