This
will most likely be one of your most widely used functions,
open. Here you will learn the basics of opening and
closing a file using perl. You will also learn to
howto load the contents of a file into an array for
output.
The
following piece of code will open the file, file.txt
for input. Saving the contents of the file to an array
(@file)
- open
(FILE, ">file.txt");
- @file
= <FILE>;
- close(FILE);
Want
to output the whole contents of a file? Checkout the
below code that utilizes the while loop:
- open
(FILE, "file.txt");
- while
(FILE) { print; }
- close(FILE);
The
following will do the same thing, however using a
foreach loop, outputing line by line:
- open
(FILE, "file.txt");
- foreach
$a (FILE) { print $a; }
- close(FILE);