#!/usr/bin/perl
use strict;
use warnings;
open(my $vmstat, "/usr/bin/vmstat 1 2>&1 |");
open(my $foo, ">", "foo.txt") or die "can't open it for write";
while(<$vmstat>) {
print "get ouput from vmstat and print it to foo.txt ...\n";
print $foo $_;
}
when I run the above code, nothing wrong happend.but after I press ctr-c to quit, nothing in the foo.txt. could any of you tells me why does this happen? thanks in advance.
Maybe the output is being buffered and you are not being patient enough. Try this extra line of code: