As I fumble along the path of Perl, I keep finding that my stride exceed my knowledge. Today it is 'tailing' files whose handles are in a hash.

I have long used the paradym of:

open INP, "<xyzzy"; while (! $fileRotated){ while(<INP>){ ... read lines } sleep 1; seek (INP,0,1); #clear EOF #check inodes and length }
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.

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.

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"; } }
'$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.
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; } }
I expected to see lines of text ( timestamp dirName textline). The while(<FH>) should be returning lines of the files. Instead I see:
1400765377 msgfrom@10.0.1.2 GLOB(0x1f11bf8)
WTFO? "GLOB(...)"

-----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:

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); } }
Nothing in the while loop executes. 'plog' is a logging subroutine.

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:

--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
$VAR2 is a reference, to a typeglob, of my hash entry??
Is this of any help?

It is always better to have seen your target for yourself, rather than depend upon someone else's description.


In reply to FileHandles in a Hash by Wiggins

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.