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

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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.

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


In reply to Re: Re: Perl Idioms Explained - my $string = do { local $/; <FILEHANDLE> }; by antirice
in thread Perl Idioms Explained - my $string = do { local $/; <FILEHANDLE> }; by jeffa

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • 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 meditating upon the Monastery: (10)
    As of 2014-12-26 17:32 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

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





      Results (174 votes), past polls