Perl 作为批处理脚本工具 - 完全管道化子进程?
如果这里的某些术语可能略有偏差,我们深表歉意。如果我使用了错误的术语,请随时纠正我。
是否可以使用 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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许您会发现
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$?
.为什么不这样:
我没有你的交互式程序,但执行的 shell 允许我输入命令,它继承了 perl 中定义的环境等。
我很长一段时间都在 Windows 上使用 perl 作为更复杂的 shell 脚本的替代品到目前为止,我需要的一切都是可能的。
Why not this way:
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.