Perl 作为批处理脚本工具 - 完全管道化子进程?

发布于 2024-11-25 09:06:07 字数 1333 浏览 2 评论 0原文

如果这里的某些术语可能略有偏差,我们深表歉意。如果我使用了错误的术语,请随时纠正我。

是否可以使用 Perl 作为“高级 shell”来运行“批处理”脚本? (在 Windows 上)

当用 Perl 脚本替换变得过于复杂的 .bat/.cmd 脚本时,我面临的问题是我不能轻易地像 shell 一样运行子进程。

也就是说,我想从我的 perl 脚本中执行与调用子进程时 shell 相同的操作,即完全“连接”STDIN、STDOUT 和 STDERR。

示例:

foo.bat -

@echo off
echo Hello, this is a simple script.
set PARAM_X=really-simple

:: The next line will allow me to simply "use" the tool on the shell I have open, 
:: that is STDOUT + STDERR of the tool are displayed on my STDOUT + STDERR and if
:: I enter something on the keyboard it is sent to the tools STDIN

interactive_commandline_tool.exe %PARAM_X%

echo The tool returned %ERROLEVEL%

但是,我不知道如何在 perl 中完全实现这一点(有可能吗?):

foo.pl -

print "Hello, this is a not so simple script.\n";
my $param_x = get_more_complicated_parameter();

# Magic: This sub executes the process and hooks up my STDIN/OUT/ERR and 
# returns the process error code when done
my $errlvl = run_executable("interactive_commandline_tool.exe", $param_x);
print "The tool returned $errlvl\n";

如何在 perl 中实现这一点?我玩过 IPC: :Open3 但这似乎不起作用......

Apologies if some of the terminology may be slighlty off here. Feel free to correct me if I use a wrong term for something.

Is it possible to use Perl as an "advanced shell" for running "batch" scripts? (on Windows)

The problem I face when replacing a .bat/.cmd script that's getting too complicated with a perl script is that I can't easily run sub processes as a shell does.

That is, I would like to do the same thing from my perl script as a shell does when invoking a child process, that is, fully "connecting" STDIN, STDOUT and STDERR.

Example:

foo.bat -

@echo off
echo Hello, this is a simple script.
set PARAM_X=really-simple

:: The next line will allow me to simply "use" the tool on the shell I have open, 
:: that is STDOUT + STDERR of the tool are displayed on my STDOUT + STDERR and if
:: I enter something on the keyboard it is sent to the tools STDIN

interactive_commandline_tool.exe %PARAM_X%

echo The tool returned %ERROLEVEL%

However, I have no clue how to fully implement this in perl (is it possible at all?):

foo.pl -

print "Hello, this is a not so simple script.\n";
my $param_x = get_more_complicated_parameter();

# Magic: This sub executes the process and hooks up my STDIN/OUT/ERR and 
# returns the process error code when done
my $errlvl = run_executable("interactive_commandline_tool.exe", $param_x);
print "The tool returned $errlvl\n";

How can I achieve this in perl? I played around with IPC::Open3 but it seems this doesn't do the trick ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乞讨 2024-12-02 09:06:07

也许您会发现 IPC::Run3 很有用。它允许您捕获 STDOUT 和 STDERR (但不能实时通过管道传输它们)。命令错误级别将在 $? 中返回。

Probably you'll find IPC::Run3 useful. It allow you to capture both STDOUT and STDERR (but not pipe them in real time). Command error level will be returned in $?.

泪冰清 2024-12-02 09:06:07

为什么不这样:

print "Hello, this is a not so simple script.\n";
my $param_x = get_more_complicated_parameter();

system('cmd.exe', $param_x);
my $errorlevel = $? >> 8;
print "The tool returned $errorlevel\n";

sub get_more_complicated_parameter { 42 }

我没有你的交互式程序,但执行的 shell 允许我输入命令,它继承了 perl 中定义的环境等。

我很长一段时间都在 Windows 上使用 perl 作为更复杂的 shell 脚本的替代品到目前为止,我需要的一切都是可能的。

Why not this way:

print "Hello, this is a not so simple script.\n";
my $param_x = get_more_complicated_parameter();

system('cmd.exe', $param_x);
my $errorlevel = $? >> 8;
print "The tool returned $errorlevel\n";

sub get_more_complicated_parameter { 42 }

I don't have your interactive program, but the shell executed allowed me to enter commands, it has inherited environment defined in perl, etc.

I am using perl as replacement for more complicated shell scripts on Windows for long time and so far everything I needed was possible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文