#!/usr/bin/env perl

use 5.022;
use strict;
use warnings;

$| = 1;

sub left_to_right {
    my $cmp = @{$a} <=> @{$b};
    my $i = 0;
    while (!$cmp && $i < @{$a}) {
        $cmp = $a->[$i] <=> $b->[$i];
        ++$i;
    }
    $cmp
}

sub right_to_left {
    my $cmp = @{$a} <=> @{$b};
    my $i = @{$a};
    while (!$cmp && $i > 0) {
        --$i;
        $cmp = $a->[$i] <=> $b->[$i];
    }
    $cmp
}

my $precedence = \&left_to_right;
my $reversed   = 0;

while (@ARGV && $ARGV[0] =~ /^-(.+)\z/s) {
    my $opt = $1;
    shift @ARGV;
    last if '-' eq $opt;
    if ($opt =~ /^[ir]+\z/) {
        $precedence = \&right_to_left if $opt =~ /i/;
        $reversed   = 1               if $opt =~ /r/;
        next;
    }
    die "usage: numlist_sort [-i] [-r] [file]...\n";
}

my @bunch = ();
while (<<>>) {
    s/^\s+//;
    my @e = split /\s+/;
    next if !@e;

    die "integer numbers separated by whitespace expected\n"
        if grep { !/^(?:0|-?[1-9][0-9]*)\z/ } @e;

    push @bunch, \@e;
}

my @sorted = sort $precedence @bunch;

if ($reversed) {
    my $i = @sorted;
    while ($i > 0) {
        --$i;
        print "@{$sorted[$i]}\n";
    }
}
else {
    foreach my $r (@sorted) {
        print "@{$r}\n";
    }
}

__END__
=head1 NAME

numlist_sort - sort a list of lists of numbers

=head1 SYNOPSIS

  numlist_sort [-i] [-r] [file]...

=head1 DESCRIPTION

This example program reads lines with lists of integer numbers, separated
by whitespace, sorts the list of lists, first by size, then by leftmost
element, then by second element from the left, etc., and writes the
final result to standard output.

Option B<-i> flips the precedence from left-to-right to right-to-left.

Option B<-r> reverses the output order.

=head1 AUTHOR

Martin Becker, E<lt>becker-cpan-mp I<at> cozap.comE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (c) 2019-2022 by Martin Becker, Blaubeuren.

=head1 DISCLAIMER OF WARRANTY

This library is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose.

=cut
