Monk::Thomas has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Deciding for a file access method - requesting opinions
by BrowserUk (Patriarch) on Jul 14, 2015 at 20:49 UTC | |
by Monk::Thomas (Friar) on Jul 14, 2015 at 21:08 UTC | |
by BrowserUk (Patriarch) on Jul 14, 2015 at 21:49 UTC | |
by Monk::Thomas (Friar) on Jul 14, 2015 at 22:06 UTC | |
by BrowserUk (Patriarch) on Jul 15, 2015 at 14:30 UTC | |
| |
| A reply falls below the community's threshold of quality. You may see it by logging in. |