in reply to Re: appending a unique marker to each url in a file
in thread appending a unique marker to each url in a file

open (FILE," $htmlfile") || die "Cannot open HTML file for parsing!: $ +!\n"; while(<FILE>) { $content="$content$_\n"; } close(FILE);
This construct is not ideal. You are interpolating the variable $content into a new string for every line. You should use concatenation and just append to the string:
while(<FILE>) { $content .= "$_\n"; }
But what you are doing now is slurping in the whole file and adding an extra newline at the end of each line (for which I see absolutely no reason). The same thing can be achieved by undefing $/ like this:
{ local $/; # undefs $/ for this block of code only open (FILE," $htmlfile") || die "Cannot open HTML file for parsing!: + $!\n"; $content = <FILE>; # reads in whole file $content =~ s/\n/\n\n/g; # if really necessary to duplicate newlines close(FILE); }

-- Hofmator

Replies are listed 'Best First'.
Re: Re: Re: appending a unique marker to each url in a file
by tachyon (Chancellor) on Aug 08, 2001 at 17:36 UTC

    How about just $content = join'', <FILE>; No need to mess with $/ and have to remember to localise it. Wo betide he who forgets to localise $, $" $/ $\

    Update

    As chipmunk points out this is slower than undef $/ for the gory details see Re: Re: Re: Re: Re: appending a unique marker to each url in a file. For big files the difference is significant, for small ones it is negligible but who wants to paint themselves into a scaling corner? It is better to undef $/, just remember to localise it.

    Ugh posted bad code again.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      Using $content = join '', <FILE>; rather than local $/; $content = <FILE> is fine, as long as you don't care that the latter is roughly an order of magnitude faster.

        An order of magnitude is not actually correct although the point is *of course* {sigh} valid. For small files the difference is negligible. The gains increase with file size. Nonetheless faster is faster is better.

        #!/usr/bin/perl -w use strict; my $file = shift; my $iter = shift; my $size = -s $file; print "File size $size Bytes\n"; my $start = time; for (1..$iter) { open FILE, $file or die $!; my $stuff = join'',<FILE>; close FILE; } printf "Join took %d seconds for %d iterations\n", (time-$start), $ite +r; $start = time; for (1..$iter) { { local $/; open FILE, $file or die $!; my $stuff = <FILE>; close FILE; } } printf "Undef \$/ took %d seconds for %d iterations\n", (time-$start), + $iter; __END__ C:\>perl test.pl bigfile.txt 2000 File size 47753 Bytes Join took 72 seconds for 2000 iterations Undef $/ took 21 seconds for 2000 iterations C:\>perl test.pl test.pl 2000 File size 878 Bytes Join took 8 seconds for 2000 iterations Undef $/ took 7 seconds for 2000 iterations C:\>

        cheers

        tachyon

        s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print