Perl Monk, Perl Meditation | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
The random error is, it's able to open the file (the die action never happens), but unable to read any data ($line is uninitialized) Since your code isn't hitting the die() after the open() call, the program found the file and had permission to open it. Chances are good you just attempted to read an empty file. As perlop will explain under the I/O Operators heading, the angled-bracket read returns undef in scalar context when you're at EOF (end-of-file.) If you care about this condition, you should test the definedness of $line before using it, and possibly report EOF as an error condition to the invoker of the script rather than try to use it. I can reproduce the condition you're seeing by running the code shown at the bottom of this post (which I call y1.pl) with the following test. Note that I'm on a Unix-alike, so you may need to populate an empty file a different way as defined by your OS. Also note that I replace an undefined result with a notice that the line was in fact undefined, using the // operator. This should illustrate the problem I suspect you're having. Test case:
y1.pl:
In reply to Re: Able to open file, unable to read
by Apero
|
|