FileIO - Disc file read and write methods
Version 1.04b - 3:57 PM 2004-10-06
use Mdc::FileIO; $F = new Mdc::FileIO; $err = $F->open($myfile, 'create'); $F->write($data); $F->close;
The FileIO module provides a set of methods to read and write disc files. Built-in safety feature prevents the accidental accessing of files using an absolute pathname or via the parent directory. Runtime errors such as a file not existing or attempting to write to a read-only file will result in an error code being returned. Fatal errors such as supplying a null string for a filename will raise an exception and cause the program to die.
Create a new file object.
$F = new Mdc::FileIO; $F = new Mdc::FileIO( safety => 1 );
Open a file for read, append, overwrite, or create. Returns a zero (0) if successful or an error code if not. See error messages section. If a subsequent open is called, the previous file will be closed.
$err = $F->open( $filename ); $err = $F->open( $filename, $mode );
Object properties that may be accessed are:
$F->{safety} prevent accessing absolute file path or parent dir
$F->{eof} 1 if at end of file during read
$F->{errno} The last error code
$F->{error} The last error message
Close a currently open file.
$F->close;
Copy a file. If the file is currently open, it will be closed. Return 1 if copy successful.
$F->copy( source, destination );
Read entire contents of the currently open file and place the data in the referenced scalar variable supplied as the argument. Return 1 if successful or 0 if at end of file.
$result = $F->read(\$data);
Read one line of data from the currently open file and place the data in the referenced scalar variable supplied as the argument. Return 1 if successful or 0 if at end of file.
$result = $F->readln(\$data);
Return end-of-file status.
Write data to the currently open file. Argument may be a scalar, scalar reference, a list, or a list reference.
$F->write( SCALAR ); $F->write( SCALAR REF ); $F->write( LIST ); $F->write( LIST REF );
use Mdc::FileIO;
my $F = new Mdc::FileIO;
$F->open('myfile.txt','create');
die $F->{error} if $F->{errno};
$F->write("My test data\n");
$F->close;
$F->open('myfile.txt');
die $F->{error} if $F->{errno};
$F->read(\my $data);
$F->close;
print $data;
1301 Filename not defined 1302 Unable to open file 1303 File does not exist 1304 File already exists 1305 Invalid character(s) in filename 1306 Absolute path not allowed while safety enabled 1307 Parent path not allowed while safety enabled 1308 File permissions do not allow read 1309 File permissions do not allow overwrite 1310 File permissions do not allow append 1311 Invalid mode 1312 Cannot write to unopened file 1313 Cannot read from unopened file 1314 File not open for writing 1315 File not open for reading
Mdc::FileIO Copyright (c) 2004 Mark K Mueller. All rights reserved.
Mark K Mueller PO Box 576705 Modesto, CA 95357 http://www.markmueller.com/