If you have Perl version 5.10+, the (?|pattern) construct (see Extended Patterns in perlre) supports a neat approach:
I will leave you to test the exceptions.c:\@Work\Perl>perl -wMstrict -le "use 5.010; ;; use Test::More 'no_plan'; use Test::NoWarnings; ;; my $rx_rng = qr{ \A (?| (\d*) - (\d+) | (\d+) - (\d*)) \z }xms; ;; my $max = 10; ;; for my $ar_vector ( [ '0-0', 0..0 ], [ '4-6', 4..6 ], [ '-2', 0..2 ], [ '3-', 3..$max ] +, ) { my ($rs, @rng) = @$ar_vector; is_deeply [ range($rs, $max, $rx_rng) ], \@rng, qq{'$rs'}; } ;; sub range { my ($rs, $r_max, $rx) = @_; ;; my ($lo, $hi) = $rs =~ $rx or die qq{not a range: '$rs'}; $lo = 0 unless length $lo; $hi = $r_max unless length $hi; ;; die qq{range inverted: '$rs'} if $lo > $hi; die qq{max out of range: '$rs'} if $hi > $r_max; ;; return $lo .. $hi; } " ok 1 - '0-0' ok 2 - '4-6' ok 3 - '-2' ok 4 - '3-' ok 5 - no warnings 1..5
Give a man a fish: <%-(-(-(-<
In reply to Re: Specifying a range of indices via the command line
by AnomalousMonk
in thread Specifying a range of indices via the command line
by eyepopslikeamosquito
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |