Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

How do I read an entire file into a string?

by vroom (Pope)
on Jan 11, 2000 at 02:27 UTC ( #1952=perlquestion: print w/replies, xml ) Need Help??

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 (Pope) 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 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 (Archbishop) 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1952]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2021-04-21 12:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?