如何在 Perl 中获取外部命令的输出? [英] How do I get the output of an external command in Perl?

查看:33
本文介绍了如何在 Perl 中获取外部命令的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 Windows 命令行程序的输出(例如,powercfg -l)写入使用 Perl 创建的文件中,然后在 for 循环中逐行读取文件并将它分配给一个字符串.

I want to have output of Windows command-line program (say, powercfg -l) written into a file which is created using Perl and then read the file line by line in a for loop and assign it to a string.

推荐答案

my $output = qx(powercfg -l);

## You've got your output loaded into the $output variable. 
## Still want to write it to a file?
open my $OUTPUT, '>', 'output.txt' or die "Couldn't open output.txt: $!
";
print $OUTPUT $output;
close $OUTPUT

## Now you can loop through each line and
##   parse the $line variable to extract the info you are looking for.
foreach my $line (split /[
]+/, $output) {
  ## Regular expression magic to grab what you want
}

这篇关于如何在 Perl 中获取外部命令的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆