Wiggins has asked for the wisdom of the Perl Monks concerning the following question:
I have long used the paradym of:
That works fine for a single known file. But now the task is to 'tail' files in an arbitrary number of directories, all in the same tailing loop.open INP, "<xyzzy"; while (! $fileRotated){ while(<INP>){ ... read lines } sleep 1; seek (INP,0,1); #clear EOF #check inodes and length }
For this task, my open() function is in a readdir() loop, and the handles are being put into a hash keyed by the directory name.
'$Accts{$_}' take place of INP. That seems to be running without visible problems. But when using these file handles in a read statement the results is not as expected.while (readdir $dirH) { next if ($_ eq '.'|| $_ eq '..'); next if (! m/\@(?:\d{1,3}\.)\d{1,3}/); #ip of src of records if ( ! defined $Accts{$_}){ #new directory # open the new file and put handle in hash #open $Accts{$_}, "<$_/all/events.log"; open $Accts{$_}, "<$Cpath/$dateDir/$_/all/events.log"; } }
I expected to see lines of text ( timestamp dirName textline). The while(<FH>) should be returning lines of the files. Instead I see:foreach $key (keys %Accts){ # seek($teamAccts{$key}, 0, 1); # reset end-of-file error my $safekey=$key; $safekey =~ s/ /_/g; # no embedded spaces in tokens while (<$Accts{$key}> ) { # one of the sub files my $L=$_; # $_ by itself gave same result my $msgL="$DTG $safekey $L"; # $L should have \n already $rsltStr .= $msgL; } }
WTFO? "GLOB(...)"1400765377 msgfrom@10.0.1.2 GLOB(0x1f11bf8)
-----Update-2-----
Found the problem! It now works!!
It was in the path value passed to the open. I did not fully construct the superior path (prefix) to the value returned from the readdir.
As a result I attempted to open an incomplete path and failed.
-----Update-1-----
I have tried the readline() approach on a file that has data present:
Nothing in the while loop executes. 'plog' is a logging subroutine.foreach $key (keys %teamAccts){ # plog "Key=$key"; seek($teamAccts{$key}, 0, 1); # reset end-of-file error my $safekey=$key; $safekey =~ s/ /_/g; # no embedded spaces in tokens #while (<$teamAccts{$key}> ) { # one of the sub files while ( readline($teamAccts{$key}) ){ my $L=$_; plog "readline=<$L>"; my $msgL="$DTG $safekey $L"; # $line should have \n already $eventStr .= $msgL; #addEvent ($key, $L); } }
I suspected that the 'seek' might not be working to clear the EOF, so I used a file that has 10 lines of text. None were processed.
I Dumped the hash after opening the file with this result:
$VAR2 is a reference, to a typeglob, of my hash entry??--scanning MSpt scanCS: opening <Chris - Kali 1@10.0.1.2> $VAR1 = 'Chris - Kali 1@10.0.1.2'; $VAR2 = \*{'::$teamAccts{...}'}; --finding +++finding MSpt Key=Chris - Kali 1@10.0.1.2 --scanning
It is always better to have seen your target for yourself, rather than depend upon someone else's description.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: FileHandles in a Hash (<> ambiguity)
by LanX (Saint) on May 28, 2014 at 16:07 UTC | |
by morgon (Priest) on May 28, 2014 at 22:10 UTC | |
by LanX (Saint) on May 29, 2014 at 01:58 UTC | |
|
Re: FileHandles in a Hash
by poj (Abbot) on May 28, 2014 at 15:52 UTC | |
by morgon (Priest) on May 28, 2014 at 21:51 UTC | |
|
Re: FileHandles in a Hash
by jellisii2 (Hermit) on May 28, 2014 at 15:42 UTC | |
|
Re: FileHandles in a Hash
by Laurent_R (Canon) on May 28, 2014 at 18:55 UTC | |
|
Re: FileHandles in a Hash
by locked_user sundialsvc4 (Abbot) on May 28, 2014 at 17:38 UTC |