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. ;)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;
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |