Click here to get back home

FAQ 5.28 How can I read in an entire file all at once?

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
FAQ 5.28 How can I read in an entire file all at once? PerlFAQ Server 03-14-2008
Posted by PerlFAQ Server on March 14, 2008, 3:03 am
Please log in for more thread options
This is an excerpt from the latest version perlfaq5.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

--------------------------------------------------------------------

5.28: How can I read in an entire file all at once?


You can use the File::Slurp module to do it in one step.

use File::Slurp;

$all_of_it = read_file($filename); # entire file in scalar
@all_lines = read_file($filename); # one line perl element

The customary Perl approach for processing all the lines in a file is to
do so one line at a time:

open (INPUT, $file) || die "can't open $file: $!";
while (<INPUT>) {
chomp;
# do something with $_
}
close(INPUT) || die "can't close $file: $!";

This is tremendously more efficient than reading the entire file into
memory as an array of lines and then processing it one element at a
time, which is often--if not almost always--the wrong approach. Whenever
you see someone do this:

@lines = <INPUT>;

you should think long and hard about why you need everything loaded at
once. It's just not a scalable solution. You might also find it more fun
to use the standard Tie::File module, or the DB_File module's $DB_RECNO
bindings, which allow you to tie an array to a file so that accessing an
element the array actually accesses the corresponding line in the file.

You can read the entire filehandle contents into a scalar.

{
local(*INPUT, $/);
open (INPUT, $file) || die "can't open $file: $!";
$var = <INPUT>;
}

That temporarily undefs your record separator, and will automatically
close the file at block exit. If the file is already open, just use
this:

$var = do { local $/; <INPUT> };

For ordinary files you can also use the read function.

read( INPUT, $var, -s INPUT );

The third argument tests the byte size of the data on the INPUT
filehandle and reads that many bytes into the buffer $var.



--------------------------------------------------------------------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.

Posted by szr on March 15, 2008, 3:04 pm
Please log in for more thread options
PerlFAQ Server wrote:
[...]
> $all_of_it = read_file($filename); # entire file in scalar
> @all_lines = read_file($filename); # one line perl element
^^^^

s/perl/per/ ?

[...]

--
szr



Posted by Dr.Ruud on March 16, 2008, 7:53 am
Please log in for more thread options
PerlFAQ Server schreef:

> $var = do { local $/; <INPUT> };


This variant uses less memory:

my $data;
do { local $/; $data = <INPUT> };

--
Affijn, Ruud

"Gewoon is een tijger."

Posted by Michele Dondi on March 16, 2008, 6:05 pm
Please log in for more thread options
On Sun, 16 Mar 2008 12:53:26 +0100, "Dr.Ruud"

>> $var = do { local $/; <INPUT> };
>
>
>This variant uses less memory:
>
> my $data;
> do { local $/; $data = <INPUT> };

Too bad that it does, because the former is certainly more
appealing...


Michele
--
->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Similar ThreadsPosted
FAQ: How can I read in an entire file all at once? October 8, 2004, 11:10 am
FAQ 5.26: How can I read in an entire file all at once? November 14, 2004, 6:03 pm
FAQ 5.26 How can I read in an entire file all at once? January 21, 2005, 12:03 pm
FAQ 5.27 How can I read in an entire file all at once? April 5, 2005, 5:03 am
FAQ 5.27 How can I read in an entire file all at once? June 20, 2005, 11:03 pm
FAQ 5.27 How can I read in an entire file all at once? October 3, 2005, 10:03 am
FAQ 5.27 How can I read in an entire file all at once? November 19, 2005, 11:03 pm
FAQ 5.27 How can I read in an entire file all at once? July 2, 2006, 9:03 am
FAQ 5.27 How can I read in an entire file all at once? August 23, 2006, 9:03 pm
FAQ 5.27 How can I read in an entire file all at once? November 4, 2006, 3:03 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap