|
Perl Tips: バッファリングを止める
2023/12/17 |
| Perl Tips [Prev] [Next] [Perl Top] |
出力はバッファリングされている
#!perl -w
use strict;
for (my $i=1; $i<4; $i++) {
sleep(1);
print "$i\n";
}
>test.pl 1 2 3
#!perl -w
use strict;
for (my $i=1; $i<4; $i++) {
sleep(1);
print "$i\r";
}
>test.pl 3 # 1,2は表示されない バッファリングを止める
#!perl -w
use IO::Handle;
use strict;
STDOUT->autoflush(1); # STDOUT(標準出力)のautoflush有効化(引数に1設定)
for (my $i=1; $i<4; $i++) {
sleep(1);
print "$i\r";
}
>test.pl 3 # 1,2も表示されたと思います
#!perl -w
use IO::Handle;
use strict;
my $fh;
open($fh, "> hoge.txt") or die; # 変数にファイルハンドル紐づけ
$fh->autoflush(1); # ファイルハンドルのautoflush有効化(引数に1設定)
for (my $i=1; $i<10; $i++) {
sleep(1);
print $fh "$i\n"; # ファイル出力(autoflush有効)
print "$i\n"; # 画面出力(改行付なのでautoflushに関係なく出力)
}
close();
|
|
Copyright(C) 2023 Altmo
本HPについて |
| Perl Tips [Prev] [Next] [Perl Top] |