Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

How do I read an entire file into a string?

( #1952=categorized question: print w/ replies, xml ) Need Help??
Contributed by vroom on Jan 11, 2000 at 02:27 UTC
Q&A  > files


Answer: How do I read an entire file into a string?
contributed by Anonymous Monk

If you are working with large files (or a large number of them) you might consider using File::Slurp.
It is much fast than the conventional:

{ local $/ = undef; open FILE, "myfile" or die "Couldn't open file: $!"; binmode FILE; $string = <FILE>; close FILE; }

In my case it managed to decrease the runtime of my script from 40-45 minutes to 3 minutes.

Uri (the current maintainer of File-Slurp) wrote a pretty exhaustive article on slurping.
Answer: How do I read an entire file into a string?
contributed by vroom

Unset $/, the Input Record Separator, to make <> give you the whole file at once.

{ local $/=undef; open FILE, "myfile" or die "Couldn't open file: $!"; $string = <FILE>; close FILE; }
Answer: How do I read an entire file into a string?
contributed by Anonymous Monk

open FILE, "myfile" or die "Couldn't open file: $!"; $string = join("", <FILE>); close FILE;
Answer: How do I read an entire file into a string?
contributed by Anonymous Monk

or, if not able to unset $/ due to some other limitations in the script:

open FILE, "myfile" or die "Couldn't open file: $!"; 
while (<FILE>){
 $string .= $_;
}
close FILE;
Answer: How do I read an entire file into a string?
contributed by DigitalKitty

If you know the size of the file, you could use the read function:

my $size = 2000; # or whatever open( FH, "sample.txt") or die("Error: $!\n"); read( FH, $data, $size ); close FH;
Answer: How do I read an entire file into a string?
contributed by particle

nothing i know beats this for speed. there's a node on this technique somewhere around here...

my $file = 'sample.txt'; { local *FH; -f FH and sysread FH, my $file, -s FH; }
Answer: How do I read an entire file into a string?
contributed by bdalzell

This is slightly different code for using File::Slurp:

use strict; use File::Slurp; my $text = read_file($file);
Be sure to read the "slurp_article", available in the distro under "Documentation" (i.e. under the "extras" directory).

And remember that if you cut and paste sample code from a web page, some of the punctuation characters may not be formatted for your system so if you get an error from cut and pasted code try typing it in directly to your program.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (4)
    As of 2014-09-20 15:43 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (160 votes), past polls