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
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|