Hi, My script reads from two text input files, each one of these text files gets updated every once in a while. I am suppose to look for a pattern in these input files and when I find a pattern I am suppose to print it to a socket.

Problem is that if you try to read from a file which has no new data in it, then the io->getline() function will get blocked, and you won't be able to read from the other file even if it is getting populated with valid data.

I have been looking at different solutions, but in every solution I seem to hit a roadblock, so please help!

Solution 1: Use IO::Select, and IO::Handle: In this solution I can select a file descriptor which is ready to be read from using:

@canBeRead = select->can_read(); foreach $elem (@canBeRead) { $elem->getline(); }
from what I have read, it is not a good idea to mix, IO::Select() and IO based getline() functions, as then select gets confused and tells you there is data to be read, when in reality there is no data to be read (because IO has its own buffers), causing all sorts of issues. Is that correct? Has anyone successfully used getline and IO::Select successfully? Note, I don't want to use sysread because I am interested in getting an entire line and not bytes.

Solution 2: I looked in a multi-threaded solution: This was so that I won't have to rely on Select and each thread could maintain its own file handles and won't get blocked. But as it turns out, in Perl threads filehandles are shared across threads by default. So now: - I create a file handles for each input file. - I create two threads to process the data in isolation. - when initiating the function call to the threaded function, I pass the file descriptors as well, so that the same function can work with both file descriptors:

$th1 = threads->create('foo',$firstFD); $th2 = threads->create('foo',$secondFD); sub foo { my $fh = shift; use the $fh to read from the file. }

problem is that file descriptors are are shared by default among all threads! so me passing file descriptors to foo does no good, as foo already has it as a global (and I read that passing file descriptors amongst threads is a bad idea). So I don't know how can I use different file descriptors within foo to read data from two separate files, and have each of those file descriptors not get blocked.

Than you!

In reply to selecting from a number of different input sources. by jasoncollins

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.