Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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

( #383935=categorized answer: print w/ replies, xml ) Need Help??

Q&A > files > How do I read an entire file into a string? contributed by Anonymous Monk

If you are working with large files (or a large number of them) you might consider using File::Slurp.
It is much fast than the conventional:
{ local $/ = undef; open FILE, "myfile" or die "Couldn't open file: $!"; binmode FILE; $string = <FILE>; close FILE; }

In my case it managed to decrease the runtime of my script from 40-45 minutes to 3 minutes.

Uri (the current maintainer of File-Slurp) wrote a pretty exhaustive article on slurping.

Comment on Answer: How do I read an entire file into a string?
Download Code
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 the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2014-09-21 08:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (168 votes), past polls