pppe wrote:
> Is someone able to tell me if when using the open
command to read data
> from a file, if the file was large, say 60mb or more,
would it load
> much of the file into memory or would it only take a
record at a time?
Using the open() function (it's not a
"command") does not load any of
the file's data into memory. The file's data is loaded
into memory
when you actually *read* the file, using either read(),
readline(), or
the <> operator.
How much is read into memory depends on how you're reading
it. If you
use read(), you specify exactly how many bites are read at
any one
time. If you use readline(), you are reading one line into
memory at
any one time. If you use <>, then it depends on the
context in which
you use the operator:
In scalar context, <> reads one line at a time. In
list context, it
reads the entire file into memory:
while (my $line = <$fh>) {
#$line contains one line. When the loop begins the
#next iteration, $line is destroyed, and the memory
released,
#and a new $line is assigned to the next line of the file
}
my lines = <$fh>;
foreach my $line ( lines) {
#here, lines contains the *entire* file,
#all stored in memory. This is generally considered
#a BAD thing.
}
foreach my $line (<$fh>) {
#here, <$fh> is being evaluated in a list context
#and so the entire file is read into memory, just
#like the previous example that used an array.
}
In general, never use an array or a foreach to read from a
file using <
>. Always use a while loop to iterate through the file.
The sole exception is when you need to be able to process
more than one line at a time. And even then, there are
often work arounds to reading the entire file into memory.
Paul Lalli
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Perl Programming" group.
To post to this group, send email to perl-programming googlegroups.com
To unsubscribe from this group, send email to
perl-programming-unsubscribe googlegroups.com
For more options, visit this group at http:
//groups.google.com/group/perl-programming
-~----------~----~----~----~------~----~------~--~---
|