in reply to speeding up script that uses filehandles

Here's a slight variation of the outputting format, but definitely while and $. are speedier.

use strict; open my $hndl, '<'."bigfile.txt" or die "Open of bigfile failed $!"; while (<$hndl>) { print sprintf("% 7s",$.) . " $_ \n"; } close $hndl;


Replies are listed 'Best First'.
Re^2: speeding up script that uses filehandles
by revdiablo (Prior) on Dec 02, 2004 at 19:14 UTC

    Off-topic from the original question, but whenever you do print sprintf, you should think about just using printf directly. I think this is clearer:

    while (<$hndl>) { printf "%7d %s\n", $., $_; }