Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

How do I read an entire file into a string?

by vroom (Pope)
on Jan 11, 2000 at 02:27 UTC ( #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

This is conventional:

{ local $/ = undef; open FILE, "myfile" or die "Couldn't open file: $!"; binmode FILE; $string = <FILE>; close FILE; }
But if you are working with large files (or a large number of them) you might consider using File::Slurp, which, in my case, decreased the runtime of my script from 40-45 minutes to 3 minutes.

Uri Guttman (the 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 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 reisinge

my $contents = do { local(@ARGV, $/) = $file; <> };

Also check out answers/discussions on SO, such as this one.

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

Using Path::Tiny:

my $contents = path($file_path)->slurp;
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.

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

I'm reading a text file on Windows. I used the first solution on this page and then print the result in $string

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

I noticed that the "end-of-line" for each line is changed from "0d 0a" to "0d 0d 0a"

I found that if I removed the "binmode FILE;" statement, "end-of-line" for each line is correctly printed as "0d 0a":

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

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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 all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (8)
    As of 2016-12-08 22:29 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      On a regular basis, I'm most likely to spy upon:













      Results (148 votes). Check out past polls.