2012-06-28 2 views
1
use Text::Diff; 
for($count = 0; $count <= 1000; $count++){ 
    my $data_dir="archive/oswiostat/oracleapps.*dat"; 
    my $data_file= `ls -t $data_dir | head -1`; 
    while($data_file){ 
     print $data_file; 
     open (DAT,$data_file) || die("Could not open file! $!"); 
     $stats1 = (stat $data_file)[9]; 
     print "Stats: \n"; 
     @raw_data=<DAT>; 
     close(DAT); 
     print "Stats1 is :$stats1\n"; 
     sleep(5); 
     if($stats1 != $stats2){ 
     @diff = diff \@raw_data, $data_file, { STYLE => "Context" }; 
     $stats2 = $stats1; 
     } 
     print @diff || die ("Didn't see any updates $!"); 
    } 
} 

는 출력 : 통계가 누락 및 해결 방법을하는 이유Perl에서 어떻게 디렉토리를 변경하여 볼 수 있습니까?

$ perl client_socket.pl 
archive/oswiostat/oracleapps.localdomain_iostat_12.06.28.1500.dat 
Stats: 
Stats1 is : 
Didn't see any updates at client_socket.pl line 18. 

당신이 말해 줄 수 있습니까?

+5

나는 그것이 실제로 실행 한 코드라고 생각하지 않습니다. $ data_file은 파일의 이름 뒤에 개행 문자가 있기 때문에 open이 실패하여 죽을 것이다. 'chomp'로 해결할 수 있습니다. 수많은 다른 문제가 있지만 코드가 아닌 것에 대해 논평해야 할 점은 무엇입니까? 나는 강력하게'use strict; 경고를 사용하고 오류를 수정하십시오. – ikegami

+0

파일은 "archive/oswiostat/oracleapps.localdomain_iostat_12.06.28.1500.dat"로 주먹 줄에 인쇄되어 있습니다. – VeerM

+0

아니요, 아카이브/oswiostat/oracleapps.localdomain_iostat_12.06.28.1500.dat가 있습니다. \ n "' – ikegami

답변

2

뭔가 다른 질문을하는 새롭게 편집 된 질문 제목이 아닌 stat()이 실패한 이유는 원래 질문에 대한 답입니다.

my $data_file= `ls -t $data_dir | head -1`; 
chomp($data_file); 

이 수정 프로그램입니다 이유 조금 어두운입니다 :

은 수정입니다. chomp()이 없으면 $data_file에는 후행 개행 문자 인 "some_filename\n"이 있습니다. 두 인수 형식은 open()ignores trailing newlines in filenames and I don't know why 이므로 두 개의 인수는 셸 동작을 모방합니다. 그러나 stat()을 호출해도 파일 이름의 개행을 무시하지 않으므로 존재하지 않는 파일이 stat()이므로 $stats1undef입니다.

+0

@picrow thans for the help. 나는 오랜 시간이 지나면 펄을 만지고, 빠른 도움을 주셔서 감사합니다. 그리고 if 루프에있는 $ stats2를 수정해야합니다. 이 변수는 전역 변수 여야합니다. 어떻게 수정해야합니까? 현재 stats2가 비어 있습니다. – VeerM

13

실제 수정 사항은 File::ChangeNotify 또는 File::Monitor 또는 유사합니다 (예 : Windows의 경우 Win32::ChangeNotify).

use File::ChangeNotify; 

my $watcher = File::ChangeNotify->instantiate_watcher(
    directories => [ 'archive/oswiostat' ], 
    filter => qr/\Aoracleapps[.].*dat\z/, 
); 

while (my @events = $watcher->wait_for_events) { 
    # ... 
} 
+0

+1 [XY 문제] (http://www.perlmonks.org/?node_id=542341) – pilcrow

+1

+1 : 신안 :이 기능이 필요하며 내 게시물이 도움이된다고 생각합니다. –

관련 문제