in reply to Re^4: Perl binary file reading
in thread Perl binary file reading
my @r = <$fh>; my $data = join '', @r;
That's a waste of memory, because you have the file content in both @r and $data. The usual idiom is:
my $data=do { local $/; <$fh> };
This reads the file contents directly into $data, without any splitting, joining, or temporary arrays. See Perl Idioms Explained - my $string = do { local $/; <FILEHANDLE> };.
See also The parable of the falling droplet to remember what $/ and $\ do.
Alexander
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Perl binary file reading
by kepler (Scribe) on May 03, 2016 at 13:33 UTC | |
by afoken (Chancellor) on May 03, 2016 at 20:19 UTC |