#!/usr/bin/perl -CSAL

use strict;
use warnings;


if( @ARGV && $ARGV[0] =~ /^--?h(?:elp)?$/i ) {
    print <<EOF;
usage: nl.pl [ <start> ] [ <file...> ]
Numbers lines in plain text files or stdin, starting with <start>.  <start>
gives the first line number (non-negative, in decimal) and format if present.
It may contain leading spaces or zeros and trailing white space, causing the
frame numbers to be formatted accordingly.  The default is "   1 ", i.e.
space-padded to width 4, right-aligned, starting with 1 and followed by a
single space.
EOF
    exit;
}

my ($format, $linenum);
if( @ARGV && $ARGV[0] =~ /^( *)(0*)(\d+)(\s*)$/ ) {
    $format= length($2) ? "$1%0".length($2.$3)."d$4%s" : "%".length($1.$3)."d$4%s";
    $linenum= $3;
    shift @ARGV;
}
else {
    $format= "%4d %s";
    $linenum= 1;
}

while( <> ) {
    printf($format, $linenum, $_);
    ++$linenum;
}

