Perl anonymous channel without output

Question

Why is nothing printed when using an anonymous channel if I do not print the actual data from the channel?


Example

use strict;
use warnings;

my $child_process_id = 0;
my $vmstat_command = 'vmstat 7|';
$child_process_id = open(VMSTAT, $vmstat_command) || die "Error when executing \"$vmstat_command\": $!";


while (<VMSTAT>) {
    print "hi" ;
}
close VMSTAT or die "bad command: $! $?";

Appears to hang


use strict;
use warnings;

my $child_process_id = 0;
my $vmstat_command = 'vmstat 7|';
$child_process_id = open(VMSTAT, $vmstat_command) || die "Error when executing \"$vmstat_command\": $!";


while (<VMSTAT>) {
    print "hi" . $_ ;
#                ^^^ Added this
}
close VMSTAT or die "bad command: $! $?";

Print

hiprocs -----------memory---------- ---swap-- -----io---- -system-- -----cpu------
hi r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
hi 1  0      0 7264836 144200 307076    0    0     0     1    0   14  0  0 100  0  0

etc...


Expected Behavior

It would print hi for each vmstat output line for the first example.


Version

perl, v5.10.0
GNU bash, version 3.2.51

Miscellaneous

It also seems to hang when using chomp before printing a line (which, as I thought, only removes newline characters).

I feel that I am missing something fundamental for how the channel is read and processed, but cannot find a similar question. If there is one, then fool it, and I will look at it.


Any additional information you just need to ask.

+4
source share
1

print "hi";

to

print "hi\n";

""

, , ,

$|

, . 0 ( , , "$ |" , Perl ). STDOUT, , , - , - . , , Perl rsh . . . getc man- perlfunc. (: , .)

+6

Source: https://habr.com/ru/post/1626330/


All Articles