Yeah, you can either use a positive integer as a file handle, or you can use a text label

Note that catfish1116 is using Perl 5.12 or better, in which case lexical filehandles are generally recommended. Also, to pick a nit, it's not a "text label", it's actually a "bareword filehandle", which is essentially a global variable, which is one of the reasons that such filehandles are usually not recommended anymore.

don't forget to close your opened file handles before the program ends

Although closing filehandles explicitly as soon as possible is a good idea, the way you've written this implies that Perl doesn't close filehandles automatically, but it does. Another advantage of lexical filehandles is that they are closed automatically as soon as they go out of scope.

Note that most of this has been discussed before. Lexical filehandles were introduced in Perl 5.6, so they are available even in your TinyPerl 5.6 and 5.8.


In reply to Re^2: Usage of File Handles by haukex
in thread Usage of File Handles by catfish1116

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.