#!/usr/bin/perl -CSAL

use strict;
use warnings;

my $sep= "\n";
if( @ARGV >= 4 && $ARGV[0] eq "-s" ) {
    shift @ARGV;
    $sep= shift @ARGV;
}

if( @ARGV < 2 || $ARGV[0] !~ /^ *0*\d+\s*$/ || $ARGV[0] =~ /^--?h(?:elp)?$/i ||
    $ARGV[1] !~ /^\d+$/ || (@ARGV == 3 && $ARGV[2] !~ /^\d+$/) ) {
    print <<EOF;
usage: seq.pl [ -s <sep> ] <first> <last> [ <step> ]
Output a sequence of integers separated by the string <sep> (default "\\n").
<first> gives the first number (non-negative, in decimal) and format.
It may contain leading spaces or zeros and trailing white space, causing the
numbers to be formatted accordingly.  The default is just "1".
EOF
    exit;
}

my ($format, $num, $end, $step);

$ARGV[0] =~ /^( *)(0*)(\d+)(\s*)$/;
$format= length($2) ? "$1%0".length($2.$3)."d$4" : "%".length($1.$3)."d$4";
$num= $3;
$end= $ARGV[1];
$step= $ARGV[2] // 1;

exit if $num > $end;

while( $num <= $end ) {
    printf($format, $num);
    $num += $step;
    print $sep if $num <= $end;
}

print "\n";

