Can Perl and Batch run in the same batch file?
Asked Answered
B

3

16

I've got a batch script that does some processing and calls some perl scripts.

My question is if there was a way to put the perl code directly into the batch script and have it run both types of scripts.

Beaudoin answered 17/6, 2010 at 14:52 Comment(0)
R
27

Active Perl has been doing this for years!

Below is a skeleton. You can only call perl once though. Because passing it the -x switch says that you'll find the perl code embedded in this file, and perl reads down the file until it finds a perl shebang (#!...perl) and starts executing there. Perl will ignore everything past the __END__ and because you told DOS to goto endofperl it won't bother with anything until it gets to the label.

@rem = '--*-Perl-*--
@echo off
perl -x -S %0 %*
goto endofperl


@rem -- BEGIN PERL -- ';
#!d:/Perl/bin/perl.exe -w
#line 10

use strict; 


__END__
:endofperl
Renunciation answered 17/6, 2010 at 15:21 Comment(1)
This is exactly the kind of thing I was looking for! I would vote on this but it's telling me to register to do that.Beaudoin
K
5

Yes you can.

In fact this is exactly what the pl2bat tool does: it transforms a perl program into a batch file which embeds the perl program. Have a look to pl2bat.bat itself.

So you can take the .pl, convert it with pl2bat, and then tweak the batch part as you need. The biggest part of the batch code must be put at the end of the file (near the :end_of_perl label) because in the code at the top you are limited to not using single quotes.

However:

  • this simple approach will not work if you need to embed more that one perl file
  • this will be a maintenance nightmare.

So I suggest instead to write the whole process in one Perl program.

Update: if you have one script and some Perl modules that you want to combine in a single batch file, you can combine the Perl file using fatpack, and then apply pl2bat on the result.

Kimes answered 17/6, 2010 at 15:19 Comment(1)
That was the problem. I had 2 perl files that I wanted to use. But thanks for the suggestion!Beaudoin
S
1

There is a way to do this, but it wont be pretty. You can echo your perl code into a temp .pl file and then run that file from within your .bat.

Same answered 17/6, 2010 at 15:0 Comment(1)
Thought about that but thought it would make my work buddy cry. :)Beaudoin

© 2022 - 2024 — McMap. All rights reserved.