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

Re: Perl Idioms Explained - my $string = do { local $/; <FILEHANDLE> };

by princepawn (Parson)
on Aug 29, 2003 at 17:55 UTC ( #287778=note: print w/replies, xml ) Need Help??

in reply to Perl Idioms Explained - my $string = do { local $/; <FILEHANDLE> };

I like:
my $slurp = join '', <FH>;

Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality... download and use The Emacs Code Browser

  • Comment on Re: Perl Idioms Explained - my $string = do { local $/; <FILEHANDLE> };
  • Download Code

Replies are listed 'Best First'.
Re: Re: Perl Idioms Explained - my $string = do { local $/; <FILEHANDLE> };
by antirice (Priest) on Aug 29, 2003 at 20:31 UTC

    A couple of things. First and foremost, don't you think that ought to be:

    my $slurp = join $/, <FH>;

    Otherwise you can't really distinguish between lines. Next, you're having perl read the file in, split the file on $/, and then rejoin everything and you end up with a string that was exactly what it was before the split. If you just localize $/ and then read the file in, you're done. Of course, I have some nice benchmarks that show the local $/ method to be a little under 5 times faster. As with any other benchmarks, YMMV.

    #!/usr/bin/perl -w use Benchmark qw(cmpthese); # file.txt is about 2200 lines each between 5 and 50 chars long open FH, "file.txt" or die $!; sub scalarcon { seek(FH,0,0); local $/; <FH>; } sub listjoin { seek(FH,0,0); join '',<FH>; } cmpthese(-5,{scalarcon=>\&scalarcon,listjoin=>\&listjoin}); __DATA__ Benchmark: running listjoin, scalarcon for at least 5 CPU seconds... listjoin: 5 wallclock secs ( 5.25 usr + 0.12 sys = 5.38 CPU) @ 30 +.70/s (n=165) scalarcon: 6 wallclock secs ( 1.95 usr + 3.36 sys = 5.30 CPU) @ 15 +3.26/s (n=813) Rate listjoin scalarcon listjoin 30.7/s -- -80% scalarcon 153/s 399% --

    Update: As chromatic has pointed out, I'm a twit.

    Hope this helps.

    The first rule of Perl club is - use Perl
    ith rule of Perl club is - follow rule i - 1 for i > 1

      Your first suggestion would seem to double the input record separator. Did you test it? Do you somehow have autochomp enabled?

      #!/usr/bin/perl -w use strict; my $line = join $/, <DATA>; print "<<$line>>\n"; __DATA__ one two tie my shoe
Re: Re: Perl Idioms Explained - my $string = do { local $/; <FILEHANDLE> };
by pbeckingham (Parson) on Mar 10, 2004 at 19:01 UTC
    I have been using
    my $slurp = join '', <FH>;
    as well, but having had the idiom explained to me, I realize that this approach is potentially doing more work than the idiom. It makes sense to me that reading from a file handle in list context causes a list to be returned, which is then collapsed by the join, and that this approach is probably not as efficient.

    I assume that the idiom is achieving this down in the (fast) IO layer, and the cost of the block, and the localized variable are lower.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2020-09-26 11:56 GMT
Find Nodes?
    Voting Booth?
    If at first I donít succeed, I Ö

    Results (141 votes). Check out past polls.