in reply to Memory efficient design

After re-reading your root post, I'm really puzzled, especially by your ReadFile subroutine listed in italics at the end.

As noted in my earlier reply, programmers need to adapt their coding style over the years as hardware evolves. In mainstream computing (as opposed to Embedded software) memory has nowadays become so abundant and cheap that it's seldom a concern. At least, that's what I see.

> I am trying to load several big files into memory by reading the entire file. (They're between 5MB and 100MB each.) I want to figure out the best way to design my code so it won't waste memory.

It's hard to buy a laptop today with less than 8 GB of memory ... so you could comfortably load forty 100 MB files into memory simultaneously on a low end laptop. If you need more, you can buy 32 GB of PC memory for around $200, a bargain compared to programmer's salaries. ;-)

To avoid eyestrain, I've reformatted your ReadFile subroutine (in italics at the end of the root node) below:

# Usage: STRING = ReadFile(FILENAME, START, LENGTH) # Reads an entire file or part of a file in binary mode. # Returns the file contents as a string. # An optional second argument will move the file pointer before readin +g, # and an optional third argument limits the number of bytes to read. sub ReadFile { my $F = defined $_[0] ? $_[0] : ''; $F =~ tr#><*%$?\r\n\"\0|##d; -e $F || return ''; -f $F || return ''; my $S = -s $F; $S || return ''; my $L = defined $_2 ? $_2 : $S; $L > 0 || return ''; local *H; sysopen(H, $F, 0) || return ''; binmode H; my $P = defined $_1 ? $_1 : 0; $P >= 0 or $P = 0; $P < $S || return ''; $P < 1 || sysseek(H, $P, 0); my $D = ''; sysread(H, $D, $L); close H; return $D; }

Though you stated this code is "irrelevant", I disagree. Certainly, if this appeared in a code review at work, serious questions would be asked, especially around error handling and interface (e.g. going through each bullet point in the "API Design Checklist" section at On Coding Standards and Code Reviews).