Curtis "Ovid" Poe > Array-FileReader > Array::FileReader



Annotate this POD

View/Report Bugs
Module Version: 0.03   Source  


Array::FileReader - Lazily tie files to arrays for reading


  use Array::FileReader;
  tie @foo, Array::FileReader, "some.file";
  print $foo[30];


Plenty of times I've wanted to run up and down a file like this:

    @foo = <FILE>;
    for (0..100) {
        print $foo[$_];
    print $foo[10], $foo[20], $foo[30];

Of course, this is hugely inefficient since you have to load the entire file into an array in memory. Array::FileReader removes the inefficiency by only storing the line offsets in memory, and only discovering the line offsets when a line is called for. For instance, $foo[4] will only load 4 numbers into memory, and then $foo[30] will load another 26.

Because the file offsets are discovered when needed, there's no good way of getting the size of the file - you just have to pad through them all, which is slow.

The module was designed to speed up Mark-Jason Dominus' Algorithm::Diff module when finding differences between two very large files. In fact, it makes things less efficient, since the first thing that module does is find the size of the arrays. It just goes to show, doesn't it?


Curtis "Ovid" Poe, <>

Reverse the name to email me.


Simon Cozens, <>


Algorithm::Diff, Tie::MmapArray

syntax highlighting: