Perl和Batch可以在同一个批处理文件中运行吗?

时间:2022-04-27 02:41:53

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

我有一个批处理脚本执行一些处理并调用一些perl脚本。

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.

我的问题是,是否有办法将perl代码直接放入批处理脚本并让它运行两种类型的脚本。

3 个解决方案

#1


23  

Active Perl has been doing this for years!

Active Perl多年来一直这样做!

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.

下面是一个骨架。你只能打电话给perl一次。因为传递它-x开关说你会发现嵌入在这个文件中的perl代码,并且perl会读取文件,直到找到perl shebang(#!... perl)并开始在那里执行。 Perl将忽略__END__之后的所有内容,因为你告诉DOS转到endofperl它不会打扰任何东西,直到它到达标签。

@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

#2


4  

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.

事实上,这正是pl2bat工具所做的:它将perl程序转换为嵌入perl程序的批处理文件。看看pl2bat.bat本身。

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.

所以你可以使用.pl,用pl2bat转换它,然后根据需要调整批处理部分。批处理代码的最大部分必须放在文件的末尾(靠近:end_of_perl标签),因为在顶部的代码中,您只能使用单引号。

However:

然而:

  • this simple approach will not work if you need to embed more that one perl file
  • 如果你需要嵌入更多的perl文件,这个简单的方法将无法工作
  • this will be a maintenance nightmare.
  • 这将是一场维护噩梦。

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

所以我建议在一个Perl程序中编写整个过程。

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.

更新:如果您要在单个批处理文件中组合一个脚本和一些Perl模块,则可以使用fatpack组合Perl文件,然后对结果应用pl2bat。

#3


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.

有一种方法可以做到这一点,但它不会很漂亮。您可以将perl代码回显到temp .pl文件中,然后在.bat中运行该文件。

#1


23  

Active Perl has been doing this for years!

Active Perl多年来一直这样做!

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.

下面是一个骨架。你只能打电话给perl一次。因为传递它-x开关说你会发现嵌入在这个文件中的perl代码,并且perl会读取文件,直到找到perl shebang(#!... perl)并开始在那里执行。 Perl将忽略__END__之后的所有内容,因为你告诉DOS转到endofperl它不会打扰任何东西,直到它到达标签。

@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

#2


4  

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.

事实上,这正是pl2bat工具所做的:它将perl程序转换为嵌入perl程序的批处理文件。看看pl2bat.bat本身。

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.

所以你可以使用.pl,用pl2bat转换它,然后根据需要调整批处理部分。批处理代码的最大部分必须放在文件的末尾(靠近:end_of_perl标签),因为在顶部的代码中,您只能使用单引号。

However:

然而:

  • this simple approach will not work if you need to embed more that one perl file
  • 如果你需要嵌入更多的perl文件,这个简单的方法将无法工作
  • this will be a maintenance nightmare.
  • 这将是一场维护噩梦。

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

所以我建议在一个Perl程序中编写整个过程。

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.

更新:如果您要在单个批处理文件中组合一个脚本和一些Perl模块,则可以使用fatpack组合Perl文件,然后对结果应用pl2bat。

#3


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.

有一种方法可以做到这一点,但它不会很漂亮。您可以将perl代码回显到temp .pl文件中,然后在.bat中运行该文件。