http://www.perlmonks.org?node_id=1952

vroom has asked for the wisdom of the Perl Monks concerning the following question: (files)

How do I read an entire file into a string?

Originally posted as a Categorized Question.

  • Comment on How do I read an entire file into a string?

Replies are listed 'Best First'.
Re: How do I read an entire file into a string?
by Anonymous Monk on Aug 18, 2004 at 12:22 UTC
    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.
        It is worth noting that File::Slurp does NOT support binmode

        Are you sure? From the pod:

        If you set the binmode option, then the file will be slurped in binary + mode. my $bin_data = read_file( $bin_file, binmode => ':raw' ) ;
Re: How do I read an entire file into a string?
by vroom (His Eminence) on Jan 11, 2000 at 02:28 UTC
    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; }
Re: How do I read an entire file into a string?
by Anonymous Monk on Jun 22, 2001 at 00:25 UTC
    open FILE, "myfile" or die "Couldn't open file: $!"; $string = join("", <FILE>); close FILE;

    Originally posted as a Categorized Answer.

Re: How do I read an entire file into a string?
by Anonymous Monk on Dec 18, 2000 at 21:42 UTC
    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;
    

    Originally posted as a Categorized Answer.

Re: How do I read an entire file into a string?
by marto (Cardinal) on Mar 26, 2018 at 14:22 UTC
Re: How do I read an entire file into a string?
by DigitalKitty (Parson) on Jul 09, 2002 at 14:37 UTC
    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;
Re: How do I read an entire file into a string?
by reisinge (Hermit) on Jun 15, 2016 at 10:54 UTC
    my $contents = do { local(@ARGV, $/) = $file; <> };

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

      Stolen from brian d foy.

      Who stole it from Juerd.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
      In the absence of evidence, opinion is indistinguishable from prejudice. Not understood.
Re: How do I read an entire file into a string?
by choroba (Cardinal) on Jun 15, 2016 at 11:46 UTC
Re: How do I read an entire file into a string?
by particle (Vicar) on Jul 09, 2002 at 15:29 UTC
    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; }
      Don't see how this works. This must just be a fragment. Seems like you need at least this much:
      my $file = 'sample.txt'; my $buf; { local *FH; open FH, "$file" or die $!; -f FH and sysread FH, $buf, -s FH; }
      results are in $buf after the read...

      Unless you meant to write to sample.txt in the first place, and FH was just presumed open to some readable file...

      thanks,
      -Cadphile...

Re: How do I read an entire file into a string?
by bdalzell (Sexton) on Dec 28, 2011 at 22:36 UTC
    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.

Re: How do I read an entire file into a string?
by fyiman (Initiate) on Jun 15, 2016 at 00:03 UTC

    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;
      I noticed that the "end-of-line" for each line is changed from "0d 0a" to "0d 0d 0a"

      Windows converts 0x0a to 0x0d 0x0a on output unless the output file handle has been marked binary (as with binmode).

      On input, it does the reverse, converting 0x0d 0x0a to 0x0a; if the file hasn't been marked binary..

      What is happening in your case is that when the text file is written, 0x0a becomes 0x0d 0x0a. When you read it in as binary, no conversion is done so you get 0x0d 0x0a in memory...

      But when you print it to the text mode screen, the automatic conversion is done (again), so 0x0d 0x0a becomes 0x0d 0x0d 0x0a as you are seeing.

      When you omit the binmode, the 0x0d 0x0a read from disk, become just 0x0a in memory; and then when you print it out, they get converted back to 0x0d 0x0a and everything looks normal.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
      In the absence of evidence, opinion is indistinguishable from prejudice. Not understood.
Re: How do I read an entire file into a string?
by bdalzell (Sexton) on Dec 28, 2011 at 22:52 UTC
    This is slightly different code for using File::Slurp
    use strict; use File::Slurp; my $text = read_file($file);
    And here is a link to ManPage of File::Slurp

    http://rpm.pbone.net/index.php3/stat/45/idpl/17060087/numer/3/nazwa/File::Slurp

    And another thorough article on Slurping:

    http://search.cpan.org/~drolsky/File-Slurp-9999.13/extras/slurp_article.pod

    And remember 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.

    Originally posted as a Categorized Answer.