Difference between revisions of "File I/O in Perl"

From Free Knowledge Base- The DUCK Project: information for everyone
Jump to: navigation, search
(New page: Use a filehandle to open a file in Perl. The filehandle identifier doesn't have a prefix like other Perl identifiers. To open a file use a FILEHANDLE (a name you decide, all caps isn't a...)
(No difference)

Revision as of 10:23, 12 April 2008

Use a filehandle to open a file in Perl. The filehandle identifier doesn't have a prefix like other Perl identifiers.

To open a file use a FILEHANDLE (a name you decide, all caps isn't a requirement but suggested for clarity) and a FILENAME

open(BOBSFILE, "information.txt");

The above file will be opened for "read" since no i/o type was specified. There are three ways to open a file. This is specified with a symbol before the filename. If no symbol is specified, "read" is assumed.

  1. read <            (open an existing file for read)   ex: open BOBSFILE, "<information.txt";
  2. write >            (create a new file to write)   ex: open NEWSTORY, ">story.txt";
  3. append >>            (add more to an existing file)   ex: open(LOG, ">>activity.log");