Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Your code,  open R,"$file" will by default open global symbol R for reading.

Better is:
open my $R, '<', "$file" or die "your specific message $!";
Use a lexical variable, "my" instead of a package global, explicitly say this is for reading (or writing), print an error message and exit your program (i.e. die) if the open does not succeed.

For a disk file, you can assume that all subsequent "prints" to that filehandle will work. There is no need to check if the file handle is still open, it is.

There are boundary cases where the write to a file will fail (like filesystem full) and there are situations where a connection to a network file socket will be lost. Handling those cases are beyond what you are asking about.

Update: While jwkrahn's answer is factually correct, this doesn't have much meaning in practical Perl code. When you open a file handle, you should check that the open worked, and if it did, thereafter you should assume that the file handle is still open. At low level C code, there is such a thing as a fileno. But you will very likely will never ever encounter or need this number in Perl. Just because a file handle is "open", that does not mean that actually using it to read or write will work. At the end of the day, "the proof is in the pudding" - you have to actually use the filehandle in order to know if it indeed works to get or send data.


In reply to Re: File input-output handle defined or no determination by Marshall
in thread File input-output handle defined or no determination by abdan

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 contemplating the Monastery: (5)
As of 2024-04-19 11:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found