Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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?
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 pondering the Monastery: (3)
As of 2016-12-04 02:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (61 votes). Check out past polls.