Perl stream file lines with line numbers
Perl
use strict;
use warnings;
use autodie;
sub each_line {
my ($path, $cb) = @_;
open my $fh, '<', $path;
my $count = 0;
while (my $line = <$fh>) {
chomp $line;
$count++;
$cb->($line, $count);
}
close $fh;
return $count;
}
my $n = each_line('data.txt', sub { print "$_[1]: $_[0]\n" });
print "read $n lines\n";