Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Able to open file, unable to read

by Marshall (Canon)
on May 14, 2016 at 09:09 UTC ( [id://1163024]=note: print w/replies, xml ) Need Help??


in reply to Able to open file, unable to read

I also agree that the old style file open is not an issue - should work fine. Apero's post shows what can happen if the file is "empty", you would get EOF.

If the writer of this file has it open, it is possible for there not to be anything in it yet, but yet possible for you to have it open for read. One of those reasons could be output buffering. Typically the writer will have to "write" at least 1K before the data gets flushed to the disk.

Perl has some built file test operators, my $size = -s $filename should give you the current file size that you are able to read.

So without fixing the writer to turn off buffering, by say adding $|=1, you could loop for a while, checking $size every second or so (sleep(1) or whatever) for a few minutes. Don't try to read from the open filehandle until $size>0. Usually once you hit "EOF", you can't read past that, but you can delay reading until something is actually available for you to read. That way you won't hit EOF.

Basically this idea just automates the "wait a few minutes" and try again part. Also adding this $size check to the code would I think be of diagnostic value, if you are getting undef read value (EOF) and $size>0, then there is some other problem.

PS: what O/S are you using?

Update: Perl File Test Operators

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1163024]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2024-04-26 02:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found