How can I get the command-line output of a DOS tool using Perl?

帅比萌擦擦* 提交于 2019-12-01 00:02:17

Use either open in pipe mode:

open($filehandle, "$command|") or die "did not work: $! $?";
while(<$filehandle>)
{
#do something with $_
}

or use backticks:

my  @programoutput=`$command`

You can't get the output with system().

Instead use bactkicks:

my $throughput = 0;
my $output = `ftp -s:c:\\ftp_dl.txt`;
if (($? == 0) && ($output =~ /([\d+\.]+)\s*K?bytes\/sec/m)) {
    $throughput = $1;
}

$output will contain all the lines from the execution of the ftp command (but not any error message sent to STDERR).
Then we check if ftp returned success (0) and if we got a throughput somewhere in the output.
If so, we set $throughput to it.

This being Perl, there are many ways to do this:

You could also use the Net::FTP module that supports Windows to deal with the file transfer and use a timing module like Time::HiRes to time it and calculate your throughput.

This way you won't depend on the ftp program (your script would not work on localised version of Windows for instance without much re-work, and you need to rely on the ftp program to be installed and in the same location).

See perlfaq8, which has several answers that deal with this topic. The ones you probably need for this question are:

Also, you might be interested in some of the IPC (Interprocess Communication) Perl modules that come in the standard library:

  • IPC::Open2
  • IPC::Open3

Some of the Perl documentation might also help:

If you're not familiar with the Perl documentation, you might check out my Perl documentation documentation.

Good luck,

You should try and use libcurl which is more suited for the task.

There is an easy to use API

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!