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

Replies are listed 'Best First'.
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>.

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

      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")

        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)

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