| 
        
          Perl Tips: tail -fのエミュレーション
         2023/12/23  | 
| Perl Tips [Prev] [Next] [Perl Top] | 
        ファイル出力を監視/表示したい
        
 
#!perl -w
use IO::Handle;
use strict;
{
    my $fh;
    open($fh, "> test0.txt") or die;
        $fh->autoflush(1);  # autoflush 有効
        for (my $i=0; $i<20; $i++) {
            print     "$i\n";  # 標準出力
            print $fh "$i\n";  # ファイルハンドル
            sleep(1);          # 1秒待ち
        }
        print $fh "EOF\n";     # 最後に文字列'EOF'を出力
    close($fh);
}
          
#!perl -w
use Time::HiRes;
use strict;
{
    my $fh;   # ファイルハンドル
    my $pos;  # ファイルポジション
    
    open($fh, "< test0.txt") or die;
        while(1) {
            my @data = <$fh>;   # 現ポジションから最後までデータ取得(ファイルハンドルはEOFになる)
            print @data;        # 読み出しデータ表示
            $pos = tell($fh);   # 現読み出し終了ポジション取得
            
            my $join_data = join('', @data);
            last if ($join_data =~ /EOF/);  # EOF文字列を見つけたらループ終了
            
            Time::HiRes::usleep(10*1000);   # 10*1000us = 10ms待ち
            
            seek($fh, $pos, 0);  # 現読み出し終了ポジションへのseekダミー実行で
                                 # 真の目的であるファイルハンドルのEOF解除を実施!!
        }
    close($fh);
}
          @echo off del test0.txt >nul 2>&1 echo #=======================================# echo # test0.pl: test0.txt Writeプロセス開始 # echo #=======================================# start test0.pl echo. echo #==========# echo # 5秒 wait # echo #==========# timeout /T 5 echo. echo #======================================# echo # test1.pl: test0.txt Readプロセス開始 # echo #======================================# test1.pl >runme.bat #=======================================# # test0.pl: test0.txt Writeプロセス開始 # #=======================================# #==========# # 5秒 wait # #==========# 0 秒待っています。続行するには何かキーを押してください ... #======================================# # test1.pl: test0.txt Readプロセス開始 # #======================================# 0 1 2 ...中略... 18 19 EOF  | 
| 
         Copyright(C) 2023 Altmo 
      本HPについて  | 
| Perl Tips [Prev] [Next] [Perl Top] |