Welcome to the Monastery | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
File input and output is not much of a stretch from normal I/O.
Basically you have to use the open command to open a
filestream and then read to and write from it. Then once you're done
with it you use close to close the file. The syntax for
opening a file is:
open(FILEHANDLE,"filename"); This opens a new filehandle with the name of FILEHANDLE, and associates it with filename which is the location of the file on your disk. This works for reading a file. If you want to write to it you need to put a > in front of the filename as seen below: open(FILEHANDLE,">filename"); To append to a file you use >> in front of the filename as you can see here: open(FILEHANDLE,">>filename"); Now for some quick examples:
To print stuff to a file you merely through a line into your program like: print FILEHANDLE "your text here\n"; FILEHANDLE needs to be a FILEHANDLE you currently have open. Notice there is no comma between the FILEHANDLE and the stuff your printing out. That is the way it needs to be. Now for a more extended example:
Another thing you might want to look into are the filetest operators these allow you to do things like test whether a file exists, is readable, is writable among other things. complete information look at the documentation In reply to File Input and Output by root
|
|