简体   繁体   English

如何在不在脚本路径前键入“perl”的情况下从Cmd运行Perl脚本?

[英]How Do I Run a Perl Script from Cmd without typing “perl” in front of the script path?

For example perl C:\\Projects\\trunk\\PcApps\\BaseCamp\\Test\\smoketest.pl C:\\Projects\\trunk\\PcApps\\BaseCamp\\Test\\log.txt 例如perl C:\\Projects\\trunk\\PcApps\\BaseCamp\\Test\\smoketest.pl C:\\Projects\\trunk\\PcApps\\BaseCamp\\Test\\log.txt

Without the perl . 没有perl

Assign the .pl extension to the perl interpreter. 将.pl扩展名分配给perl解释器。 It depends on your Windows version how you do that. 这取决于您的Windows版本如何做到这一点。

Depending on the perl installer you are using it might also provide you with an option to do so automatically. 根据您使用的perl安装程序,它可能还会为您提供自动执行此操作的选项。

You can put this at the top of your perl script's file: 您可以将它放在perl脚本文件的顶部:

@SETLOCAL ENABLEEXTENSIONS
@c:\strawberry-perl-port\perl\bin\perl.exe -x "%~f0" %*
@exit /b %ERRORLEVEL%

#!perl

....perl program goes here...

You'll also need to change the extension of your script so that it's .cmd instead of .pl. 您还需要更改脚本的扩展名,使其为.cmd而不是.pl。 The above trick runs the strawberry perl interpreter, calling it with the -x switch followed by "%~f0". 上面的技巧运行了草莓perl解释器,用-x开关调用它后跟“%~f0”。 This is the path to the .cmd script. 这是.cmd脚本的路径。 The .cmd script will then exit once your perl program is complete. 一旦perl程序完成,.cmd脚本将退出。

The bit below the #!perl line is your actual perl program, which the perl.exe knows to skip to when this line runs: #!perl行下方的位是您的实际perl程序,perl.exe知道在此行运行时跳过:

@c:\strawberry-perl-port\perl\bin\perl.exe -x "path\to\my\perl.cmd" %*

There are a few programs that can convert your .pl file to .exe format: 有一些程序可以将.pl文件转换为.exe格式:

After conversion, you may still need perl installed on your system, but the exe file finds the perl interpreter by itself. 转换后,您可能仍需要在系统上安装perl,但exe文件本身会找到perl解释器。 (Disclaimer: I did not try any of these) (免责声明:我没有尝试过这些)

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

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