Prev

Perl Notes

Next

Chapter 8.2 Writing to a File

you may want to save some output from a script to an external file.

To write to a file:

  1. Open the file as described in Chapter 8.1
  2. Type print LABEL, where LABEL is the same filehandle you used in Chapter 8.1
  3. Type $scalar to write the contents of scalar to the open file. Or type "string", (and don't forget the quotes) where string is a constant you wish to write to the open file.
  4. Type ; to complete the line.
  5. Close the file (see Chapter 8.5).

open(FILE, ">>../myfile.txt") || &ErrorMessage;
print FILE "I'm adding text to a file.\n";
close(FILE);

sub ErrorMessage {
  print "Could not open file. It either does not exist or the permissions are wrong\n";
  exit;
}


Prev Home Next