我試圖刷新一個管道句柄使用從IO::Handle
模塊要麼autoflush()
和flush()
方法從open
獲得的,但我認爲這是行不通的。下面是一個例子:
host.pl:
use feature qw(say);
use strict;
use warnings;
my $client_pid = open (my $fh, '|-', 'client.pl')
or die "Could not open client: $!";
#$fh->autoflush(1); # adding this line does not help
sleep 2;
say "Host: sent message";
print $fh "Hello";
#print $fh "Hello\n"; # adding a newline works fine
$fh->flush() or warn "$!"; # this does not work
sleep 2;
say "Host exits.";
close $fh;
client.pl:
use feature qw(say);
use strict;
use warnings;
say "Client running..";
chomp (my $line = <STDIN>);
say "Client got line: '$line'";
sleep 1;
say "Client exits..";
運行host.pl
的輸出是:
Client running..
Host: sent message
Host exits.
Client got line: 'Hello'
Client exits..
預期OUTP UT是:
Client running..
Host: sent message
Client got line: 'Hello'
Client exits..
Host exits.
我知道我可以通過在字符串末尾添加一個新行解決這個問題要打印:
print $fh "Hello\n";
,但我很好奇,爲什麼$fh->flush()
沒有在這裏工作?
因爲客戶端正在使用'readline',所以它在''中循環直到'\ n'到達。 –
melpomene
@melpomene當然,我是盲目的!但問題是,如果有可能以其他方式閱讀郵件? –
@Borodin我很好奇你爲什麼使用'language:lang-perl'編輯語法突出顯示?沒有添加標籤,語法突出顯示對我來說也很好。或者我錯過了什麼? –