Вы находитесь на странице: 1из 1

Writing files

Now that you know how to open and read files learning how to write to them is st
raighforward. Take a look at the following code:
open FILE, ">file.txt" or die $!;
print FILE $str;
close FILE; Not much is new here. The only thing to observe is the two-argument
use of print, the first argument being the FILEHANDLE to write to and the second
an expression to be written. The expression can be anything: a scalar, a list,
a hash, etc. Appending to a file can be accomplished in exactly the same manner
- apart from specifying the appropriate (>>) mode of course.
At this point you are probably thinking that a description of the write is what
follows. Actually, as the manual page for write puts it:
Note that write is not the opposite of read. Unfortunately.
Instead write is used to write formatted records to file, a subject outside the
scope of this article.
Closing files
Once you are done reading and writing you should close any open filehandles.
open FILE1, "file.txt" or die $!;
open FILE2, "picture.jpg" or die $!;
...
close FILE2;
close FILE1;

Вам также может понравиться