Windows – Difference between ‘call perl script.pl’ vs ‘call script.pl’ on Windows

batchbatch-filewindowswindows-server-2008-r2windows-server-2012-r2

On Windows servers (I've tested 2008 R2 and 2012 R2) we noticed different behavior in execution of Perl scripts that are called from batch.

Consider a primitive batch file as follows:

:beginlbl
call path\script.pl
goto :beginlbl

Sooner or later (sometimes after just a few script executions on other occasions after hundreds of executions) there will be an error message:
"Windows cannot access the specified device, path, or file. You may not have the appropriate permissions to access the item."

If the second line of the code is modified to look as follows:

call perl path\script.pl

(Note the executable name before the script.) The script will run forever without issues.

What is Windows executing differently in the two cases outlined above?

Best Answer

Using the file association goes through a shell process to evaluate the file type (extension) and launch the appropriate process. You're likely overwhelming this. This is similar to how you get your registered editor when invoking a .txt. See also ShellExecute.

Directly invoking the executable cuts out the middleman, and would always be preferred.

Uslackr's comment on your original question is correct as well, and he notes that the perl executable on the system path is invoked. You're best off to directly specify the executable you wish to run by a full path to avoid another executable from being accidentally invoked. This is good for both reliability and security. Portability is sacrificed, but you can choose your priorities.