http://www.perlmonks.org?node_id=522800


in reply to How can you sysread an entire file?

sysread(DF, $rec, -s DF); will save you from specifying an arbitrary number.

foreach(split("\n",$rec)){push(@array,"$_\n";)} causes three copies of the file to be in memory at once. ($rec, foreach's list and @array).

push(@array, $1) while $rec =~ /(.*?\n)/g; will save you from having an third copy of the file in memory, and will save you from re-adding the "\n". Caveat: It will skip the last line if it isn't terminated by a "\n".

Unfortunately, splitting on "\n" is not portable. Keep in mind that sysread has a bug causing it to never translate CRLF to LF in Windows.

Is push(@array, $_) while <DF>; really that much slower?

Any why not use undef $rec; instead of $rec = '';? I prefer to just use curlies, though: my @array; { my $rec; ... }