Hello fellow monks
I'm having trouble to decide for a file access method. I'm trying to describe the candidates that come to mind and their pros and cons I can think of. I'd like your input to pick the most appropriate one.
Requirements
Additional limitation: My parser is driven via a user-provided syntax description. I haven't found a way to describe a packed byte as a single value, they are handled as separate values. Therefore the parsing/writing requires multiple passes over the same byte. The data stream MUST either be able to return to a previous position or the current byte's content must be cached for a subsequent read.
Option a) file handle
open my $fh, '<', $filename;
PRO: works perfectly fine when converting from binary to parsed, 'natural' way to handle a file, file size does not need to have an impact on memory consumption
CON: converting from parsed to binary is better done in memory because it's easier to build the data from back-to-front; must protect file against change during parse/write
Option b) load data into scalar
local $/; my $data = <$fh>;
PRO: drastically reduce the time while parser is vulnerable to file change; does not matter where data is read/written.
CON: Must rely on substr to extract values from byte stream and/or manually track position
Option c) memory backed file handle
open my $fh, '<', \$data;
PRO: keep file access methods, while also lessen vulnerability to file change
CON: file must be read completely into memory even if just a small number of records are parsed and most of them are simply skipped
Which option is the most reasonable to you? Is there another option I am not aware of?
Update: crossed out remarks regarding file access concurrency, they are distracting| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |