Basic HTML version of Foils prepared March 30 97

Foil 60 FileHandles I -- open close die (Chapter 10 of Llama Book)

From PERL4 Tutorial for CPS616 Computational Science for Information Age Course CPS616 -- February 1995. by Geoffrey C. Fox


Files are like statement labels designated by a string without a special initial character. It is recommended that you use all capitals in such labels
STDIN STDOUT STDERR (and diamond <> null name) have been introduced and correspond to UNIX stdin, stdout and stderr (and concatenation of argument files if <> operator)
Filehandles allow you to address general files and the syntax is similar to UNIX standard I/O (stdio.h) support
  • open(FILEHANDLE,"unixname"); # opens file unixname for reading -- can use <
  • open(FILEHANDLE,">unixname"); # opens file unixname for writing
  • open(FILEHANDLE,">>unixname"); # opens file unixname in append mode
close(FILEHANDLE) closes file
Errors can be handled with die construct
open(FH,'>'.$criticalfile) || die("Print an error message if file can't be opened\n"); # Note how we add '>' (or ',' '>>') to file name stored in Perl variable



© Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.

Page produced by wwwfoil on Sat Sep 6 1997