Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

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

by Anonymous Monk
on Aug 18, 2004 at 12:22 UTC ( #383935=categorized answer: print w/replies, xml ) Need Help??

Q&A > files > How do I read an entire file into a string? - Answer 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.

Replies are listed 'Best First'.
Re: Answer: How do I read an entire file into a string?
by Grygonos (Chaplain) on Aug 18, 2004 at 14:28 UTC
      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' ) ;
Log In?

What's my password?
Create A New User
[Discipulus]: floating was the keyword and.. to use supersearch is plus sign not a (plus) sin
Discipulus wonders if just numbers less than 18 are really floating..( chemical humor)
[Lady_Aleena]: Hello.

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2017-06-26 21:01 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (594 votes). Check out past polls.