gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 06:19:28 25 4
gpt4 key购买 nike

如果这里的某些术语可能有点过时,请见谅。如果我对某事使用了错误的术语,请随时纠正我。

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

我在更换 .bat 时遇到的问题/.cmd使用 perl 脚本变得过于复杂的脚本是我不能像 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但这似乎不起作用......

最佳答案

为什么不这样:

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 中定义的环境等。

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

关于Perl 作为批处理脚本工具 - 完全管道化子进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6779406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com