Beefy Boxes and Bandwidth Generously Provided by pair Networks
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:

cp /dev/null file.txt perl ./y1.pl echo "hello world" > file.txt perl ./y1.pl

y1.pl:

use strict; use warnings; my $file_src = 'file.txt'; open(my $fh, '<', $file_src) or die "open() failed: $!"; my $line = <$fh>; close($fh) or die "close() failed: $!"; printf( "Got a line: %s", $line // '<line-was-undef>' );

In reply to Re: Able to open file, unable to read by Apero
in thread Able to open file, unable to read by feiiiiiiiiiii

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2024-04-19 15:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found