| 1 | #!/usr/local/bin/perl | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =encoding utf8 | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =begin metadata | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | Name: seq | ||||||||||||||||||||||||||||
| 8 | Description: print a numeric sequence | ||||||||||||||||||||||||||||
| 9 | Author: Michael Mikonos | ||||||||||||||||||||||||||||
| 10 | License: artistic2 | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =end metadata | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =cut | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 5 | ▶ | use File::Basename qw(basename); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 5 | ▶ | use POSIX qw(floor); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 5 | ▶ | use constant EX_SUCCESS => 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 5 | ▶ | use constant EX_FAILURE => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 5 | my $Program = basename($0); | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 5 | my $begin = 1; | |||||||||||||||||||||||||||
| 27 | 5 | my $step = 1; | |||||||||||||||||||||||||||
| 28 | 5 | my $ender; | |||||||||||||||||||||||||||
| 29 | 5 | my $format = "%g"; | |||||||||||||||||||||||||||
| 30 | 5 | my $term = "\n"; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 0 | ▶ | sub usage { | ||||||||||||||||||||||||||
|
Subroutine
usage: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 33 | 0 | warn "usage: $Program [-f format] [-s string] [begin [step]] end\n"; | |||||||||||||||||||||||||||
| 34 | 0 | exit EX_FAILURE; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 5 | ▶ | while (@ARGV && $ARGV[0] =~ /^-/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@ARGV and $ARGV[0] =~ /^-/
MC/DC50%1/2
@ARGV and $ARGV[0] =~ /^-/
@ARGV
$ARGV[0] =~ /^-/
Truth table66%2/3
A and B
A= @ARGVB= $ARGV[0] =~ /^-/
| |||||||||||||||||||||||||||||
| 38 | 4 | my $opt = shift; | |||||||||||||||||||||||||||
| 39 | 4 | ▶ | if ($opt eq '--') { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 40 | 0 | last; | |||||||||||||||||||||||||||
| 41 | } elsif ($opt eq '-s') { | ||||||||||||||||||||||||||||
| 42 | 1 | $term = shift; | |||||||||||||||||||||||||||
| 43 | } elsif ($opt eq '-f') { | ||||||||||||||||||||||||||||
| 44 | 1 | $format = shift; | |||||||||||||||||||||||||||
| 45 | } elsif ($opt =~ m/\A\-?[0-9]/) { | ||||||||||||||||||||||||||||
| 46 | 2 | unshift @ARGV, $opt; | |||||||||||||||||||||||||||
| 47 | 2 | last; | |||||||||||||||||||||||||||
| 48 | } else { | ||||||||||||||||||||||||||||
| 49 | 0 | warn "$Program: unexpected option: '$opt'\n"; | |||||||||||||||||||||||||||
| 50 | 0 | usage(); | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | 5 | ▶ | if (@ARGV == 0) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 54 | 0 | usage(); | |||||||||||||||||||||||||||
| 55 | } elsif (@ARGV == 1) { | ||||||||||||||||||||||||||||
| 56 | 0 | $ender = getnum($ARGV[0]); | |||||||||||||||||||||||||||
| 57 | } elsif (@ARGV == 2) { | ||||||||||||||||||||||||||||
| 58 | 3 | $begin = getnum($ARGV[0]); | |||||||||||||||||||||||||||
| 59 | 3 | $ender = getnum($ARGV[1]); | |||||||||||||||||||||||||||
| 60 | } elsif (@ARGV == 3) { | ||||||||||||||||||||||||||||
| 61 | 2 | $begin = getnum($ARGV[0]); | |||||||||||||||||||||||||||
| 62 | 2 | $step = getnum($ARGV[1]); | |||||||||||||||||||||||||||
| 63 | 2 | $ender = getnum($ARGV[2]); | |||||||||||||||||||||||||||
| 64 | } else { | ||||||||||||||||||||||||||||
| 65 | 0 | warn "$Program: extra argument '$ARGV[3]'\n"; | |||||||||||||||||||||||||||
| 66 | 0 | usage(); | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 5 | ▶ | if ($step == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | warn "$Program: illegal step value of zero\n"; | |||||||||||||||||||||||||||
| 71 | 0 | exit EX_FAILURE; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | 5 | ▶ | if ($ender < $begin) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | if (@ARGV != 3) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 75 | 0 | $step = -$step; | |||||||||||||||||||||||||||
| 76 | } elsif ($step > 0) { | ||||||||||||||||||||||||||||
| 77 | 0 | warn "$Program: needs negative decrement\n"; | |||||||||||||||||||||||||||
| 78 | 0 | exit EX_FAILURE; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | } else { | ||||||||||||||||||||||||||||
| 81 | 5 | ▶ | if ($step < 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | warn "$Program: needs positive increment\n"; | |||||||||||||||||||||||||||
| 83 | 0 | exit EX_FAILURE; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 5 | my $head = 1; | |||||||||||||||||||||||||||
| 88 | 5 | my $count = floor(($ender - $begin) / $step); | |||||||||||||||||||||||||||
| 89 | 5 | for (0 .. $count) { | |||||||||||||||||||||||||||
| 90 | 43 | ▶ | if ($head) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 5 | $head = 0; | |||||||||||||||||||||||||||
| 92 | } else { | ||||||||||||||||||||||||||||
| 93 | 38 | print $term; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | 43 | printf $format, $begin + $_ * $step; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | 5 | print "\n"; | |||||||||||||||||||||||||||
| 98 | 5 | exit EX_SUCCESS; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 12 | ▶ | sub getnum { | ||||||||||||||||||||||||||
|
Subroutine
getnum: called CC=2 SCAR=8.4
| |||||||||||||||||||||||||||||
| 101 | 12 | my $n = shift; | |||||||||||||||||||||||||||
| 102 | 12 | ▶ | if ($n !~ m/\A[\+\-]?[0-9]+(\.[0-9]+)?\Z/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 0 | warn "$Program: invalid number '$n'\n"; | |||||||||||||||||||||||||||
| 104 | 0 | exit EX_FAILURE; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | 12 | return $n; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | __END__ | ||||||||||||||||||||||||||||