File::RandomAccess3pm
Random access reads of sequential file or scalar
Usage
use File::RandomAccess;$raf = File::RandomAccess->new(\*FILE, $disableSeekTest);$raf = File::RandomAccess->new(\$data);$err = $raf->Seek($pos);$num = $raf->Read($buff, $bytes);
Overview
Allows random access to sequential file by buffering the file if necessary. Also allows access to data in memory to be accessed as if it were a file.
Methods
new-
Creates a new RandomAccess object given a file reference or reference to data in memory.
# Read from open file or pipe$raf = File::RandomAccess->new(\*FILE);# Read from data in memory$raf = File::RandomAccess->new(\$data);- Inputs:
-
0) Reference to RandomAccess object or RandomAccess class name.
1) File reference or scalar reference.
2) Flag set if file is already random access (disables automatic SeekTest).
- Returns:
-
Reference to RandomAccess object.
SeekTest-
Performs test
seek()on file to determine if buffering is necessary. If theseek()fails, then the file is buffered to allow random access.SeekTest() is automatically called fromnewunless specified.$result = $raf->SeekTest();- Inputs:
-
0) Reference to RandomAccess object.
- Returns:
-
1 if seek test passed (ie. no buffering required).
- Notes:
-
Must be called before any other i/o.
Tell-
Get current position in file
$pos = $raf->Tell();- Inputs:
-
0) Reference to RandomAccess object.
- Returns:
-
Current position in file
Seek-
Seek to specified position in file. When buffered, this doesn't quite behave like
seek()since it returns success even if you seek outside the limits of the file.$success = $raf->Seek($pos, 0);- Inputs:
-
0) Reference to RandomAccess object.
1) Position.
2) Whence (0=from start, 1=from cur pos, 2=from end).
- Returns:
-
1 on success, 0 otherwise
Read-
Read data from the file.
$num = $raf->Read($buff, 1024);- Inputs:
-
0) Reference to RandomAccess object.
1) Buffer.
2) Number of bytes to read.
- Returns:
-
Number of bytes actually read.
ReadLine-
Read a line from file (end of line is $/).
- Inputs:
-
0) Reference to RandomAccess object.
1) Buffer.
- Returns:
-
Number of bytes read.
Slurp-
Read whole file into buffer, without changing read pointer.
- Inputs:
-
0) Reference to RandomAccess object.
- Returns:
-
Nothing.
BinMode-
Set binary mode for file.
- Inputs:
-
0) Reference to RandomAccess object.
- Returns:
-
Nothing.
Close-
Close the file and free the buffer.
- Inputs:
-
0) Reference to RandomAccess object.
- Returns:
-
Nothing.
Options
NoBuffer-
Avoid buffering sequential files.
$raf->{NoBuffer} = 1;When this option is set, old data is purged from the internal buffer before a read operation on a sequential file. In this mode, memory requirements may be significantly reduced when reading sequential files, but seeking backward is limited to within the size of the internal buffer (which will be at least as large as the last returned data block), and seeking relative to the end of file is not allowed.
Releases
11 releases since Debian 4.0 (Etch); the text changed 10 times.