Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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

by particle (Vicar)
on Jul 09, 2002 at 15:29 UTC ( #180509=categorized answer: print w/replies, xml ) Need Help??

Q&A > files > How do I read an entire file into a string? - Answer contributed by particle

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; }

Replies are listed 'Best First'.
Re: Answer: How do I read an entire file into a string?
by cadphile (Beadle) on Aug 11, 2014 at 21:09 UTC
    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...

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
QM waves to the peanut gallery, and wishes he were writing Perl today.

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2017-06-29 09:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (657 votes). Check out past polls.