Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do I Run a Perl Script from Cmd without typing "perl" in front of the script path?

Tags:

windows

cmd

perl

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

Without the perl.

like image 604
Christopher Peterson Avatar asked Feb 26 '23 21:02

Christopher Peterson


2 Answers

Assign the .pl extension to the perl interpreter. It depends on your Windows version how you do that.

Depending on the perl installer you are using it might also provide you with an option to do so automatically.

like image 90
ThiefMaster Avatar answered Feb 28 '23 11:02

ThiefMaster


You can put this at the top of your perl script's file:

@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. The above trick runs the strawberry perl interpreter, calling it with the -x switch followed by "%~f0". This is the path to the .cmd script. The .cmd script will then exit once your perl program is complete.

The bit below the #!perl line is your actual perl program, which the perl.exe knows to skip to when this line runs:

@c:\strawberry-perl-port\perl\bin\perl.exe -x "path\to\my\perl.cmd" %*
like image 33
slm Avatar answered Feb 28 '23 11:02

slm