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

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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 per 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>) {
                    # 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

            $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

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

Quoted text here. Click to load it
Quoted text here. Click to load it

Instead of local(*INPUT), these days a lexical file handle should be

            local $/;
            open (my $input, $file)     || die "can't open $file: $!";
            $var = <$input>;

Similarily, in the other examples, a lexical file handle can be used.
(Personally, I use the suffix "_fh" to mark file handles, so that would

            local $/;
            open (my $input_fh, $file)     || die "can't open $file: $!";
            $var = <$input_fh>;

or more likely (the shorter the scope, the shorter the variable names):

            local $/;
            open (my $fh, $file)     || die "can't open $file: $!";
            $var = <$fh>;

but YMMV. Oh, and I use "or" instead of "||" for the "in case of error"


Site Timeline