I'd do this:
use strict; use warnings; my $in = '/x.txt'; my $out = '/y.txt'; open 0, "< $in" or die "Can't open $in for reading"; if (!open 1, "> $out") { close 0; die "Can't open $out for writing"; } $a = <0>; close 1 or die "Can't close output file $out"; close 0 or die "Can't close input file $in"; print $a; exit;
Notice that you do not need to use a variable for file handle. You can use numbers. Also, IF we successfully open the input file for reading but fail to open the output file for writing, then I am not sure if Perl automatically closes the open file handle when we call die(), so I just close it to make sure it's done. Also, if you write the little arrow and the file name together into one string, then your perl script will be compatible with earlier versions of perl such as perl 5.004 and such. But if you pass 3 separate arguments to open(), then your program will not compile with earlier perl interpreters! So, ever since I found this out, I always pass only two arguments to open() to make sure that my code will be backwards compatible. ;)

In reply to Re: First attempt at bringing in file for input/output by harangzsolt33
in thread First attempt at bringing in file for input/output 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.