The following Perl script prints out its input so that each line begins with a running line number:


#!/usr/bin/perl
$line = 1;
while (<>) {
  print $line++, " ", $_; }

The scalar variable $line is of course the line counter.

The loop construct is of the form
while (<>) {
process one line of input }

Assuming that you have written this script (the simpler version of it) into a file named lines, you could test it using a command of the form
./lines datafile
In particular, using the script as input to itself, you would do as follows (the details of system output vary from one system to another):

lk-hp-23 perl 251 % ./lines lines
1 #!/usr/bin/perl
2 $line = 1;
3 while (<>) {
4   print $line++, " ", $_; }
lk-hp-23 perl 252 %