Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Re: Slurp a file

by merlyn (Sage)
on Jan 08, 2001 at 21:55 UTC ( #50532=note: print w/ replies, xml ) Need Help??


in reply to Re: Slurp a file
in thread Slurp a file

And my most-frequently-used variant of that is:

my $content = do {local $/; <SOMEHANDLE> };
Short, and effective.

-- Randal L. Schwartz, Perl hacker


Comment on Re: Re: Slurp a file
Download Code
Re: Re: Re: Slurp a file
by jeroenes (Priest) on Jan 08, 2001 at 21:59 UTC
    Tilly pointed me to the use of join in this case. Methinks it's pretty easy:
    $content = join '', <SOMEHANDLE>;
    Chipmunk, just 4u: One might see the actual response of tilly in this node <grin>.

    Jeroen
    I was dreaming of guitarnotes that would irritate an executive kind of guy (FZ)

        Yes they were :) But I'm not sure I like the results. Is there a problem with this benchmarking code?

        #!/usr/bin/perl -w use strict; use Benchmark; my $file = $0; open IN, $file or die "$file: $!\n"; sub joinit { my $content = join '', <IN>; } sub dollarslash { my $content = do { local $/; <IN> } } timethese(100_000, {join => \&joinit, slash => \&dollarslash} );

        because it implies that the join is faster...

        Benchmark: timing 100000 iterations of join, slash... join: 2 wallclock secs ( 1.52 usr + 0.29 sys = 1.81 CPU) slash: 4 wallclock secs ( 2.99 usr + 0.35 sys = 3.34 CPU)
        --
        <http://www.dave.org.uk>

        "Perl makes the fun jobs fun
        and the boring jobs bearable" - me

      That will be slower though.

      The reason I suggested it is in case someone wanted to use your SuperSplit with tied filehandles. I would not depend on a tied filehandle correctly paying attention to $/, so the join makes sense.

      But if you care about performance or have good reason to believe that people won't use tied filehandles, then avoid join.

        For the best of both worlds, use both:

        my $contents= do { local($/); join '', <INPUT> };
        should work quickly for file handles that pay attention to $/ and work about as quickly as possible for those that don't.

                - tye (but my friends call me "Tye")

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (14)
As of 2014-12-18 13:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (51 votes), past polls