CIW Course Revision Site

Perl - Advanced File Input and Output

In a Nutshell - CIW Course Section 2, Part B2, Chapter 7

 

When you're writing a text file, Perl translates the \n character sequence into the record separator that your operating system uses. In Unix, \n becomes an ASCII 10 (LF); on a Macintosh, ASCII 13 (CR); and on DOS and Windows systems, it becomes the sequence ASCII 13 and ASCII 10 (CRLF). When you're writing text, this behaviour is appropriate.

When you're writing binary data—GIF files, EXE files, MS Word documents, and so on—translation isn't what you want. Any time you really need to write binary data, and don't want Perl or the operating system to translate it for you, you must use the binmode function to mark the filehandle as binary. 

read Function

The read function reads $length bytes from a file to position $offset in $buffer and returns the number of bytes successfully read from the file.

$bytesread = read(MYFILE, $buffer, $length, $offset);

The read operation begins at the current pointer position within the file, data is read into the scalar variable $buffer at a position within this string value identified by $offset. $buffer will be grown or shrunk to the length actually read. The function returns the number of bytes read or 0 (zero) if the position is at EOF.

write Function

The write function is almost never used in normal Perl file processing. print is preferable - and, in fact, more correct - in nearly every case. write is used to put formatted records into a file. It is intended to be used with reports printed on paper, so it's functionality veers towards formatting headings, pages, and such.

getc Function

The getc function returns a single character read from the file identified by FILEHANDLE. It will return a null string if at EOF.

$mychar = getc(MYFILE);

If a filehandle is not specified getc will read input from the computer keyboard.

File Pointers

A file pointer represents the current position within a file when reading from or writing to a file. The tell function reports the current position and the seek function allows the setting of a new position.

tell Function

The tell function returns the current position or byte value of the file pointer in the specified filehandle:

$pointer = tell(MYFILE);

Apparently, the tell function will operate on the currently selected filehandle if no filehandle is specified. Quite how one determines the current filehandle I am not sure.

seek Function

The seek function sets the file pointer position within the file. This position is relative to the start, end or current position in the file. The function return 1 if successful and 0 if not.

$myvar = seek(MYFILE, $position, $startpoint);

Seek Start Points
Value Relative Position
0 Start of the file
1 Current file pointer position
2 End of file

close Function

The close function closes the connection to the file specified by filehandle. Returns 1 if successful. Closes the currently selected filehandle if the argument is omitted.

$myvar = close(MYFILE);

binmode Function

binmode marks the filehandle as binary. Use binmode after the filehandle is opened but before you do any input or output from it:

open(FH, ">elephant.gif") || die "$!";
binmode(FH); # The filehandle is now binary.
# Start of a valid GIF file...
print FH "GIF87a\056\001\045\015\000";
close(FH);

binmode needs to be used only once unless you close and reopen the file.

Design by Stephen

Certified Internet Webmaster

Page last Edited: 10 Nov 2011