Important alert: (current site time 7/15/2013 5:47:51 AM EDT)
 

article

How to start a Perl script under Windows without writing a separate batch file

Email
Submitted on: 3/11/2009 10:40:01 AM
By: Marc-Alain Gross 
Level: Intermediate
User Rating: By 2 Users
Compatibility: Active Perl specific
Views: 85164
 
     You know the problem: you want to write a Perl script under Windows but to run it, you need to specify the path to the script (i.e: "perl c:\apps\perl\showdate.pl"). Basically DOS allows to search through the Environment Variable "PATH" to find a script. But it allows only Microsoft scripts to run through this way (actually you have the possibility to assign all ".pl" files to Perl, but it doesn't work under DOS). Your (and my) goal is to start the script using the short way, that is without specifying the script's path and extension (i.e: "showdate"). In other words, our goal is to be able to write a file that is compatible for both Batch and Perl. This document shows different methods to reach this goal.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				

Introduction

Batch basics:

·         Its extension should be ".bat" or ".cmd" (i.e: "showdate.bat").

·         A comment in a batch is allowed using the "rem" command.

·         To avoid printing out batch execution on terminal, the "@" char should precede the Batch command (i.e: "@rem ..." or "@perl ...").

·         To jump from one line to another, the command "goto" needs a label as parameter (i.e: "goto end"). The label should be defined with ":" character before it (i.e: ":end").

 

Perl basics:

·         A Perl script line beginning with "@", means that the token afterwards is an array (i.e: "@array = (1, 2, 3);").

·         Comments under Perl begin with a "#" character.

·         A string with simple quote "'" can contain new lines characters.

 

Facts:

·         Batch and Perl scripts share only the "@" character.

The ActiveState solution

In directory "<perl path>\bin", you will find more than 50 batch files starting Perl Script.

 

Basically, the idea is to use the following structure:

@rem = '

BATCH command to call Perl

perl -x -S %0 %*

goto endofperl

@rem ';

Perl script

__END__

:endofperl

ev. other BATCH commands

In this case, the Batch part is at the beginning (between "@rem = '" and "goto endofperl", including these lines) and at the end of the file.

 

Problems:

·         it can't contain the "'" character, otherwise Perl will not be able to understand the command "@rem = '… ' … ';"

·         The error analysis should be done in the Batch part itself, as the "goto endofperl" erases the Perl script return-code…

Probably the best solution

Basically, the idea is to use the following structure:

@goto ;#DOS

Perl script

__END__

:;#DOS

BATCH command to call Perl

perl -S %0 %*

 

The only common part used by Perl and Batch is the line "@goto ;#DOS" which means:

·         for Perl: "Do nothing with @goto" (which produces a warning if switch "-w" has been used) followed by the comment "DOS".

·         for Batch: "jump to label ;#DOS"

 

This way, both script are completely separated. Beside it, as the Perl call is the last batch command used, any result (return-code) returned by the Perl Script is automatically returned by the batch itself.

 

Problems:

·         The Perl script generates two warnings

·         "Useless use of a variable in void context at" and

·         "Name "main::goto" used only once"

·         The Perl call as described "perl -S %0 %*" is not supported under Windows 95/98/ME

 

Other solutions:

·         To avoid both warnings try following:

@goto = "GotoBATCH" ;

@goto = ();

Perl script

__END__

:"GotoBATCH"

BATCH command to call Perl

perl -S %0 %*

·         To be compatible both for Windows NT (NT, 2000, 2003, 2008, XP, Vista) and Windows 95/98/ME, use:

perl -x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9

Tests solution

I was very sceptic with the Perl's "-S" switch. I thought we needed to specify the extension and it will not work with full path script. Therefore I tried following script (stored under "c:\apps\bat\testperl.bat", "c:\apps\bat" being set in the Path):

@goto ;#DOS

print("This is Perl speaking: $0 @ARGV\n");

__END__

:;#DOS

@echo This is DOS speaking: %0 %*

@perl -S %0 %*

 

And tried following calls:

·         Calling "c:\apps\bat\testperl.bat param1 param2 param3" returns:

This is DOS speaking: c:\apps\bat\testperl.bat param1 param2 param3

This is Perl speaking: c:\apps\bat\testperl.bat param1 param2 param3

 

·         Calling "testperl.bat param1 param2 param3" returns:

This is DOS speaking: testperl.bat param1 param2 param3

This is Perl speaking: c:\apps\bat/testperl.bat param1 param2 param3

 

·         Calling "testperl param1 param2 param3" returns:

This is DOS speaking: testperl param1 param2 param3

This is Perl speaking: c:\apps\bat/testperl.bat param1 param2 param3

 

·         Renaming "testperl.bat" by "test perl.bat" and calling
"
"test perl" param1 param2 param3" returns:

This is DOS speaking: "test perl" param1 param2 param3

This is Perl speaking: c:\apps\bat/test perl.bat param1 param2 param3

 

·         Even "TESTPE~1.BAT param1 param2 param3" returns:

This is DOS speaking: TESTPE~1.BAT param1 param2 param3

This is Perl speaking: c:\apps\bat/TESTPE~1.BAT param1 param2 param3

 

Now let's check some error cases:

·         When Perl works fine,"echo %errorlevel%" returns then:

0

 

·         Perl is not in the Path or not installed:

·         change "@perl -S %0 %*" with "@XXXperl ‑S %0 %*"

·         This returns:

This is DOS speaking: testperl param1 param2 param3

Der Befehl "XXXperl" ist entweder falsch geschrieben oder

konnte nicht gefunden werden.

·         "echo %errorlevel%" returns then:

9009

 

·         When Perl script has troubles:

·         change back "@XXXperl ‑S %0 %*" with "@perl -S %0 %*"

·         insert a new line "print 1/0;" before line "__END__":

·         This returns:

This is DOS speaking: testperl param1 param2 param3

This is Perl speaking: c:\apps\bat/testperl.bat param1 param2 param3

Illegal division by zero at c:\apps\bat/testperl.bat line 3.

·         "echo %errorlevel%" returns then:

9

 

·         When Perl script returns an exit code:

·         replace "print 1/0;" with "exit 5;":

·         This returns:

This is DOS speaking: testperl param1 param2 param3

This is Perl speaking: c:\apps\bat/testperl.bat param1 param2 param3

·         "echo %errorlevel%" returns then:

5

 


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

11/18/2009 12:28:25 AMre

ka
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.