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

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

问题描述

我想有Windows命令行程序的输出(例如, POWERCFG -l <​​/ code>)写入作为使用Perl创建,然后通过读取文件行的​​文件在一条线上的循环,并将其分配给一个字符串。


解决方案

 我的$输出= QX(POWERCFG -l);##你有你的输出加载到$输出变量。
##还是想将其写入到一个文件?
打开我的$ OUTPUT,'&GT;','output.txt的或死无法打开output.txt中:$ \\ N!
打印$ OUTPUT $输出;
接近$ OUTPUT##现在,您可以通过每个线路回路和
##解析$行变量来提取您正在寻找的信息。
我的foreach $线(分/ [\\ r \\ n] + /,$输出){
  ##普通EX pression魔术抓住你想要什么
}

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: $!\n";
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 /[\r\n]+/, $output) {
  ## Regular expression magic to grab what you want
}

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

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