| 1 | package Getopt::Euclid; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 66 | ▶ | use version; our $VERSION = version->declare('0.4.5'); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 66 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 66 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 66 | ▶ | use 5.005000; # perl 5.5.0 | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 66 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 66 | ▶ | use Symbol (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 66 | ▶ | use re 'eval'; # for matcher regex | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 66 | ▶ | use Pod::Select; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 66 | ▶ | use Pod::PlainText; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 66 | ▶ | use File::Basename; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 66 | ▶ | use File::Spec::Functions qw(splitpath catpath catfile); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 66 | ▶ | use List::Util qw( first ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 66 | ▶ | use Text::Balanced qw(extract_multiple extract_bracketed extract_variable extract_delimited); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | # Set some module variables | ||||||||||||||||||||||||||||||||||||||||||
| 20 | 66 | my $skip_keyword = 'Getopt::Euclid'; # Ignore files with a first line containing this keyword. | |||||||||||||||||||||||||||||||||||||||||
| 21 | 66 | my $pod_file_msg = "# This file was generated dynamically by $skip_keyword. Do not edit it."; | |||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 66 | my $has_run = 0; | |||||||||||||||||||||||||||||||||||||||||
| 24 | 66 | my $has_processed_pod = 0; | |||||||||||||||||||||||||||||||||||||||||
| 25 | 66 | my $export_lvl = 1; | |||||||||||||||||||||||||||||||||||||||||
| 26 | 66 | my @pod_names; | |||||||||||||||||||||||||||||||||||||||||
| 27 | my $minimal_keys; | ||||||||||||||||||||||||||||||||||||||||||
| 28 | 66 | my $vars_prefix; | |||||||||||||||||||||||||||||||||||||||||
| 29 | 66 | my $defer = 0; | |||||||||||||||||||||||||||||||||||||||||
| 30 | 66 | my $matcher; | |||||||||||||||||||||||||||||||||||||||||
| 31 | my %requireds; | ||||||||||||||||||||||||||||||||||||||||||
| 32 | 66 | my %options; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 66 | my %longnames; | |||||||||||||||||||||||||||||||||||||||||
| 34 | 66 | our $man; # --man message | |||||||||||||||||||||||||||||||||||||||||
| 35 | 66 | my $help; # --help message | |||||||||||||||||||||||||||||||||||||||||
| 36 | my $usage; # --usage message | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 66 | my $version; # --version message | |||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 66 | my $optional_re; | |||||||||||||||||||||||||||||||||||||||||
| 40 | 66 | $optional_re = qr{ \[ [^[]* (?: (??{$optional_re}) [^[]* )* \] }xms; | |||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | # Global variables | ||||||||||||||||||||||||||||||||||||||||||
| 44 | 66 | our $SCRIPT_NAME; | |||||||||||||||||||||||||||||||||||||||||
| 45 | 66 | our $SCRIPT_VERSION; # for ticket # 55259 | |||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | # Convert arg specification syntax to Perl regex syntax | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 66 | my %std_matcher_for = ( | |||||||||||||||||||||||||||||||||||||||||
| 51 | integer => '[+-]?\\d+(?:[eE][+]?\d+)?', | ||||||||||||||||||||||||||||||||||||||||||
| 52 | number => '[+-]?(?:\\d+\\.?\\d*|\\.\\d+)(?:[eE][+-]?\d+)?', | ||||||||||||||||||||||||||||||||||||||||||
| 53 | input => '\S+', | ||||||||||||||||||||||||||||||||||||||||||
| 54 | output => '\S+', | ||||||||||||||||||||||||||||||||||||||||||
| 55 | string => '\S+', | ||||||||||||||||||||||||||||||||||||||||||
| 56 | q{} => '\S+', | ||||||||||||||||||||||||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 66 | _make_equivalent( | |||||||||||||||||||||||||||||||||||||||||
| 60 | \%std_matcher_for, | ||||||||||||||||||||||||||||||||||||||||||
| 61 | integer => [qw( int i +int +i 0+int 0+i +integer 0+integer )], | ||||||||||||||||||||||||||||||||||||||||||
| 62 | number => [qw( num n +num +n 0+num 0+n +number 0+number )], | ||||||||||||||||||||||||||||||||||||||||||
| 63 | input => [qw( readable in )], | ||||||||||||||||||||||||||||||||||||||||||
| 64 | output => [qw( writable writeable out )], | ||||||||||||||||||||||||||||||||||||||||||
| 65 | string => [qw( str s )], | ||||||||||||||||||||||||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | my %std_constraint_for = ( | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 11 | ▶ | 'string' => sub { 1 }, # Always okay (matcher ensures this) | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 33 | ▶ | 'integer' => sub { 1 }, # Always okay (matcher ensures this) | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 10 | ▶ | '+integer' => sub { $_[0] > 0 }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 12 | ▶ | '0+integer' => sub { $_[0] >= 0 }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 27 | ▶ | 'number' => sub { 1 }, # Always okay (matcher ensures this) | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 18 | ▶ | '+number' => sub { $_[0] > 0 }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 21 | ▶ | '0+number' => sub { $_[0] >= 0 }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 31 | ▶ | 'input' => sub { $_[0] eq '-' || -r $_[0] }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 32 | ▶ | 'output' => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.8
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 32 | my ( $vol, $dir ) = splitpath( $_[0] ); | |||||||||||||||||||||||||||||||||||||||||
| 79 | 32 | ▶ | $dir = ($vol && $dir) ? catpath($vol, $dir) : '.'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$vol and $dir
MC/DC0%0/2
$vol and $dir
$vol
$dir
Truth table33%1/3
A and B
A= $volB= $dir
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 32 | ▶ | $_[0] eq '-' ? 1 : -e $_[0] ? -w $_[0] : -w $dir; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 66 | ); | |||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | 66 | _make_equivalent( | |||||||||||||||||||||||||||||||||||||||||
| 85 | \%std_constraint_for, | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 'integer' => [qw( int i )], | ||||||||||||||||||||||||||||||||||||||||||
| 87 | '+integer' => [qw( +int +i )], | ||||||||||||||||||||||||||||||||||||||||||
| 88 | '0+integer' => [qw( 0+int 0+i )], | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 'number' => [qw( num n )], | ||||||||||||||||||||||||||||||||||||||||||
| 90 | '+number' => [qw( +num +n )], | ||||||||||||||||||||||||||||||||||||||||||
| 91 | '0+number' => [qw( 0+num 0+n )], | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 'string' => [qw( str s )], | ||||||||||||||||||||||||||||||||||||||||||
| 93 | 'input' => [qw( in readable )], | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 'output' => [qw( out writable writeable )], | ||||||||||||||||||||||||||||||||||||||||||
| 95 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | sub Getopt::Euclid::Importer::DESTROY { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | ▶ | return if $has_run || $^C; # No errors when only compiling | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$has_run or $^C
MC/DC0%0/2
$has_run or $^C
$has_run
$^C
Truth table0%0/3
A or B
A= $has_runB= $^C
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | croak '.pm file cannot define an explicit import() when using Getopt::Euclid'; | |||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | 69 | ▶ | sub import { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=10 SCAR=23.0
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 69 | shift @_; | |||||||||||||||||||||||||||||||||||||||||
| 106 | 69 | ▶ | @_ = grep { !( /:minimal_keys/ and $minimal_keys = 1 ) } @_; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
/:minimal_keys/ && ($minimal_keys = 1)
MC/DC100%1/1
/:minimal_keys/ && ($minimal_keys = 1)
/:minimal_keys/
Truth table100%2/2
A
A= /:minimal_keys/
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 69 | ▶ | @_ = grep { !( /:vars(?:<(\w+)>)?/ and $vars_prefix = $1 || 'ARGV_' ) } @_; | ||||||||||||||||||||||||||||||||||||||||
|
Condition80%4/5
/:vars(?:<(\w+)>)?/ && ($vars_prefix = $1 || 'ARGV_')
$1 || 'ARGV_'
MC/DC100%2/2
/:vars(?:<(\w+)>)?/ && ($vars_prefix = $1 || 'ARGV_')
/:vars(?:<(\w+)>)?/
$1
Truth table100%3/3
A && B
A= /:vars(?:<(\w+)>)?/B= $1
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 69 | ▶ | @_ = grep { !( /:defer/ and $defer = 1 ) } @_; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
/:defer/ && ($defer = 1)
MC/DC100%1/1
/:defer/ && ($defer = 1)
/:defer/
Truth table100%2/2
A
A= /:defer/
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | 69 | croak "Unknown mode ('$_')" for @_; | |||||||||||||||||||||||||||||||||||||||||
| 110 | 68 | ▶ | $export_lvl++ if not $defer; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | # No POD parsing and argument processing in Perl compile mode (ticket 34195) | ||||||||||||||||||||||||||||||||||||||||||
| 113 | 68 | ▶ | return if $^C; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | # Get name of caller program and its modules in @pod_names | ||||||||||||||||||||||||||||||||||||||||||
| 116 | 67 | ▶ | return unless _get_pod_names(); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | # Extract POD of given files | ||||||||||||||||||||||||||||||||||||||||||
| 119 | 63 | __PACKAGE__->process_pods( [reverse @pod_names] ); | |||||||||||||||||||||||||||||||||||||||||
| 120 | 63 | undef @pod_names; | |||||||||||||||||||||||||||||||||||||||||
| 121 | 63 | $has_run = 1; | |||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | # Parse POD + parse and export arguments | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | ###### | ||||||||||||||||||||||||||||||||||||||||||
| 126 | #use Data::Dumper; print "ARGV: ".Dumper(\@ARGV); | ||||||||||||||||||||||||||||||||||||||||||
| 127 | ###### | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | 63 | ▶ | __PACKAGE__->process_args( \@ARGV ) unless $defer; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 36 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | 64 | ▶ | sub process_pods { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
process_pods: called CC=10 SCAR=23.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | # Extract POD content from list of Perl scripts (.pl) and modules (.pm) and | ||||||||||||||||||||||||||||||||||||||||||
| 137 | # their corresponding .pod file if available. When given the argument | ||||||||||||||||||||||||||||||||||||||||||
| 138 | # {-strict => 1}, do not look for .pod files. | ||||||||||||||||||||||||||||||||||||||||||
| 139 | 64 | my ($self, $perl_files, $args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | 64 | my $pod_string = ''; | |||||||||||||||||||||||||||||||||||||||||
| 142 | 64 | ▶ | open my $pod_fh, '>', \$pod_string | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | or croak "Could not open filehandle to variable because $!"; | ||||||||||||||||||||||||||||||||||||||||||
| 144 | 64 | for my $perl_file (@$perl_files) { | |||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | 67 | my $got_pod_file = 0; | |||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | 67 | ▶ | if ( not $args->{-strict} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | # Find corresponding .pod file | ||||||||||||||||||||||||||||||||||||||||||
| 151 | 67 | my ($name_re, $path, $suffix) = fileparse($perl_file, qr/\.[^.]*/); | |||||||||||||||||||||||||||||||||||||||||
| 152 | 67 | my $pod_file = catfile( $path, $name_re.'.pod' ); | |||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | # Get POD either from .pod file (preferably) or from Perl file | ||||||||||||||||||||||||||||||||||||||||||
| 155 | 67 | ▶ | if ( -e $pod_file ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | # Get .pod file content | ||||||||||||||||||||||||||||||||||||||||||
| 157 | 6 | ▶ | open my $in, '<', $pod_file | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | or croak "Could not open file $pod_file because $!"; | ||||||||||||||||||||||||||||||||||||||||||
| 159 | 6 | my $first_line = <$in>; | |||||||||||||||||||||||||||||||||||||||||
| 160 | 6 | chomp $first_line; | |||||||||||||||||||||||||||||||||||||||||
| 161 | 6 | ▶ | if ( not ($first_line =~ m/$skip_keyword/) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | # Skip G::E auto-generated files since they lack important data | ||||||||||||||||||||||||||||||||||||||||||
| 163 | 4 | print $pod_fh "$first_line\n"; | |||||||||||||||||||||||||||||||||||||||||
| 164 | 4 | ▶ | print $pod_fh $_ while <$in>; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | 4 | $got_pod_file = 1; | |||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||
| 167 | 6 | close $in; | |||||||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | 67 | ▶ | if (not $got_pod_file) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | # Parse POD content of Perl file | ||||||||||||||||||||||||||||||||||||||||||
| 173 | 63 | podselect( {-output => $pod_fh}, $perl_file ); | |||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||
| 175 | 67 | ▶ | print $pod_fh "\n" if $pod_string; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||
| 178 | 64 | close $pod_fh; | |||||||||||||||||||||||||||||||||||||||||
| 179 | 64 | $man = $pod_string; | |||||||||||||||||||||||||||||||||||||||||
| 180 | 64 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | 70 | ▶ | sub process_args { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
process_args: called CC=33 SCAR=35.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 185 | # First, parse the POD specifications. Then, parse the given array of | ||||||||||||||||||||||||||||||||||||||||||
| 186 | # arguments (\@ARGV or other) and populate %ARGV (or export specific | ||||||||||||||||||||||||||||||||||||||||||
| 187 | # variable names). | ||||||||||||||||||||||||||||||||||||||||||
| 188 | 70 | my ($self, $args, $options) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | # Parse POD | ||||||||||||||||||||||||||||||||||||||||||
| 191 | 70 | ▶ | if (not $has_processed_pod) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 192 | 64 | _parse_pod(); | |||||||||||||||||||||||||||||||||||||||||
| 193 | 51 | $has_processed_pod = 1; | |||||||||||||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | # Set options for argument parsing | ||||||||||||||||||||||||||||||||||||||||||
| 197 | 57 | ▶ | if (defined $options) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 198 | 2 | ▶ | if (exists $options->{-minimal_keys}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 199 | 1 | $minimal_keys = 1; | |||||||||||||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||||||||||||
| 201 | 2 | ▶ | if (exists $options->{-vars}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 202 | 1 | $vars_prefix = $options->{-vars}; | |||||||||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | 57 | %ARGV = (); | |||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||
| 208 | # Handle standard args... | ||||||||||||||||||||||||||||||||||||||||||
| 209 | 57 | ▶ | if ( first { $_ eq '--man' } @$args ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.7
Branch: 5 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 210 | 0 | _print_pod( __PACKAGE__->man(), 'paged' ); | |||||||||||||||||||||||||||||||||||||||||
| 211 | 0 | exit; | |||||||||||||||||||||||||||||||||||||||||
| 212 | 629 | ▶ | } elsif ( first { $_ eq '--usage' } @$args ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 213 | 0 | print __PACKAGE__->usage(); | |||||||||||||||||||||||||||||||||||||||||
| 214 | 0 | exit; | |||||||||||||||||||||||||||||||||||||||||
| 215 | 629 | ▶ | } elsif ( first { $_ eq '--help' } @$args ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 216 | 0 | _print_pod( __PACKAGE__->help(), 'paged' ); | |||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | exit; | |||||||||||||||||||||||||||||||||||||||||
| 218 | 629 | ▶ | } elsif ( first { $_ eq '--version' } @$args ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | 0 | print __PACKAGE__->version(); | |||||||||||||||||||||||||||||||||||||||||
| 220 | 0 | exit; | |||||||||||||||||||||||||||||||||||||||||
| 221 | 629 | ▶ | } elsif ( first { $_ eq '--podfile' } @$args ) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 222 | # Option meant for authors | ||||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | my $podfile = podfile( ); | |||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | print "Wrote POD manual in file $podfile\n"; | |||||||||||||||||||||||||||||||||||||||||
| 225 | 0 | exit; | |||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||
| 228 | # Subroutine to report problems during parsing... | ||||||||||||||||||||||||||||||||||||||||||
| 229 | 16 | ▶ | *_bad_arglist = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 230 | 16 | my (@msg) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 231 | 16 | my $msg = join q{}, @msg; | |||||||||||||||||||||||||||||||||||||||||
| 232 | 16 | $msg = _rectify_arg($msg); | |||||||||||||||||||||||||||||||||||||||||
| 233 | 16 | $msg =~ s/\n?\z/\n/xms; | |||||||||||||||||||||||||||||||||||||||||
| 234 | 16 | warn "$msg\nTry this for usage help: $SCRIPT_NAME --help\n". | |||||||||||||||||||||||||||||||||||||||||
| 235 | "Or this for full manual: $SCRIPT_NAME --man\n\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 236 | 16 | exit 2; # Traditional "bad arg list" value | |||||||||||||||||||||||||||||||||||||||||
| 237 | 57 | }; | |||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | # Run matcher... | ||||||||||||||||||||||||||||||||||||||||||
| 240 | 57 | my $argv = join( q{ }, map { $_ = _escape_arg($_) } @$args ); | |||||||||||||||||||||||||||||||||||||||||
| 241 | 57 | my $all_args_ref = { %options, %requireds }; | |||||||||||||||||||||||||||||||||||||||||
| 242 | 57 | ▶ | if ( my $error = _doesnt_match( $matcher, $argv, $all_args_ref ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 243 | 7 | _bad_arglist($error); | |||||||||||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||
| 246 | # Check that all requireds have been found... | ||||||||||||||||||||||||||||||||||||||||||
| 247 | 50 | my @missing; | |||||||||||||||||||||||||||||||||||||||||
| 248 | 50 | ▶ | while ( my ($req) = each %requireds ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 249 | 101 | ▶ | push @missing, "\t$req\n" if !exists $ARGV{$req}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||||||||||||
| 251 | _bad_arglist( | ||||||||||||||||||||||||||||||||||||||||||
| 252 | 50 | ▶ | 'Missing required argument', | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 253 | ( @missing == 1 ? q{} : q{s} ), | ||||||||||||||||||||||||||||||||||||||||||
| 254 | ":\n", @missing | ||||||||||||||||||||||||||||||||||||||||||
| 255 | ) if @missing; | ||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||
| 257 | # Back-translate \0-quoted spaces and \1-quoted tabs... | ||||||||||||||||||||||||||||||||||||||||||
| 258 | 49 | _rectify_all_args(); | |||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||
| 260 | # Check exclusive variables, variable constraints and fill in defaults... | ||||||||||||||||||||||||||||||||||||||||||
| 261 | 49 | _verify_args($all_args_ref); | |||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||
| 263 | # Clean up @$args since everything must have been parsed | ||||||||||||||||||||||||||||||||||||||||||
| 264 | 41 | @$args = (); | |||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||
| 266 | # Clean up %ARGV | ||||||||||||||||||||||||||||||||||||||||||
| 267 | 41 | for my $arg_name ( keys %ARGV ) { | |||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||
| 269 | # Flatten non-repeatables... | ||||||||||||||||||||||||||||||||||||||||||
| 270 | 260 | my $vals = delete $ARGV{$arg_name}; | |||||||||||||||||||||||||||||||||||||||||
| 271 | 260 | my $repeatable = $all_args_ref->{$arg_name}{is_repeatable}; | |||||||||||||||||||||||||||||||||||||||||
| 272 | 260 | ▶ | if ($repeatable) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 273 | 4 | pop @{$vals}; | |||||||||||||||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||
| 276 | 260 | for my $val ( @{$vals} ) { | |||||||||||||||||||||||||||||||||||||||||
| 277 | 260 | my $var_count = keys %{$val}; | |||||||||||||||||||||||||||||||||||||||||
| 278 | $val = $var_count == 0 | ||||||||||||||||||||||||||||||||||||||||||
| 279 | ? 1 # Boolean -> true | ||||||||||||||||||||||||||||||||||||||||||
| 280 | : $var_count == 1 | ||||||||||||||||||||||||||||||||||||||||||
| 281 | 260 | ▶ | ? ( values %{$val} )[0] # Single var -> var's val | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 282 | : $val # Otherwise keep hash | ||||||||||||||||||||||||||||||||||||||||||
| 283 | ; | ||||||||||||||||||||||||||||||||||||||||||
| 284 | 260 | my $false_vals = $all_args_ref->{$arg_name}{false_vals}; | |||||||||||||||||||||||||||||||||||||||||
| 285 | 260 | my %vars_opt_vals; | |||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||
| 287 | 260 | for my $arg_flag ( _get_variants($arg_name) ) { | |||||||||||||||||||||||||||||||||||||||||
| 288 | 480 | my $variant_val = $val; | |||||||||||||||||||||||||||||||||||||||||
| 289 | 480 | ▶ | if ( $false_vals && $arg_flag =~ m{\A $false_vals \z}xms ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$false_vals and $arg_flag =~ /\A $false_vals \z/msx
MC/DC100%2/2
$false_vals and $arg_flag =~ /\A $false_vals \z/msx
$false_vals
$arg_flag =~ /\A $false_vals \z/msx
Truth table100%3/3
A and B
A= $false_valsB= $arg_flag =~ /\A $false_vals \z/msx
| |||||||||||||||||||||||||||||||||||||||||||
| 290 | 14 | ▶ | $variant_val = $variant_val ? 0 : 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||
| 293 | 480 | ▶ | if ($repeatable) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 294 | 25 | push @{ $ARGV{$arg_flag} }, $variant_val; | |||||||||||||||||||||||||||||||||||||||||
| 295 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 296 | 455 | $ARGV{$arg_flag} = $variant_val; | |||||||||||||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||||||||||||
| 298 | 480 | ▶ | $vars_opt_vals{$arg_flag} = $ARGV{$arg_flag} if $vars_prefix; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||
| 301 | 260 | ▶ | if ($vars_prefix) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 302 | 28 | _minimize_entries_of( \%vars_opt_vals ); | |||||||||||||||||||||||||||||||||||||||||
| 303 | 28 | my $maximal = _longestname( keys %vars_opt_vals ); | |||||||||||||||||||||||||||||||||||||||||
| 304 | 28 | _export_var( $vars_prefix, $maximal, $vars_opt_vals{$maximal} ); | |||||||||||||||||||||||||||||||||||||||||
| 305 | 28 | delete $longnames{$maximal}; | |||||||||||||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||
| 310 | 41 | ▶ | if ($vars_prefix) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||
| 312 | # Export any unspecified options to keep use strict happy | ||||||||||||||||||||||||||||||||||||||||||
| 313 | 3 | ▶ | while ( my ($opt_name, $arg_name) = each %longnames ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 314 | 17 | my $arg_info = $all_args_ref->{$arg_name}; | |||||||||||||||||||||||||||||||||||||||||
| 315 | 17 | my $val; | |||||||||||||||||||||||||||||||||||||||||
| 316 | 17 | ▶ | if ( $arg_info->{is_repeatable} or $arg_name =~ />\.\.\./ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$arg_info->{"is_repeatable"} or $arg_name =~ />\.\.\./
MC/DC100%2/2
$arg_info->{"is_repeatable"} or $arg_name =~ />\.\.\./
$arg_info->{"is_repeatable"}
$arg_name =~ />\.\.\./
Truth table100%3/3
A or B
A= $arg_info->{"is_repeatable"}B= $arg_name =~ />\.\.\./
| |||||||||||||||||||||||||||||||||||||||||||
| 317 | # Empty arrayref for repeatable options | ||||||||||||||||||||||||||||||||||||||||||
| 318 | 3 | $val = []; | |||||||||||||||||||||||||||||||||||||||||
| 319 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 320 | 14 | ▶ | if (keys %{ $arg_info->{var} } > 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 321 | # Empty hashref for non-repeatable options with multiple placeholders | ||||||||||||||||||||||||||||||||||||||||||
| 322 | 1 | $val = {}; | |||||||||||||||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||||||||||||||||
| 325 | 17 | _export_var( $vars_prefix, $opt_name, $val ); | |||||||||||||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||
| 330 | 41 | ▶ | if ($minimal_keys) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 331 | 6 | _minimize_entries_of( \%ARGV ); | |||||||||||||||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||
| 334 | 41 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||
| 338 | 1 | ▶ | sub podfile { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
podfile: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 339 | # Write the given POD doc into a .pod file, overwriting any existing .pod file | ||||||||||||||||||||||||||||||||||||||||||
| 340 | 1 | ▶ | return if not -e $0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 341 | 1 | my ($name_re, $path, $suffix) = fileparse($0, qr/\.[^.]*/); | |||||||||||||||||||||||||||||||||||||||||
| 342 | 1 | my $pod_file = catfile( $path, $name_re.'.pod' ); | |||||||||||||||||||||||||||||||||||||||||
| 343 | 1 | ▶ | open my $out_fh, '>', $pod_file or croak "Could not write file $pod_file because $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 344 | 1 | print $out_fh $pod_file_msg."\n\n".__PACKAGE__->man(); | |||||||||||||||||||||||||||||||||||||||||
| 345 | 1 | close $out_fh; | |||||||||||||||||||||||||||||||||||||||||
| 346 | 1 | return $pod_file; | |||||||||||||||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||
| 350 | 6 | ▶ | sub man { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
man: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 351 | 6 | return $man; | |||||||||||||||||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||
| 355 | 1 | ▶ | sub usage { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
usage: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 356 | 1 | return $usage; | |||||||||||||||||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||
| 360 | 2 | ▶ | sub help { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
help: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 361 | 2 | return $help; | |||||||||||||||||||||||||||||||||||||||||
| 362 | } | ||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||
| 365 | 1 | ▶ | sub version { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
version: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 366 | 1 | return $version; | |||||||||||||||||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||
| 370 | # # # # # # # # Utility subs # # # # # # # # | ||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||
| 372 | # Recursively remove decorations on %ARGV keys | ||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||
| 374 | 9 | ▶ | sub AUTOLOAD { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 375 | 9 | our $AUTOLOAD; | |||||||||||||||||||||||||||||||||||||||||
| 376 | 9 | $AUTOLOAD =~ s{.*::}{main::}xms; | |||||||||||||||||||||||||||||||||||||||||
| 377 | 66 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 378 | 9 | goto &$AUTOLOAD; | |||||||||||||||||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||
| 382 | 64 | ▶ | sub _parse_pod { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_pod: called CC=31 SCAR=34.4
| |||||||||||||||||||||||||||||||||||||||||||
| 383 | # Set up parsing rules... | ||||||||||||||||||||||||||||||||||||||||||
| 384 | 64 | my $space_re = qr{ [^\S\n]* }xms; | |||||||||||||||||||||||||||||||||||||||||
| 385 | 64 | my $head_start_re = qr{ ^=head1 }xms; | |||||||||||||||||||||||||||||||||||||||||
| 386 | 64 | my $head_end_re = qr{ (?= $head_start_re | \z) }xms; | |||||||||||||||||||||||||||||||||||||||||
| 387 | 64 | my $pod_cmd_re = qr{ = [^\W\d]\w+ [^\n]* (?= \n\n )}xms; | |||||||||||||||||||||||||||||||||||||||||
| 388 | 64 | my $pod_cut_re = qr{ (?! \n\n ) = cut $space_re (?= \n\n )}xms; | |||||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||||||
| 390 | 64 | my $name_re = qr{ $space_re NAME $space_re \n }xms; | |||||||||||||||||||||||||||||||||||||||||
| 391 | 64 | my $vers_re = qr{ $space_re VERSION $space_re \n }xms; | |||||||||||||||||||||||||||||||||||||||||
| 392 | 64 | my $usage_re = qr{ $space_re USAGE $space_re \n }xms; | |||||||||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||||||||||
| 394 | 64 | my $std_re = qr{ STANDARD | STD | PROGRAM | SCRIPT | CLI | COMMAND(?:-|\s)?LINE }xms; | |||||||||||||||||||||||||||||||||||||||||
| 395 | 64 | my $arg_re = qr{ $space_re (?:PARAM(?:ETER)?|ARG(?:UMENT)?)S? }xms; | |||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||
| 397 | 64 | my $options_re = qr{ $space_re $std_re? $space_re OPTION(?:AL|S)? $arg_re? $space_re \n }xms; | |||||||||||||||||||||||||||||||||||||||||
| 398 | 64 | my $required_re = qr{ $space_re $std_re? $space_re (?:REQUIRED|MANDATORY) $arg_re? $space_re \n }xms; | |||||||||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||||||||||
| 400 | 64 | my $euclid_arg = qr{ ^=item \s* ([^\n]*?) \s* \n\s*\n | |||||||||||||||||||||||||||||||||||||||||
| 401 | ( | ||||||||||||||||||||||||||||||||||||||||||
| 402 | .*? | ||||||||||||||||||||||||||||||||||||||||||
| 403 | (?: | ||||||||||||||||||||||||||||||||||||||||||
| 404 | ^=for \s* (?i: Euclid) .*? \n\s*\n | ||||||||||||||||||||||||||||||||||||||||||
| 405 | | (?= ^=[^\W\d]\w* | \z) | ||||||||||||||||||||||||||||||||||||||||||
| 406 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 407 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 408 | }xms; | ||||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||
| 410 | # Clean up line delimiters | ||||||||||||||||||||||||||||||||||||||||||
| 411 | 64 | $man =~ s{ [\n\r] }{\n}gx; | |||||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||||||
| 413 | # Clean up significant entities... | ||||||||||||||||||||||||||||||||||||||||||
| 414 | 64 | $man =~ s{ E<lt> }{<}gxms; | |||||||||||||||||||||||||||||||||||||||||
| 415 | 64 | $man =~ s{ E<gt> }{>}gxms; | |||||||||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||||||||||
| 417 | # Put program name in man | ||||||||||||||||||||||||||||||||||||||||||
| 418 | 64 | ▶ | $SCRIPT_NAME = (-e $0) ? (splitpath $0)[-1] : 'one-liner'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 419 | 64 | $man =~ s{ ($head_start_re $name_re \s*) .*? (- .*)? $head_end_re } | |||||||||||||||||||||||||||||||||||||||||
| 420 | 52 | ▶ | {$1.$SCRIPT_NAME.($2 ? " $2" : "\n\n")}xems; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||||
| 422 | # Put version number in man | ||||||||||||||||||||||||||||||||||||||||||
| 423 | 64 | ($SCRIPT_VERSION) = | |||||||||||||||||||||||||||||||||||||||||
| 424 | $man =~ m/$head_start_re $vers_re .*? (\d+(?:[._]\d+)+) .*? $head_end_re /xms; | ||||||||||||||||||||||||||||||||||||||||||
| 425 | 64 | ▶ | if ( !defined $SCRIPT_VERSION ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 426 | 14 | $SCRIPT_VERSION = $main::VERSION; | |||||||||||||||||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||||||||||||||||
| 428 | 64 | ▶ | if ( !defined $SCRIPT_VERSION ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 429 | 14 | ▶ | $SCRIPT_VERSION = (-e $0) ? localtime((stat $0)[9]) : 'one-liner'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 430 | } | ||||||||||||||||||||||||||||||||||||||||||
| 431 | 64 | $man =~ s{ ($head_start_re $vers_re \s*) .*? (\s*) $head_end_re } | |||||||||||||||||||||||||||||||||||||||||
| 432 | {$1This document refers to $SCRIPT_NAME version $SCRIPT_VERSION $2}xms; | ||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||
| 434 | # Extra info from PODs | ||||||||||||||||||||||||||||||||||||||||||
| 435 | 64 | my ($options, $opt_name, $required, $req_name, $licence); | |||||||||||||||||||||||||||||||||||||||||
| 436 | 64 | ▶ | while ($man =~ m/$head_start_re ($required_re) (.*?) $head_end_re /gxms) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 437 | # Required arguments | ||||||||||||||||||||||||||||||||||||||||||
| 438 | 51 | my ( $more_req_name, $more_required ) = ($1, $2); | |||||||||||||||||||||||||||||||||||||||||
| 439 | 51 | ▶ | $req_name = $more_req_name if not defined $req_name; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 440 | 51 | ▶ | $required = ( $more_required || q{} ) . ( $required || q{} ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%2/4
$more_required || ''
$required || ''
MC/DC0%0/1
$more_required || ''
$more_required
MC/DC0%0/1
$required || ''
$required
Truth table50%1/2
A
A= $more_required
Truth table50%1/2
A
A= $required
| |||||||||||||||||||||||||||||||||||||||||||
| 441 | } | ||||||||||||||||||||||||||||||||||||||||||
| 442 | 64 | ▶ | while ($man =~ m/$head_start_re ($options_re) (.*?) $head_end_re /gxms) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 443 | # Optional arguments | ||||||||||||||||||||||||||||||||||||||||||
| 444 | 55 | my ( $more_opt_name, $more_options ) = ($1, $2); | |||||||||||||||||||||||||||||||||||||||||
| 445 | 55 | ▶ | $opt_name = $more_opt_name if not defined $opt_name; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 446 | 55 | ▶ | $options = ( $more_options || q{} ) . ( $options || q{} ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%2/4
$more_options || ''
$options || ''
MC/DC0%0/1
$more_options || ''
$more_options
MC/DC0%0/1
$options || ''
$options
Truth table50%1/2
A
A= $more_options
Truth table50%1/2
A
A= $options
| |||||||||||||||||||||||||||||||||||||||||||
| 447 | } | ||||||||||||||||||||||||||||||||||||||||||
| 448 | 64 | ▶ | while ($man =~ m/$head_start_re [^\n]+ (?i: licen[sc]e | copyright ) .*? \n \s* (.*?) \s* $head_end_re /gxms) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 449 | # License information | ||||||||||||||||||||||||||||||||||||||||||
| 450 | 47 | my ($more_licence) = ($1, $2); | |||||||||||||||||||||||||||||||||||||||||
| 451 | 47 | ▶ | $licence = ( $more_licence || q{} ) . ( $licence || q{} ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%2/4
$more_licence || ''
$licence || ''
MC/DC0%0/1
$more_licence || ''
$more_licence
MC/DC0%0/1
$licence || ''
$licence
Truth table50%1/2
A
A= $more_licence
Truth table50%1/2
A
A= $licence
| |||||||||||||||||||||||||||||||||||||||||||
| 452 | } | ||||||||||||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||||||||||||
| 454 | # Clean up interface titles... | ||||||||||||||||||||||||||||||||||||||||||
| 455 | 64 | for my $name_re ( $opt_name, $req_name ) { | |||||||||||||||||||||||||||||||||||||||||
| 456 | 128 | ▶ | next if !defined $name_re; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 457 | 106 | $name_re =~ s{\A \s+ | \s+ \z}{}gxms; | |||||||||||||||||||||||||||||||||||||||||
| 458 | } | ||||||||||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||||||||||
| 460 | # Extract the actual interface and store each arg entry into a hash of specifications... | ||||||||||||||||||||||||||||||||||||||||||
| 461 | 64 | my $seq = 0; | |||||||||||||||||||||||||||||||||||||||||
| 462 | 64 | my $seen = {}; | |||||||||||||||||||||||||||||||||||||||||
| 463 | 64 | ▶ | while ( ( $required || q{} ) =~ m{ $euclid_arg }gxms ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$required || ''
MC/DC100%1/1
$required || ''
$required
Truth table100%2/2
A
A= $required
| |||||||||||||||||||||||||||||||||||||||||||
| 464 | 120 | $seen = _register_specs( $1, $2, $seq, \%requireds, \%longnames, $seen ); | |||||||||||||||||||||||||||||||||||||||||
| 465 | 120 | $seq++; | |||||||||||||||||||||||||||||||||||||||||
| 466 | } | ||||||||||||||||||||||||||||||||||||||||||
| 467 | 64 | ▶ | while ( ( $options || q{} ) =~ m{ $euclid_arg }gxms ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$options || ''
MC/DC100%1/1
$options || ''
$options
Truth table100%2/2
A
A= $options
| |||||||||||||||||||||||||||||||||||||||||||
| 468 | 418 | $seen = _register_specs( $1, $2, $seq, \%options, \%longnames, $seen ); | |||||||||||||||||||||||||||||||||||||||||
| 469 | 417 | $seq++; | |||||||||||||||||||||||||||||||||||||||||
| 470 | } | ||||||||||||||||||||||||||||||||||||||||||
| 471 | 63 | undef $seen; | |||||||||||||||||||||||||||||||||||||||||
| 472 | 63 | _minimize_entries_of( \%longnames ); | |||||||||||||||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||||||||||||||||
| 474 | # Extract Euclid information... | ||||||||||||||||||||||||||||||||||||||||||
| 475 | 63 | my $all_specs = {%requireds, %options}; | |||||||||||||||||||||||||||||||||||||||||
| 476 | 63 | _process_euclid_specs( $all_specs ); | |||||||||||||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||||||||||||
| 478 | # Insert default values (if any) in the program's documentation | ||||||||||||||||||||||||||||||||||||||||||
| 479 | 52 | $required = _insert_default_values(\%requireds); | |||||||||||||||||||||||||||||||||||||||||
| 480 | 51 | $options = _insert_default_values(\%options ); | |||||||||||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||||||||||||
| 482 | # One-line representation of interface... | ||||||||||||||||||||||||||||||||||||||||||
| 483 | my $arg_summary = join ' ', (sort | ||||||||||||||||||||||||||||||||||||||||||
| 484 | 51 | { $requireds{$a}{'seq'} <=> $requireds{$b}{'seq'} } | |||||||||||||||||||||||||||||||||||||||||
| 485 | (keys %requireds)); | ||||||||||||||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||||||||||||
| 487 | 51 | ▶ | 1 while $arg_summary =~ s/\[ [^][]* \]//gxms; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||||||||||||
| 489 | 51 | ▶ | if ($opt_name) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 490 | 42 | ▶ | $arg_summary .= ' ' if $arg_summary; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 491 | 42 | $arg_summary .= lc "[$opt_name]"; | |||||||||||||||||||||||||||||||||||||||||
| 492 | } | ||||||||||||||||||||||||||||||||||||||||||
| 493 | 51 | $arg_summary =~ s/\s+/ /gxms; | |||||||||||||||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||||||||||||||||
| 495 | # Manual message | ||||||||||||||||||||||||||||||||||||||||||
| 496 | 51 | $man =~ s{ ($head_start_re $usage_re \s*) .*? (\s*) $head_end_re } {$1$SCRIPT_NAME $arg_summary$2}xms; | |||||||||||||||||||||||||||||||||||||||||
| 497 | 51 | $man =~ s{ ($head_start_re $required_re \s*) .*? (\s*) $head_end_re } {$1$required$2}xms; | |||||||||||||||||||||||||||||||||||||||||
| 498 | 51 | $man =~ s{ ($head_start_re $options_re \s*) .*? (\s*) $head_end_re } {$1$options$2}xms; | |||||||||||||||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||||||||||||||||
| 500 | # Usage message | ||||||||||||||||||||||||||||||||||||||||||
| 501 | 51 | $usage = " $SCRIPT_NAME $arg_summary\n"; | |||||||||||||||||||||||||||||||||||||||||
| 502 | 51 | $usage .= " $SCRIPT_NAME --help\n"; | |||||||||||||||||||||||||||||||||||||||||
| 503 | 51 | $usage .= " $SCRIPT_NAME --man\n"; | |||||||||||||||||||||||||||||||||||||||||
| 504 | 51 | $usage .= " $SCRIPT_NAME --usage\n"; | |||||||||||||||||||||||||||||||||||||||||
| 505 | 51 | $usage .= " $SCRIPT_NAME --version\n"; | |||||||||||||||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||||||||||||||||
| 507 | # Help message | ||||||||||||||||||||||||||||||||||||||||||
| 508 | 51 | $help = "=head1 \L\uUsage:\E\n\n$usage\n"; | |||||||||||||||||||||||||||||||||||||||||
| 509 | 51 | ▶ | $help .= "=head1 \L\u$req_name:\E\n\n$required\n\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$req_name || ''
MC/DC100%1/1
$req_name || ''
$req_name
Truth table100%2/2
A
A= $req_name
| |||||||||||||||||||||||||||||||||||||||||||
| 510 | if ( $req_name || q{} ) =~ /\S/; | ||||||||||||||||||||||||||||||||||||||||||
| 511 | 51 | ▶ | $help .= "=head1 \L\u$opt_name:\E\n\n$options\n\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$opt_name || ''
MC/DC100%1/1
$opt_name || ''
$opt_name
Truth table100%2/2
A
A= $opt_name
| |||||||||||||||||||||||||||||||||||||||||||
| 512 | if ( $opt_name || q{} ) =~ /\S/; | ||||||||||||||||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||||||||||||||||
| 514 | 51 | $usage = "Usage:\n".$usage; | |||||||||||||||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||||||||||||||||
| 516 | # Version message | ||||||||||||||||||||||||||||||||||||||||||
| 517 | 51 | $version = "This is $SCRIPT_NAME version $SCRIPT_VERSION\n"; | |||||||||||||||||||||||||||||||||||||||||
| 518 | 51 | ▶ | $version .= "\n$licence\n" if $licence; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||||||||||||||||
| 520 | # Convert arg specifications to regexes... | ||||||||||||||||||||||||||||||||||||||||||
| 521 | 51 | _convert_to_regex( $all_specs ); | |||||||||||||||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||||||||||||||||
| 523 | # Build matcher... | ||||||||||||||||||||||||||||||||||||||||||
| 524 | 51 | my @arg_list = ( values(%requireds), values(%options) ); | |||||||||||||||||||||||||||||||||||||||||
| 525 | 435 | $matcher = join '|', map { $_->{matcher} } | |||||||||||||||||||||||||||||||||||||||||
| 526 | 998 | sort( { $b->{name} cmp $a->{name} } grep { $_->{name} =~ /^[^<]/ } @arg_list ), | |||||||||||||||||||||||||||||||||||||||||
| 527 | 51 | sort( { $a->{seq} <=> $b->{seq} } grep { $_->{name} =~ /^[<]/ } @arg_list ); | |||||||||||||||||||||||||||||||||||||||||
| 528 | 51 | $matcher .= '|(?> (.+)) (?{ push @errors, $^N }) (?!)'; | |||||||||||||||||||||||||||||||||||||||||
| 529 | 51 | $matcher = '(?:' . $matcher . ')'; | |||||||||||||||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||||||||||||||||
| 531 | 51 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 532 | } | ||||||||||||||||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||||||||||||||||
| 535 | 538 | ▶ | sub _register_specs { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_register_specs: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 536 | 538 | my ($name_re, $spec, $seq, $storage, $longnames, $seen) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 537 | 538 | my @variants = _get_variants($name_re); | |||||||||||||||||||||||||||||||||||||||||
| 538 | 538 | $storage->{$name_re} = { | |||||||||||||||||||||||||||||||||||||||||
| 539 | seq => $seq, | ||||||||||||||||||||||||||||||||||||||||||
| 540 | src => $spec, | ||||||||||||||||||||||||||||||||||||||||||
| 541 | name => $name_re, | ||||||||||||||||||||||||||||||||||||||||||
| 542 | variants => \@variants, | ||||||||||||||||||||||||||||||||||||||||||
| 543 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 544 | 538 | ▶ | if ($minimal_keys) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 545 | 41 | my $minimal = _minimize_name($name_re); | |||||||||||||||||||||||||||||||||||||||||
| 546 | croak "Internal error: minimalist mode caused arguments ". | ||||||||||||||||||||||||||||||||||||||||||
| 547 | "'$name_re' and '".$seen->{$minimal}."' to clash" | ||||||||||||||||||||||||||||||||||||||||||
| 548 | 41 | ▶ | if $seen->{$minimal}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 549 | 40 | $seen->{$minimal} = $name_re; | |||||||||||||||||||||||||||||||||||||||||
| 550 | } | ||||||||||||||||||||||||||||||||||||||||||
| 551 | 537 | $longnames->{ _longestname(@variants) } = $name_re; | |||||||||||||||||||||||||||||||||||||||||
| 552 | 537 | return $seen; | |||||||||||||||||||||||||||||||||||||||||
| 553 | } | ||||||||||||||||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||||||||||||||||
| 556 | 63 | ▶ | sub _process_euclid_specs { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_process_euclid_specs: called CC=42 SCAR=37.4
| |||||||||||||||||||||||||||||||||||||||||||
| 557 | 63 | my ($args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 558 | 63 | my %all_var_list; | |||||||||||||||||||||||||||||||||||||||||
| 559 | my %excluded_by_def; | ||||||||||||||||||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||||||||||||||||
| 561 | ARG: | ||||||||||||||||||||||||||||||||||||||||||
| 562 | 63 | ▶ | while ( (undef, my $arg) = each %$args ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||||||||||||||||
| 564 | # Validate and record variable names seen here... | ||||||||||||||||||||||||||||||||||||||||||
| 565 | 492 | my $var_list = _validate_name( $arg->{name} ); | |||||||||||||||||||||||||||||||||||||||||
| 566 | 490 | ▶ | while (my ($var_name, undef) = each %$var_list) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 567 | 377 | $all_var_list{$var_name} = undef; | |||||||||||||||||||||||||||||||||||||||||
| 568 | } | ||||||||||||||||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||||||||||||||||
| 570 | # Process arguments with a Euclid specification further | ||||||||||||||||||||||||||||||||||||||||||
| 571 | 490 | ▶ | $arg->{src} =~ s{^ =for \s+ Euclid\b [^\n]* \s* (.*) \z}{}ixms | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 572 | or next ARG; | ||||||||||||||||||||||||||||||||||||||||||
| 573 | 240 | my $info = $1; | |||||||||||||||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||||||||||||||||
| 575 | 240 | $arg->{is_repeatable} = $info =~ s{^ \s* repeatable \s*? $}{}xms; | |||||||||||||||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||||||||||||||||
| 577 | 240 | my @false_vals; | |||||||||||||||||||||||||||||||||||||||||
| 578 | 240 | ▶ | while ( $info =~ s{^ \s* false \s*[:=] \s* ([^\n]*)}{}xms ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 579 | 10 | my $regex = $1; | |||||||||||||||||||||||||||||||||||||||||
| 580 | 10 | ▶ | 1 while $regex =~ s/ \[ ([^]]*) \] /(?:$1)?/gxms; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 581 | 0 | $regex =~ s/ (\s+) /$1.'[\\s\\0\\1]*'/egxms; | |||||||||||||||||||||||||||||||||||||||||
| 582 | 10 | push @false_vals, $regex; | |||||||||||||||||||||||||||||||||||||||||
| 583 | } | ||||||||||||||||||||||||||||||||||||||||||
| 584 | 240 | ▶ | if (@false_vals) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 585 | 8 | $arg->{false_vals} = '(?:' . join( '|', @false_vals ) . ')'; | |||||||||||||||||||||||||||||||||||||||||
| 586 | } | ||||||||||||||||||||||||||||||||||||||||||
| 587 | |||||||||||||||||||||||||||||||||||||||||||
| 588 | 240 | ▶ | while ( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 589 | $info =~ m{\G \s* (([^.]+)\.([^:=\s]+) \s*[:=]\s* ([^\n]*)) }gcxms ) | ||||||||||||||||||||||||||||||||||||||||||
| 590 | { | ||||||||||||||||||||||||||||||||||||||||||
| 591 | 408 | my ( $spec, $var, $field, $val ) = ( $1, $2, $3, $4 ); | |||||||||||||||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||||||||||||||||
| 593 | # Check for misplaced fields... | ||||||||||||||||||||||||||||||||||||||||||
| 594 | 408 | ▶ | if ( $arg->{name} !~ m{\Q<$var>}xms ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 595 | 1 | _fail( "Invalid constraint: $spec\n(No <$var> placeholder in ". | |||||||||||||||||||||||||||||||||||||||||
| 596 | "argument: $arg->{name})" ); | ||||||||||||||||||||||||||||||||||||||||||
| 597 | } | ||||||||||||||||||||||||||||||||||||||||||
| 598 | |||||||||||||||||||||||||||||||||||||||||||
| 599 | # Decode... | ||||||||||||||||||||||||||||||||||||||||||
| 600 | 407 | ▶ | if ( $field eq 'type.error' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 601 | 2 | $arg->{var}{$var}{type_error} = $val; | |||||||||||||||||||||||||||||||||||||||||
| 602 | } elsif ( $field eq 'type' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 603 | 240 | $val = _qualify_variables_fully( $val ); | |||||||||||||||||||||||||||||||||||||||||
| 604 | 240 | my ( $matchtype, $comma, $constraint ) = | |||||||||||||||||||||||||||||||||||||||||
| 605 | $val =~ m{(/(?:\.|.)+/ | [^,\s]+)\s*(?:(,))?\s*(.*)}xms; | ||||||||||||||||||||||||||||||||||||||||||
| 606 | 240 | $arg->{var}{$var}{type} = $matchtype; | |||||||||||||||||||||||||||||||||||||||||
| 607 | 240 | ▶ | if ( $comma && length $constraint ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$comma and length $constraint
MC/DC50%1/2
$comma and length $constraint
$comma
length $constraint
Truth table66%2/3
A and B
A= $commaB= length $constraint
| |||||||||||||||||||||||||||||||||||||||||||
| 608 | 18 | ( $arg->{var}{$var}{constraint_desc} = $constraint ) =~ | |||||||||||||||||||||||||||||||||||||||||
| 609 | s/\s*\b\Q$var\E\b\s*//g; | ||||||||||||||||||||||||||||||||||||||||||
| 610 | 18 | $constraint =~ s/\b\Q$var\E\b/\$_[0]/g; | |||||||||||||||||||||||||||||||||||||||||
| 611 | 18 | ▶ | $arg->{var}{$var}{constraint} = eval "sub{ $constraint }" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 612 | or _fail("Invalid .type constraint: $spec\n($@)"); | ||||||||||||||||||||||||||||||||||||||||||
| 613 | } elsif ( length $constraint ) { | ||||||||||||||||||||||||||||||||||||||||||
| 614 | 40 | $arg->{var}{$var}{constraint_desc} = $constraint; | |||||||||||||||||||||||||||||||||||||||||
| 615 | $arg->{var}{$var}{constraint} = | ||||||||||||||||||||||||||||||||||||||||||
| 616 | 40 | ▶ | eval "sub{ \$_[0] $constraint }" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 617 | or _fail("Invalid .type constraint: $spec\n($@)"); | ||||||||||||||||||||||||||||||||||||||||||
| 618 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 619 | 182 | $arg->{var}{$var}{constraint_desc} = $matchtype; | |||||||||||||||||||||||||||||||||||||||||
| 620 | $arg->{var}{$var}{constraint} = | ||||||||||||||||||||||||||||||||||||||||||
| 621 | $matchtype =~ m{\A\s*/.*/\s*\z}xms | ||||||||||||||||||||||||||||||||||||||||||
| 622 | 4 | ▶ | ? sub { 1 } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 623 | 182 | ▶ | : $std_constraint_for{$matchtype} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 624 | or _fail("Unknown .type constraint: $spec"); | ||||||||||||||||||||||||||||||||||||||||||
| 625 | } | ||||||||||||||||||||||||||||||||||||||||||
| 626 | |||||||||||||||||||||||||||||||||||||||||||
| 627 | ▶ | } elsif ( ($field eq 'default') || ($field eq 'opt_default') ) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$field eq "default" or $field eq "opt_default"
MC/DC100%2/2
$field eq "default" or $field eq "opt_default"
$field eq "default"
$field eq "opt_default"
Truth table100%3/3
A or B
A= $field eq "default"B= $field eq "opt_default"
| |||||||||||||||||||||||||||||||||||||||||||
| 628 | 157 | $val = _qualify_variables_fully( $val ); | |||||||||||||||||||||||||||||||||||||||||
| 629 | 157 | ▶ | eval "\$val = $val; 1" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 630 | or _fail("Invalid .$field value: $spec\n($@)"); | ||||||||||||||||||||||||||||||||||||||||||
| 631 | 156 | $arg->{var}{$var}{$field} = $val; | |||||||||||||||||||||||||||||||||||||||||
| 632 | 156 | my $has_field = 'has_'.$field; | |||||||||||||||||||||||||||||||||||||||||
| 633 | $arg->{$has_field} = exists $arg->{$has_field} ? | ||||||||||||||||||||||||||||||||||||||||||
| 634 | 156 | ▶ | $arg->{$has_field}++ : | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 635 | 1; | ||||||||||||||||||||||||||||||||||||||||||
| 636 | |||||||||||||||||||||||||||||||||||||||||||
| 637 | 156 | ▶ | if ($field eq 'opt_default') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 638 | # Check that placeholders with optional defaults have a flagged argument | ||||||||||||||||||||||||||||||||||||||||||
| 639 | 8 | ▶ | if ( $arg->{name} =~ m{^<}xms ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 640 | 1 | _fail( "Invalid .$field constraint: $spec\nParameter ". | |||||||||||||||||||||||||||||||||||||||||
| 641 | "$arg->{name} must have a flag" ); | ||||||||||||||||||||||||||||||||||||||||||
| 642 | } | ||||||||||||||||||||||||||||||||||||||||||
| 643 | # Check that placeholders with optional defaults is optional | ||||||||||||||||||||||||||||||||||||||||||
| 644 | 7 | ▶ | if ( $arg->{name} !~ m{\Q[<$var>]}xms ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 645 | 1 | _fail( "Invalid .$field constraint: $spec\nPlaceholder". | |||||||||||||||||||||||||||||||||||||||||
| 646 | " <$var> must be optional, i.e. [<$var>], to have ". | ||||||||||||||||||||||||||||||||||||||||||
| 647 | "an optional default in argument: $arg->{name}" ); | ||||||||||||||||||||||||||||||||||||||||||
| 648 | } | ||||||||||||||||||||||||||||||||||||||||||
| 649 | } | ||||||||||||||||||||||||||||||||||||||||||
| 650 | |||||||||||||||||||||||||||||||||||||||||||
| 651 | } elsif ( $field eq 'excludes.error' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 652 | 1 | $arg->{var}{$var}{excludes_error} = $val; | |||||||||||||||||||||||||||||||||||||||||
| 653 | } elsif ( $field eq 'excludes' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 654 | 6 | $arg->{var}{$var}{excludes} = [ split '\s*,\s*', $val ]; | |||||||||||||||||||||||||||||||||||||||||
| 655 | 6 | for my $excl_var (@{$arg->{var}{$var}{excludes}}) { | |||||||||||||||||||||||||||||||||||||||||
| 656 | 8 | ▶ | if ($var eq $excl_var) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 657 | 1 | _fail( "Invalid .excludes value for variable <$var>: ". | |||||||||||||||||||||||||||||||||||||||||
| 658 | "<$excl_var> cannot exclude itself." ); | ||||||||||||||||||||||||||||||||||||||||||
| 659 | } | ||||||||||||||||||||||||||||||||||||||||||
| 660 | } | ||||||||||||||||||||||||||||||||||||||||||
| 661 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 662 | 1 | _fail("Unknown specification: $spec"); | |||||||||||||||||||||||||||||||||||||||||
| 663 | } | ||||||||||||||||||||||||||||||||||||||||||
| 664 | } | ||||||||||||||||||||||||||||||||||||||||||
| 665 | # Record variables excluded by another that has a default | ||||||||||||||||||||||||||||||||||||||||||
| 666 | 233 | ▶ | while (my ($var_name, $var_data) = each %{$arg->{var}}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 667 | 260 | for my $excl_var (@{$arg->{var}{$var_name}{excludes}}) { | |||||||||||||||||||||||||||||||||||||||||
| 668 | 7 | ▶ | $excluded_by_def{$excl_var}{default}{$var_name} = 1 if $arg->{has_default}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 669 | 7 | ▶ | $excluded_by_def{$excl_var}{opt_default}{$var_name} = 1 if $arg->{has_opt_default}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 670 | } | ||||||||||||||||||||||||||||||||||||||||||
| 671 | } | ||||||||||||||||||||||||||||||||||||||||||
| 672 | 233 | ▶ | if ( $info =~ m{\G \s* ([^\s\0\1] [^\n]*) }gcxms ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 673 | 1 | _fail("Unknown specification: $1"); | |||||||||||||||||||||||||||||||||||||||||
| 674 | } | ||||||||||||||||||||||||||||||||||||||||||
| 675 | } | ||||||||||||||||||||||||||||||||||||||||||
| 676 | |||||||||||||||||||||||||||||||||||||||||||
| 677 | # Validate and complete .excludes specs | ||||||||||||||||||||||||||||||||||||||||||
| 678 | |||||||||||||||||||||||||||||||||||||||||||
| 679 | 53 | ▶ | while ( (undef, my $arg) = each %$args ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 680 | 446 | ▶ | while ( my ($var, $var_specs) = each %{$arg->{var}} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 681 | # Check for invalid placeholder name in .excludes specifications | ||||||||||||||||||||||||||||||||||||||||||
| 682 | 241 | for my $excl_var (@{$var_specs->{excludes}}) { | |||||||||||||||||||||||||||||||||||||||||
| 683 | 7 | ▶ | if (not exists $all_var_list{$excl_var}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 684 | 1 | _fail( "Invalid .excludes value for variable <$var>: ". | |||||||||||||||||||||||||||||||||||||||||
| 685 | "<$excl_var> does not exist\n" ); | ||||||||||||||||||||||||||||||||||||||||||
| 686 | } | ||||||||||||||||||||||||||||||||||||||||||
| 687 | } | ||||||||||||||||||||||||||||||||||||||||||
| 688 | # Remove default for placeholders excluded by others that have a default | ||||||||||||||||||||||||||||||||||||||||||
| 689 | 240 | for my $type ( 'default', 'opt_default' ) { | |||||||||||||||||||||||||||||||||||||||||
| 690 | 480 | ▶ | if ( (exists $arg->{var}->{$var}->{$type}) && (exists $excluded_by_def{$var}{$type}) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $arg->{"var"}{$var}{$type} and exists $excluded_by_def{$var}{$type}
MC/DC100%2/2
exists $arg->{"var"}{$var}{$type} and exists $excluded_by_def{$var}{$type}
exists $arg->{"var"}{$var}{$type}
exists $excluded_by_def{$var}{$type}
Truth table100%3/3
A and B
A= exists $arg->{"var"}{$var}{$type}B= exists $excluded_by_def{$var}{$type}
| |||||||||||||||||||||||||||||||||||||||||||
| 691 | 3 | delete $arg->{var}->{$var}->{$type}; | |||||||||||||||||||||||||||||||||||||||||
| 692 | 3 | $arg->{"has_$type"}--; | |||||||||||||||||||||||||||||||||||||||||
| 693 | 3 | ▶ | if ($arg->{"has_$type"} == 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 694 | 2 | delete $arg->{"has_$type"}; | |||||||||||||||||||||||||||||||||||||||||
| 695 | } | ||||||||||||||||||||||||||||||||||||||||||
| 696 | } | ||||||||||||||||||||||||||||||||||||||||||
| 697 | } | ||||||||||||||||||||||||||||||||||||||||||
| 698 | } | ||||||||||||||||||||||||||||||||||||||||||
| 699 | } | ||||||||||||||||||||||||||||||||||||||||||
| 700 | |||||||||||||||||||||||||||||||||||||||||||
| 701 | 52 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 702 | } | ||||||||||||||||||||||||||||||||||||||||||
| 703 | |||||||||||||||||||||||||||||||||||||||||||
| 704 | |||||||||||||||||||||||||||||||||||||||||||
| 705 | 397 | ▶ | sub _qualify_variables_fully { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_qualify_variables_fully: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||
| 706 | # Restore fully-qualified name to variables: | ||||||||||||||||||||||||||||||||||||||||||
| 707 | # $x becomes $main::x | ||||||||||||||||||||||||||||||||||||||||||
| 708 | # $::x becomes $main::x | ||||||||||||||||||||||||||||||||||||||||||
| 709 | # $Package::x stays as $Package::x | ||||||||||||||||||||||||||||||||||||||||||
| 710 | # /^asdf$/ stays as /^asdf$/ | ||||||||||||||||||||||||||||||||||||||||||
| 711 | # '$10' stays as '$10' | ||||||||||||||||||||||||||||||||||||||||||
| 712 | # Note: perlvar indicates that ' can also be used instead of :: | ||||||||||||||||||||||||||||||||||||||||||
| 713 | 397 | my ($val) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 714 | 397 | ▶ | if ($val =~ m/[\$\@\%]/) { # Avoid expensive Text::Balanced operations when there are no variables | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 715 | 9 | my $new_val; | |||||||||||||||||||||||||||||||||||||||||
| 716 | 9 | ▶ | for my $s (extract_multiple($val,[{Quoted=>sub{extract_delimited($_[0])}}],undef,0)) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 717 | 10 | ▶ | if (not ref $s) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 718 | # A non-quoted section... may contain variables to fix | ||||||||||||||||||||||||||||||||||||||||||
| 719 | 9 | for my $var_name ( @{_get_variable_names($s)} ) { | |||||||||||||||||||||||||||||||||||||||||
| 720 | # Skip fully qualified names, such as '$Package::x' | ||||||||||||||||||||||||||||||||||||||||||
| 721 | 11 | ▶ | next if $var_name =~ m/main(?:'|::)/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 722 | # Remove sigils from beginning of variable name: $ @ % { | ||||||||||||||||||||||||||||||||||||||||||
| 723 | 10 | $var_name =~ s/^[\$\@\%\{]+//; | |||||||||||||||||||||||||||||||||||||||||
| 724 | # Substitute non-fully qualified vars, e.g. '$x' or '$::x', by '$main::x' | ||||||||||||||||||||||||||||||||||||||||||
| 725 | 10 | my $new_name = Symbol::qualify($var_name, 'main'); | |||||||||||||||||||||||||||||||||||||||||
| 726 | 10 | ▶ | next if $new_name eq $var_name; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 727 | 9 | $var_name = quotemeta( $var_name ); | |||||||||||||||||||||||||||||||||||||||||
| 728 | 9 | $s =~ s/$var_name/$new_name/; | |||||||||||||||||||||||||||||||||||||||||
| 729 | } | ||||||||||||||||||||||||||||||||||||||||||
| 730 | 9 | $new_val .= $s; | |||||||||||||||||||||||||||||||||||||||||
| 731 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 732 | # A quoted section, to keep as-is | ||||||||||||||||||||||||||||||||||||||||||
| 733 | 1 | $new_val .= $$s; | |||||||||||||||||||||||||||||||||||||||||
| 734 | } | ||||||||||||||||||||||||||||||||||||||||||
| 735 | } | ||||||||||||||||||||||||||||||||||||||||||
| 736 | 9 | return $new_val; | |||||||||||||||||||||||||||||||||||||||||
| 737 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 738 | 388 | return $val; | |||||||||||||||||||||||||||||||||||||||||
| 739 | } | ||||||||||||||||||||||||||||||||||||||||||
| 740 | } | ||||||||||||||||||||||||||||||||||||||||||
| 741 | |||||||||||||||||||||||||||||||||||||||||||
| 742 | |||||||||||||||||||||||||||||||||||||||||||
| 743 | 9 | ▶ | sub _get_variable_names { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_variable_names: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 744 | # Get an arrayref of the variables names found in the provided string. | ||||||||||||||||||||||||||||||||||||||||||
| 745 | # This function is a hack, needed only because of Text::Balanced ticket #78855: | ||||||||||||||||||||||||||||||||||||||||||
| 746 | # https://rt.cpan.org/Public/Bug/Display.html?id=78855 | ||||||||||||||||||||||||||||||||||||||||||
| 747 | 9 | my ($str) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 748 | 9 | my $vars = []; | |||||||||||||||||||||||||||||||||||||||||
| 749 | 9 | ▶ | for my $var (extract_multiple($str,[sub{extract_variable($_[0],'')}],undef,1)) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 750 | # Name must start with underscore or a letter, e.g. $t $$h{a} ${$h}{a} $h->{a} @_ | ||||||||||||||||||||||||||||||||||||||||||
| 751 | # Skip special or invalid names, e.g. $/ $1 | ||||||||||||||||||||||||||||||||||||||||||
| 752 | 13 | my $tmp = $var; | |||||||||||||||||||||||||||||||||||||||||
| 753 | 13 | $tmp =~ s/(?:{|})//g; | |||||||||||||||||||||||||||||||||||||||||
| 754 | 13 | ▶ | next if not $tmp =~ m/^[\$\@\%]+[_a-z]/i; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 755 | 11 | push @$vars, $var; | |||||||||||||||||||||||||||||||||||||||||
| 756 | } | ||||||||||||||||||||||||||||||||||||||||||
| 757 | 9 | return $vars; | |||||||||||||||||||||||||||||||||||||||||
| 758 | } | ||||||||||||||||||||||||||||||||||||||||||
| 759 | |||||||||||||||||||||||||||||||||||||||||||
| 760 | |||||||||||||||||||||||||||||||||||||||||||
| 761 | 701 | ▶ | sub _minimize_name { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_minimize_name: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 762 | 701 | my ($name_re) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 763 | 701 | $name_re =~ s{[][]}{}gxms; # remove all square brackets | |||||||||||||||||||||||||||||||||||||||||
| 764 | 701 | $name_re =~ s{\A \W+ ([\w-]*) .* \z}{$1}gxms; | |||||||||||||||||||||||||||||||||||||||||
| 765 | 701 | $name_re =~ s{-}{_}gxms; | |||||||||||||||||||||||||||||||||||||||||
| 766 | 701 | return $name_re; | |||||||||||||||||||||||||||||||||||||||||
| 767 | } | ||||||||||||||||||||||||||||||||||||||||||
| 768 | |||||||||||||||||||||||||||||||||||||||||||
| 769 | |||||||||||||||||||||||||||||||||||||||||||
| 770 | 97 | ▶ | sub _minimize_entries_of { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_minimize_entries_of: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 771 | 97 | my ($arg_ref) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 772 | 97 | ▶ | return if ref $arg_ref ne 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 773 | |||||||||||||||||||||||||||||||||||||||||||
| 774 | 97 | for my $old_key (keys %$arg_ref) { | |||||||||||||||||||||||||||||||||||||||||
| 775 | 660 | my $new_key = _minimize_name($old_key); | |||||||||||||||||||||||||||||||||||||||||
| 776 | 660 | $arg_ref->{$new_key} = delete $arg_ref->{$old_key}; | |||||||||||||||||||||||||||||||||||||||||
| 777 | } | ||||||||||||||||||||||||||||||||||||||||||
| 778 | |||||||||||||||||||||||||||||||||||||||||||
| 779 | 97 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 780 | } | ||||||||||||||||||||||||||||||||||||||||||
| 781 | |||||||||||||||||||||||||||||||||||||||||||
| 782 | |||||||||||||||||||||||||||||||||||||||||||
| 783 | # Do match, recursively trying to expand cuddles... | ||||||||||||||||||||||||||||||||||||||||||
| 784 | 62 | ▶ | sub _doesnt_match { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_doesnt_match: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||
| 785 | 62 | my ( $matcher, $argv, $arg_specs_ref ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 786 | |||||||||||||||||||||||||||||||||||||||||||
| 787 | 62 | our @errors; # 'our' instead of 'my' because it is needed for the re pragma | |||||||||||||||||||||||||||||||||||||||||
| 788 | 62 | local @errors = (); | |||||||||||||||||||||||||||||||||||||||||
| 789 | 62 | %ARGV = (); | |||||||||||||||||||||||||||||||||||||||||
| 790 | |||||||||||||||||||||||||||||||||||||||||||
| 791 | # Match arguments, populate %ARGV and @errors | ||||||||||||||||||||||||||||||||||||||||||
| 792 | # Note that the matcher needs the pragma: use re 'eval'; | ||||||||||||||||||||||||||||||||||||||||||
| 793 | 62 | $argv =~ m{\A (?: \s* $matcher )* \s* \z}xms; | |||||||||||||||||||||||||||||||||||||||||
| 794 | |||||||||||||||||||||||||||||||||||||||||||
| 795 | # Report errors in passed arguments | ||||||||||||||||||||||||||||||||||||||||||
| 796 | 62 | for my $error (@errors) { | |||||||||||||||||||||||||||||||||||||||||
| 797 | 12 | ▶ | if ( $error =~ m/\A ((\W) (\w) (\w+))/xms ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 798 | 5 | my ( $bundle, $marker, $firstchar, $chars ) = ( $1, $2, $3, $4 ); | |||||||||||||||||||||||||||||||||||||||||
| 799 | 5 | $argv =~ s{\Q$bundle\E}{$marker$firstchar $marker$chars}xms; | |||||||||||||||||||||||||||||||||||||||||
| 800 | 5 | ▶ | return if !_doesnt_match( $matcher, $argv, $arg_specs_ref ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 801 | } | ||||||||||||||||||||||||||||||||||||||||||
| 802 | ARG: | ||||||||||||||||||||||||||||||||||||||||||
| 803 | 10 | for my $arg_spec_ref ( values %{$arg_specs_ref} ) { | |||||||||||||||||||||||||||||||||||||||||
| 804 | 36 | our $bad_type; | |||||||||||||||||||||||||||||||||||||||||
| 805 | 36 | local $bad_type; | |||||||||||||||||||||||||||||||||||||||||
| 806 | next ARG | ||||||||||||||||||||||||||||||||||||||||||
| 807 | 36 | ▶ | if $error !~ m/\A [\s\0\1]* ($arg_spec_ref->{generic_matcher})/xms | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$error =~ /\A [\s\0\1]* ($arg_spec_ref->{"generic_matcher"})/msx and $Getopt::Euclid::bad_type
MC/DC100%2/2
$error =~ /\A [\s\0\1]* ($arg_spec_ref->{"generic_matcher"})/msx and $Getopt::Euclid::bad_type
$error =~ /\A [\s\0\1]* ($arg_spec_ref->{"generic_matcher"})/msx
$Getopt::Euclid::bad_type
Truth table100%3/3
A and B
A= $error =~ /\A [\s\0\1]* ($arg_spec_ref->{"generic_matcher"})/msxB= $Getopt::Euclid::bad_type
| |||||||||||||||||||||||||||||||||||||||||||
| 808 | || !$bad_type; | ||||||||||||||||||||||||||||||||||||||||||
| 809 | |||||||||||||||||||||||||||||||||||||||||||
| 810 | my $msg = _type_error( $bad_type->{arg}, $bad_type->{var}, | ||||||||||||||||||||||||||||||||||||||||||
| 811 | 4 | $bad_type->{val}, $bad_type->{type}, $bad_type->{type_error} ); | |||||||||||||||||||||||||||||||||||||||||
| 812 | 4 | return $msg; | |||||||||||||||||||||||||||||||||||||||||
| 813 | } | ||||||||||||||||||||||||||||||||||||||||||
| 814 | 6 | return "Unknown argument: $error"; | |||||||||||||||||||||||||||||||||||||||||
| 815 | } | ||||||||||||||||||||||||||||||||||||||||||
| 816 | |||||||||||||||||||||||||||||||||||||||||||
| 817 | 50 | return 0; # No error | |||||||||||||||||||||||||||||||||||||||||
| 818 | } | ||||||||||||||||||||||||||||||||||||||||||
| 819 | |||||||||||||||||||||||||||||||||||||||||||
| 820 | |||||||||||||||||||||||||||||||||||||||||||
| 821 | 629 | ▶ | sub _escape_arg { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_escape_arg: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 822 | 629 | my $arg = shift; | |||||||||||||||||||||||||||||||||||||||||
| 823 | 629 | my ($num_replaced) = ($arg =~ tr/ \t/\0\1/); | |||||||||||||||||||||||||||||||||||||||||
| 824 | 629 | return $arg; | |||||||||||||||||||||||||||||||||||||||||
| 825 | } | ||||||||||||||||||||||||||||||||||||||||||
| 826 | |||||||||||||||||||||||||||||||||||||||||||
| 827 | |||||||||||||||||||||||||||||||||||||||||||
| 828 | 428 | ▶ | sub _rectify_arg { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_rectify_arg: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 829 | 428 | my $arg = shift; | |||||||||||||||||||||||||||||||||||||||||
| 830 | 428 | my ($num_replaced) = ($arg =~ tr/\0\1/ \t/); | |||||||||||||||||||||||||||||||||||||||||
| 831 | 428 | return $arg; | |||||||||||||||||||||||||||||||||||||||||
| 832 | } | ||||||||||||||||||||||||||||||||||||||||||
| 833 | |||||||||||||||||||||||||||||||||||||||||||
| 834 | |||||||||||||||||||||||||||||||||||||||||||
| 835 | 49 | ▶ | sub _rectify_all_args { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_rectify_all_args: called CC=7 SCAR=21.5
| |||||||||||||||||||||||||||||||||||||||||||
| 836 | 49 | ▶ | while ( my (undef, $arg_list) = each %ARGV ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 837 | 275 | for my $arg ( @{$arg_list} ) { | |||||||||||||||||||||||||||||||||||||||||
| 838 | 286 | ▶ | if ( ref $arg eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 839 | 286 | for my $var ( values %{$arg} ) { | |||||||||||||||||||||||||||||||||||||||||
| 840 | 312 | ▶ | if ( ref $var eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 841 | 35 | $var = [ map { _rectify_arg($_) } @{$var} ]; | |||||||||||||||||||||||||||||||||||||||||
| 842 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 843 | 277 | $var = _rectify_arg($var); | |||||||||||||||||||||||||||||||||||||||||
| 844 | } | ||||||||||||||||||||||||||||||||||||||||||
| 845 | } | ||||||||||||||||||||||||||||||||||||||||||
| 846 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 847 | 0 | ▶ | if ( ref $arg eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 848 | 0 | $arg = [ map { _rectify_arg($_) } @{$arg} ]; | |||||||||||||||||||||||||||||||||||||||||
| 849 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 850 | 0 | $arg = _rectify_arg($arg); | |||||||||||||||||||||||||||||||||||||||||
| 851 | } | ||||||||||||||||||||||||||||||||||||||||||
| 852 | } | ||||||||||||||||||||||||||||||||||||||||||
| 853 | } | ||||||||||||||||||||||||||||||||||||||||||
| 854 | } | ||||||||||||||||||||||||||||||||||||||||||
| 855 | 49 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 856 | } | ||||||||||||||||||||||||||||||||||||||||||
| 857 | |||||||||||||||||||||||||||||||||||||||||||
| 858 | |||||||||||||||||||||||||||||||||||||||||||
| 859 | 49 | ▶ | sub _verify_args { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_verify_args: called CC=39 SCAR=38.2
| |||||||||||||||||||||||||||||||||||||||||||
| 860 | 49 | my ($arg_specs_ref) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 861 | # Check exclusive variables, variable constraints and fill in defaults... | ||||||||||||||||||||||||||||||||||||||||||
| 862 | # Handle mutually exclusive arguments | ||||||||||||||||||||||||||||||||||||||||||
| 863 | 49 | my %seen_vars; | |||||||||||||||||||||||||||||||||||||||||
| 864 | 49 | ▶ | while ( my ($arg_name, $arg_elems) = each %ARGV ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 865 | 275 | for my $elem (@{$arg_elems}) { | |||||||||||||||||||||||||||||||||||||||||
| 866 | 286 | ▶ | while ( my ($var_name) = each (%{$elem}) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 867 | 312 | ▶ | $seen_vars{$var_name} = $arg_name if $var_name; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 868 | } | ||||||||||||||||||||||||||||||||||||||||||
| 869 | } | ||||||||||||||||||||||||||||||||||||||||||
| 870 | } | ||||||||||||||||||||||||||||||||||||||||||
| 871 | |||||||||||||||||||||||||||||||||||||||||||
| 872 | 49 | ▶ | while ( my ($arg_name, $arg) = each %{$arg_specs_ref} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 873 | 441 | ▶ | while ( my ($var_name, $var) = each %{$arg->{var}} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 874 | # Enforce placeholders that cannot be specified with others | ||||||||||||||||||||||||||||||||||||||||||
| 875 | 339 | for my $excluded_var ( @{$var->{excludes}} ) { | |||||||||||||||||||||||||||||||||||||||||
| 876 | 10 | ▶ | if (exists $seen_vars{$var_name} && | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $seen_vars{$var_name} and exists $seen_vars{$excluded_var}
MC/DC50%1/2
exists $seen_vars{$var_name} and exists $seen_vars{$excluded_var}
exists $seen_vars{$var_name}
exists $seen_vars{$excluded_var}
Truth table66%2/3
A and B
A= exists $seen_vars{$var_name}B= exists $seen_vars{$excluded_var}
| |||||||||||||||||||||||||||||||||||||||||||
| 877 | exists $seen_vars{$excluded_var}) { | ||||||||||||||||||||||||||||||||||||||||||
| 878 | 3 | my $excl_arg = $seen_vars{$excluded_var}; | |||||||||||||||||||||||||||||||||||||||||
| 879 | 3 | my $msg; | |||||||||||||||||||||||||||||||||||||||||
| 880 | 3 | ▶ | if (exists $var->{excludes_error}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 881 | 1 | $msg = $var->{excludes_error}; | |||||||||||||||||||||||||||||||||||||||||
| 882 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 883 | 2 | $msg = | |||||||||||||||||||||||||||||||||||||||||
| 884 | qq{Invalid "$excl_arg" argument.\n<$excluded_var> }. | ||||||||||||||||||||||||||||||||||||||||||
| 885 | qq{cannot be specified with <$var_name> because }. | ||||||||||||||||||||||||||||||||||||||||||
| 886 | qq{argument "$arg_name" excludes <$excluded_var>}; | ||||||||||||||||||||||||||||||||||||||||||
| 887 | } | ||||||||||||||||||||||||||||||||||||||||||
| 888 | 3 | _bad_arglist($msg); | |||||||||||||||||||||||||||||||||||||||||
| 889 | } | ||||||||||||||||||||||||||||||||||||||||||
| 890 | } | ||||||||||||||||||||||||||||||||||||||||||
| 891 | } | ||||||||||||||||||||||||||||||||||||||||||
| 892 | } | ||||||||||||||||||||||||||||||||||||||||||
| 893 | |||||||||||||||||||||||||||||||||||||||||||
| 894 | # Enforce constraints and fill in defaults... | ||||||||||||||||||||||||||||||||||||||||||
| 895 | ARG: | ||||||||||||||||||||||||||||||||||||||||||
| 896 | 46 | ▶ | while (my ($arg_name, $arg_specs) = each %{$arg_specs_ref} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 897 | |||||||||||||||||||||||||||||||||||||||||||
| 898 | # Skip non-existent/non-defaulting/non-optional-defaulting arguments | ||||||||||||||||||||||||||||||||||||||||||
| 899 | next ARG | ||||||||||||||||||||||||||||||||||||||||||
| 900 | if !exists $ARGV{$arg_name} | ||||||||||||||||||||||||||||||||||||||||||
| 901 | && !( $arg_specs->{has_default} | ||||||||||||||||||||||||||||||||||||||||||
| 902 | 390 | ▶ | || $arg_specs->{has_opt_default} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
exists $ARGV{$arg_name} or ($arg_specs->{"has_default"} or $arg_specs->{"has_opt_default"})
$arg_specs->{"has_default"} or $arg_specs->{"has_opt_default"}
MC/DC100%3/3
exists $ARGV{$arg_name} or ($arg_specs->{"has_default"} or $arg_specs->{"has_opt_default"})
exists $ARGV{$arg_name}
$arg_specs->{"has_default"}
$arg_specs->{"has_opt_default"}
Truth table100%4/4
A or B or C
A= exists $ARGV{$arg_name}B= $arg_specs->{"has_default"}C= $arg_specs->{"has_opt_default"}
| |||||||||||||||||||||||||||||||||||||||||||
| 903 | |||||||||||||||||||||||||||||||||||||||||||
| 904 | # Ensure all vars exist within arg... | ||||||||||||||||||||||||||||||||||||||||||
| 905 | 271 | my @vars = keys %{$arg_specs->{placeholders}}; | |||||||||||||||||||||||||||||||||||||||||
| 906 | 271 | for my $index ( 0 .. $#{ $ARGV{$arg_name} } ) { | |||||||||||||||||||||||||||||||||||||||||
| 907 | 247 | my $entry = $ARGV{$arg_name}[$index]; | |||||||||||||||||||||||||||||||||||||||||
| 908 | 247 | @{$entry}{@vars} = @{$entry}{@vars}; | |||||||||||||||||||||||||||||||||||||||||
| 909 | |||||||||||||||||||||||||||||||||||||||||||
| 910 | # Get arg specs... | ||||||||||||||||||||||||||||||||||||||||||
| 911 | VAR: | ||||||||||||||||||||||||||||||||||||||||||
| 912 | 247 | for my $var (@vars) { | |||||||||||||||||||||||||||||||||||||||||
| 913 | |||||||||||||||||||||||||||||||||||||||||||
| 914 | 249 | my $arg_vars = $arg_specs->{var}->{$var}; | |||||||||||||||||||||||||||||||||||||||||
| 915 | |||||||||||||||||||||||||||||||||||||||||||
| 916 | # Check constraints on vars... | ||||||||||||||||||||||||||||||||||||||||||
| 917 | 249 | ▶ | if ( exists $ARGV{$arg_name} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 918 | |||||||||||||||||||||||||||||||||||||||||||
| 919 | 249 | ▶ | if ( ref $entry eq 'HASH' && defined $entry->{$var} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
ref $entry eq "HASH" and defined $entry->{$var}
MC/DC50%1/2
ref $entry eq "HASH" and defined $entry->{$var}
ref $entry eq "HASH"
defined $entry->{$var}
Truth table66%2/3
A and B
A= ref $entry eq "HASH"B= defined $entry->{$var}
| |||||||||||||||||||||||||||||||||||||||||||
| 920 | # Named vars... | ||||||||||||||||||||||||||||||||||||||||||
| 921 | 222 | ▶ | for my $val ( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 922 | ref $entry->{$var} eq 'ARRAY' | ||||||||||||||||||||||||||||||||||||||||||
| 923 | 35 | ? @{ $entry->{$var} } | |||||||||||||||||||||||||||||||||||||||||
| 924 | : $entry->{$var} | ||||||||||||||||||||||||||||||||||||||||||
| 925 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 926 | { | ||||||||||||||||||||||||||||||||||||||||||
| 927 | 322 | ▶ | if ( $arg_vars->{constraint} && | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$arg_vars->{"constraint"} and not $arg_vars->{"constraint"}->($val)
MC/DC100%2/2
$arg_vars->{"constraint"} and not $arg_vars->{"constraint"}->($val)
$arg_vars->{"constraint"}
not $arg_vars->{"constraint"}->($val)
Truth table100%3/3
A and not(B)
A= $arg_vars->{"constraint"}B= not $arg_vars->{"constraint"}->($val)
| |||||||||||||||||||||||||||||||||||||||||||
| 928 | !$arg_vars->{constraint}->($val) ) { | ||||||||||||||||||||||||||||||||||||||||||
| 929 | _bad_arglist( _type_error($arg_name, $var, $val, | ||||||||||||||||||||||||||||||||||||||||||
| 930 | $arg_vars->{constraint_desc}, | ||||||||||||||||||||||||||||||||||||||||||
| 931 | 5 | $arg_vars->{type_error}) ); | |||||||||||||||||||||||||||||||||||||||||
| 932 | } | ||||||||||||||||||||||||||||||||||||||||||
| 933 | } | ||||||||||||||||||||||||||||||||||||||||||
| 934 | 217 | next VAR; | |||||||||||||||||||||||||||||||||||||||||
| 935 | ▶ | } elsif ( ref $entry ne 'HASH' && defined $entry ) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $entry ne "HASH" and defined $entry
MC/DC0%0/2
ref $entry ne "HASH" and defined $entry
ref $entry ne "HASH"
defined $entry
Truth table33%1/3
A and B
A= ref $entry ne "HASH"B= defined $entry
| |||||||||||||||||||||||||||||||||||||||||||
| 936 | # Unnamed vars... | ||||||||||||||||||||||||||||||||||||||||||
| 937 | 0 | ▶ | for my $val ( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 938 | ref $entry eq 'ARRAY' | ||||||||||||||||||||||||||||||||||||||||||
| 939 | 0 | ? @{$entry} | |||||||||||||||||||||||||||||||||||||||||
| 940 | : $entry | ||||||||||||||||||||||||||||||||||||||||||
| 941 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 942 | { | ||||||||||||||||||||||||||||||||||||||||||
| 943 | 0 | ▶ | if ( $arg_vars->{constraint} && | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$arg_vars->{"constraint"} and not $arg_vars->{"constraint"}->($val)
MC/DC0%0/2
$arg_vars->{"constraint"} and not $arg_vars->{"constraint"}->($val)
$arg_vars->{"constraint"}
not $arg_vars->{"constraint"}->($val)
Truth table0%0/3
A and not(B)
A= $arg_vars->{"constraint"}B= not $arg_vars->{"constraint"}->($val)
| |||||||||||||||||||||||||||||||||||||||||||
| 944 | !$arg_vars->{constraint}->($val) ) { | ||||||||||||||||||||||||||||||||||||||||||
| 945 | _bad_arglist( _type_error( $arg_name, $var, $val, | ||||||||||||||||||||||||||||||||||||||||||
| 946 | $arg_vars->{constraint_desc}, | ||||||||||||||||||||||||||||||||||||||||||
| 947 | 0 | $arg_vars->{type_error}) ); | |||||||||||||||||||||||||||||||||||||||||
| 948 | } | ||||||||||||||||||||||||||||||||||||||||||
| 949 | $entry->{$var} = '' | ||||||||||||||||||||||||||||||||||||||||||
| 950 | 0 | ▶ | unless defined( $ARGV{$arg_name} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 951 | } | ||||||||||||||||||||||||||||||||||||||||||
| 952 | 0 | next VAR; | |||||||||||||||||||||||||||||||||||||||||
| 953 | } | ||||||||||||||||||||||||||||||||||||||||||
| 954 | } | ||||||||||||||||||||||||||||||||||||||||||
| 955 | |||||||||||||||||||||||||||||||||||||||||||
| 956 | # Assign placeholder defaults (if necessary)... | ||||||||||||||||||||||||||||||||||||||||||
| 957 | next ARG | ||||||||||||||||||||||||||||||||||||||||||
| 958 | if !exists $arg_vars->{default} | ||||||||||||||||||||||||||||||||||||||||||
| 959 | 27 | ▶ | && !exists $arg_vars->{opt_default}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $arg_vars->{"default"} or exists $arg_vars->{"opt_default"}
MC/DC100%2/2
exists $arg_vars->{"default"} or exists $arg_vars->{"opt_default"}
exists $arg_vars->{"default"}
exists $arg_vars->{"opt_default"}
Truth table100%3/3
A or B
A= exists $arg_vars->{"default"}B= exists $arg_vars->{"opt_default"}
| |||||||||||||||||||||||||||||||||||||||||||
| 960 | |||||||||||||||||||||||||||||||||||||||||||
| 961 | $entry->{$var} = exists $arg_vars->{opt_default} ? | ||||||||||||||||||||||||||||||||||||||||||
| 962 | $arg_vars->{opt_default} : | ||||||||||||||||||||||||||||||||||||||||||
| 963 | 17 | ▶ | $arg_vars->{default}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 964 | } | ||||||||||||||||||||||||||||||||||||||||||
| 965 | } | ||||||||||||||||||||||||||||||||||||||||||
| 966 | |||||||||||||||||||||||||||||||||||||||||||
| 967 | # Handle defaults for missing args... | ||||||||||||||||||||||||||||||||||||||||||
| 968 | 256 | ▶ | if ( !@{ $ARGV{$arg_name} } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 969 | 35 | for my $var (@vars) { | |||||||||||||||||||||||||||||||||||||||||
| 970 | # Assign defaults (if necessary)... | ||||||||||||||||||||||||||||||||||||||||||
| 971 | 36 | my $arg_vars = $arg_specs->{var}->{$var}; | |||||||||||||||||||||||||||||||||||||||||
| 972 | next ARG | ||||||||||||||||||||||||||||||||||||||||||
| 973 | 36 | ▶ | if !exists $arg_vars->{default}; # no default specified | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 974 | |||||||||||||||||||||||||||||||||||||||||||
| 975 | # Omit default if it conflicts with a specified parameter | ||||||||||||||||||||||||||||||||||||||||||
| 976 | 31 | for my $excl_var ( @{$arg_specs->{var}->{$var}->{excludes}} ) { | |||||||||||||||||||||||||||||||||||||||||
| 977 | 5 | ▶ | if (exists $seen_vars{$excl_var}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 978 | 3 | next ARG; | |||||||||||||||||||||||||||||||||||||||||
| 979 | } | ||||||||||||||||||||||||||||||||||||||||||
| 980 | } | ||||||||||||||||||||||||||||||||||||||||||
| 981 | |||||||||||||||||||||||||||||||||||||||||||
| 982 | 28 | $ARGV{$arg_name}[0]{$var} = $arg_vars->{default}; | |||||||||||||||||||||||||||||||||||||||||
| 983 | } | ||||||||||||||||||||||||||||||||||||||||||
| 984 | } | ||||||||||||||||||||||||||||||||||||||||||
| 985 | } | ||||||||||||||||||||||||||||||||||||||||||
| 986 | 41 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 987 | } | ||||||||||||||||||||||||||||||||||||||||||
| 988 | |||||||||||||||||||||||||||||||||||||||||||
| 989 | |||||||||||||||||||||||||||||||||||||||||||
| 990 | 9 | ▶ | sub _type_error { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_type_error: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 991 | 9 | my ($arg_name, $var_name, $var_val, $var_constraint, $var_error) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 992 | 9 | my $msg = qq{Invalid "$arg_name" argument.\n}; | |||||||||||||||||||||||||||||||||||||||||
| 993 | 9 | $var_name =~ s{\W+}{}gxms; | |||||||||||||||||||||||||||||||||||||||||
| 994 | 9 | ▶ | if ( $var_error ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 995 | 3 | $msg = $var_error; | |||||||||||||||||||||||||||||||||||||||||
| 996 | 3 | $msg =~ s{(?<!<)\b$var_name\b|\b$var_name\b(?!>)}{$var_val}gxms; | |||||||||||||||||||||||||||||||||||||||||
| 997 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 998 | 6 | $msg = qq{<$var_name> must be $var_constraint but the supplied value }. | |||||||||||||||||||||||||||||||||||||||||
| 999 | qq{("$var_val") is not.}; | ||||||||||||||||||||||||||||||||||||||||||
| 1000 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1001 | 9 | return $msg; | |||||||||||||||||||||||||||||||||||||||||
| 1002 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1003 | |||||||||||||||||||||||||||||||||||||||||||
| 1004 | |||||||||||||||||||||||||||||||||||||||||||
| 1005 | 51 | ▶ | sub _convert_to_regex { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_convert_to_regex: called CC=14 SCAR=26.4
| |||||||||||||||||||||||||||||||||||||||||||
| 1006 | 51 | my ($args_ref) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 1007 | |||||||||||||||||||||||||||||||||||||||||||
| 1008 | # Regexp to capture the start of a new argument | ||||||||||||||||||||||||||||||||||||||||||
| 1009 | 51 | my $no_esc_ws = '(?!\0)'; # no escaped whitespaces | |||||||||||||||||||||||||||||||||||||||||
| 1010 | |||||||||||||||||||||||||||||||||||||||||||
| 1011 | 51 | my @arg_variants; | |||||||||||||||||||||||||||||||||||||||||
| 1012 | 51 | ▶ | while ( my ($arg_name, $arg_specs) = each %{$args_ref} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1013 | 435 | push @arg_variants, @{$arg_specs->{variants}}; | |||||||||||||||||||||||||||||||||||||||||
| 1014 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1015 | |||||||||||||||||||||||||||||||||||||||||||
| 1016 | 51 | my $no_match = join('|',@arg_variants); | |||||||||||||||||||||||||||||||||||||||||
| 1017 | 51 | $no_match = _escape_specials($no_match); | |||||||||||||||||||||||||||||||||||||||||
| 1018 | 51 | $no_match = '(?!(?:'.$no_match.')'.$no_esc_ws.')'; | |||||||||||||||||||||||||||||||||||||||||
| 1019 | |||||||||||||||||||||||||||||||||||||||||||
| 1020 | 51 | ▶ | while ( my ($arg_name, $arg) = each %{$args_ref} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1021 | 435 | my $regex = $arg_name; | |||||||||||||||||||||||||||||||||||||||||
| 1022 | |||||||||||||||||||||||||||||||||||||||||||
| 1023 | # Quotemeta specials... | ||||||||||||||||||||||||||||||||||||||||||
| 1024 | 435 | $regex = _escape_specials($regex); | |||||||||||||||||||||||||||||||||||||||||
| 1025 | 435 | $regex = "(?:$regex)"; | |||||||||||||||||||||||||||||||||||||||||
| 1026 | |||||||||||||||||||||||||||||||||||||||||||
| 1027 | # Convert optionals... | ||||||||||||||||||||||||||||||||||||||||||
| 1028 | 435 | ▶ | 1 while $regex =~ s/ \[ ([^]]*) \] /(?:$1)?/gxms; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1029 | 435 | $regex =~ s/ (\s+) /$1.'\s*'.$no_esc_ws/egxms; | |||||||||||||||||||||||||||||||||||||||||
| 1030 | 435 | my $generic = $regex; | |||||||||||||||||||||||||||||||||||||||||
| 1031 | |||||||||||||||||||||||||||||||||||||||||||
| 1032 | # Set the matcher | ||||||||||||||||||||||||||||||||||||||||||
| 1033 | $regex =~ | ||||||||||||||||||||||||||||||||||||||||||
| 1034 | s{ < (.*?) >(\.\.\.|) } | ||||||||||||||||||||||||||||||||||||||||||
| 1035 | 347 | { my ($var_name, $var_rep) = ($1, $2); | |||||||||||||||||||||||||||||||||||||||||
| 1036 | 347 | $var_name =~ s/(\s+)\[\\s\\0\\1]\*/$1/gxms; | |||||||||||||||||||||||||||||||||||||||||
| 1037 | 347 | ▶ | my $type = $arg->{var}{$var_name}{type} || q{}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$arg->{'var'}{$var_name}{'type'} || ''
MC/DC100%1/1
$arg->{'var'}{$var_name}{'type'} || ''
$arg->{'var'}{$var_name}{'type'}
Truth table100%2/2
A
A= $arg->{'var'}{$var_name}{'type'}
| |||||||||||||||||||||||||||||||||||||||||||
| 1038 | 347 | $arg->{placeholders}->{$var_name} = undef; | |||||||||||||||||||||||||||||||||||||||||
| 1039 | my $matcher = | ||||||||||||||||||||||||||||||||||||||||||
| 1040 | $type =~ m{\A\s*/.*/\s*\z}xms | ||||||||||||||||||||||||||||||||||||||||||
| 1041 | ? eval "qr$type" | ||||||||||||||||||||||||||||||||||||||||||
| 1042 | 347 | ▶ | : $std_matcher_for{ $type } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 1043 | or _fail("Unknown type ($type) in specification: $arg_name"); | ||||||||||||||||||||||||||||||||||||||||||
| 1044 | 347 | ▶ | $var_rep ? | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1045 | "(?:[\\s\\0\\1]*$no_match($matcher)(?{push \@{(\$ARGV{q{$arg_name}}||=[{}])->[-1]{q{$var_name}}}, \$^N}))+" | ||||||||||||||||||||||||||||||||||||||||||
| 1046 | : | ||||||||||||||||||||||||||||||||||||||||||
| 1047 | "(?:$no_match($matcher)(?{(\$ARGV{q{$arg_name}}||=[{}])->[-1]{q{$var_name}} = \$^N}))"; | ||||||||||||||||||||||||||||||||||||||||||
| 1048 | }gexms | ||||||||||||||||||||||||||||||||||||||||||
| 1049 | 435 | ▶ | or do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1050 | 167 | $regex .= "(?{(\$ARGV{q{$arg_name}}||=[{}])->[-1]{q{}} = 1})"; | |||||||||||||||||||||||||||||||||||||||||
| 1051 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 1052 | |||||||||||||||||||||||||||||||||||||||||||
| 1053 | 435 | ▶ | if ( $arg->{is_repeatable} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1054 | 6 | $arg->{matcher} = "$regex (?:(?<!\\w)|(?!\\w)) (?{push \@{\$ARGV{q{$arg_name}}}, {} })"; | |||||||||||||||||||||||||||||||||||||||||
| 1055 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 1056 | $arg->{matcher} = "(??{exists\$ARGV{q{$arg_name}}?'(?!)':''}) " | ||||||||||||||||||||||||||||||||||||||||||
| 1057 | . ( | ||||||||||||||||||||||||||||||||||||||||||
| 1058 | $arg->{false_vals} | ||||||||||||||||||||||||||||||||||||||||||
| 1059 | 429 | ▶ | ? "(?:$arg->{false_vals} (?:(?<!\\w)|(?!\\w)) (?{\$ARGV{q{$arg_name}} ||= [{ q{} => 0 }] }) | $regex (?:(?<!\\w)|(?!\\w)) (?{\$ARGV{q{$arg_name}} ||= [{ q{} => 1}] }))" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1060 | : "$regex (?:(?<!\\w)|(?!\\w)) (?{\$ARGV{q{$arg_name}} ||= [{}] })" | ||||||||||||||||||||||||||||||||||||||||||
| 1061 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1062 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1063 | |||||||||||||||||||||||||||||||||||||||||||
| 1064 | # Set the generic matcher | ||||||||||||||||||||||||||||||||||||||||||
| 1065 | 435 | $generic =~ | |||||||||||||||||||||||||||||||||||||||||
| 1066 | s{ < (.*?) > } | ||||||||||||||||||||||||||||||||||||||||||
| 1067 | 347 | { my $var_name = $1; | |||||||||||||||||||||||||||||||||||||||||
| 1068 | 347 | $var_name =~ s/(\s+)\[\\s\\0\\1]\*/$1/gxms; | |||||||||||||||||||||||||||||||||||||||||
| 1069 | 347 | ▶ | my $type = $arg->{var}{$var_name}{type} || q{}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$arg->{'var'}{$var_name}{'type'} || ''
MC/DC100%1/1
$arg->{'var'}{$var_name}{'type'} || ''
$arg->{'var'}{$var_name}{'type'}
Truth table100%2/2
A
A= $arg->{'var'}{$var_name}{'type'}
| |||||||||||||||||||||||||||||||||||||||||||
| 1070 | 347 | ▶ | my $type_error = $arg->{var}{$var_name}{type_error} || q{}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$arg->{'var'}{$var_name}{'type_error'} || ''
MC/DC100%1/1
$arg->{'var'}{$var_name}{'type_error'} || ''
$arg->{'var'}{$var_name}{'type_error'}
Truth table100%2/2
A
A= $arg->{'var'}{$var_name}{'type_error'}
| |||||||||||||||||||||||||||||||||||||||||||
| 1071 | my $matcher = $type =~ m{\A\s*/.*/\s*\z}xms | ||||||||||||||||||||||||||||||||||||||||||
| 1072 | ? eval "qr$type" | ||||||||||||||||||||||||||||||||||||||||||
| 1073 | 347 | ▶ | : $std_matcher_for{ $type }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1074 | 347 | "(?:($matcher|([^\\s\\0\\1]+)" | |||||||||||||||||||||||||||||||||||||||||
| 1075 | . "(?{\$bad_type ||= " | ||||||||||||||||||||||||||||||||||||||||||
| 1076 | . "{arg=>q{$arg_name},type=>q{$type},type_error=>q{$type_error}, var=>q{<$var_name>},val=>\$^N};})))" | ||||||||||||||||||||||||||||||||||||||||||
| 1077 | }gexms; | ||||||||||||||||||||||||||||||||||||||||||
| 1078 | 435 | $arg->{generic_matcher} = $generic; | |||||||||||||||||||||||||||||||||||||||||
| 1079 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1080 | 51 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 1081 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1082 | |||||||||||||||||||||||||||||||||||||||||||
| 1083 | |||||||||||||||||||||||||||||||||||||||||||
| 1084 | 486 | ▶ | sub _escape_specials { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_escape_specials: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1085 | # Escape quotemeta special characters | ||||||||||||||||||||||||||||||||||||||||||
| 1086 | 486 | my $arg = shift; | |||||||||||||||||||||||||||||||||||||||||
| 1087 | 486 | $arg =~ s{([@#\$^*()+{}?])}{\\$1}gxms; | |||||||||||||||||||||||||||||||||||||||||
| 1088 | 486 | return $arg; | |||||||||||||||||||||||||||||||||||||||||
| 1089 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1090 | |||||||||||||||||||||||||||||||||||||||||||
| 1091 | |||||||||||||||||||||||||||||||||||||||||||
| 1092 | 0 | ▶ | sub _print_pod { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_print_pod: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1093 | 0 | my ( $pod, $paged ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 1094 | |||||||||||||||||||||||||||||||||||||||||||
| 1095 | 0 | ▶ | if ($paged) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1096 | # Page output | ||||||||||||||||||||||||||||||||||||||||||
| 1097 | 0 | ▶ | eval { require IO::Pager::Page } or eval { require IO::Page }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1098 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1099 | |||||||||||||||||||||||||||||||||||||||||||
| 1100 | # Convert POD to plaintext, wrapping the lines at 76 chars and print to STDOUT | ||||||||||||||||||||||||||||||||||||||||||
| 1101 | 0 | ▶ | open my $parser_in, '<', \$pod or croak "Could not read from variable because $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1102 | 0 | Pod::PlainText->new()->parse_from_filehandle($parser_in); | |||||||||||||||||||||||||||||||||||||||||
| 1103 | 0 | close $parser_in; | |||||||||||||||||||||||||||||||||||||||||
| 1104 | |||||||||||||||||||||||||||||||||||||||||||
| 1105 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 1106 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1107 | |||||||||||||||||||||||||||||||||||||||||||
| 1108 | |||||||||||||||||||||||||||||||||||||||||||
| 1109 | 492 | ▶ | sub _validate_name { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_validate_name: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 1110 | # Check that the argument name only has pairs of < > brackets (ticket 34199) | ||||||||||||||||||||||||||||||||||||||||||
| 1111 | # Return the name of the variables that this argument specifies | ||||||||||||||||||||||||||||||||||||||||||
| 1112 | 492 | my ($name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 1113 | 492 | ▶ | if ($name =~ m/[<>]/) { # skip expensive Text::Balance functions if possible | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1114 | 302 | my %var_names; | |||||||||||||||||||||||||||||||||||||||||
| 1115 | 302 | my $pos = 0; | |||||||||||||||||||||||||||||||||||||||||
| 1116 | 302 | ▶ | for my $s (extract_multiple($name,[sub{extract_bracketed($_[0],'<>')}],undef,0)) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1117 | 839 | ▶ | next if not $s =~ m/[<>]/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1118 | 391 | $s =~ s/^<(.*)>$/$1/; | |||||||||||||||||||||||||||||||||||||||||
| 1119 | 391 | ▶ | if ( $s =~ m/[<>]/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1120 | 2 | _fail( 'Invalid argument specification: '.$name ); | |||||||||||||||||||||||||||||||||||||||||
| 1121 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1122 | 389 | $pos++; | |||||||||||||||||||||||||||||||||||||||||
| 1123 | 389 | ▶ | $var_names{$s} = $pos if not exists $var_names{$s}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1124 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1125 | 300 | return \%var_names; | |||||||||||||||||||||||||||||||||||||||||
| 1126 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 1127 | 190 | return {}; | |||||||||||||||||||||||||||||||||||||||||
| 1128 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1129 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1130 | |||||||||||||||||||||||||||||||||||||||||||
| 1131 | |||||||||||||||||||||||||||||||||||||||||||
| 1132 | 798 | ▶ | sub _get_variants { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_variants: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||
| 1133 | 798 | my @arg_desc = shift =~ m{ [^[|]+ (?: $optional_re [^[|]* )* }gmxs; | |||||||||||||||||||||||||||||||||||||||||
| 1134 | |||||||||||||||||||||||||||||||||||||||||||
| 1135 | 798 | for (@arg_desc) { | |||||||||||||||||||||||||||||||||||||||||
| 1136 | 823 | s{^ \s+ | \s+ $}{}gxms; | |||||||||||||||||||||||||||||||||||||||||
| 1137 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1138 | |||||||||||||||||||||||||||||||||||||||||||
| 1139 | # Only consider first "word"... | ||||||||||||||||||||||||||||||||||||||||||
| 1140 | 798 | ▶ | return $1 if $arg_desc[0] =~ m/\A (< [^>]+ >)/xms; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1141 | |||||||||||||||||||||||||||||||||||||||||||
| 1142 | 761 | $arg_desc[0] =~ s/\A ([^\s<]+) \s* (?: < .*)? \z/$1/xms; | |||||||||||||||||||||||||||||||||||||||||
| 1143 | |||||||||||||||||||||||||||||||||||||||||||
| 1144 | # Variants are all those with and without each optional component... | ||||||||||||||||||||||||||||||||||||||||||
| 1145 | 761 | my %variants; | |||||||||||||||||||||||||||||||||||||||||
| 1146 | 761 | ▶ | while (@arg_desc) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1147 | 3047 | my $arg_desc_with = shift @arg_desc; | |||||||||||||||||||||||||||||||||||||||||
| 1148 | 3047 | my $arg_desc_without = $arg_desc_with; | |||||||||||||||||||||||||||||||||||||||||
| 1149 | |||||||||||||||||||||||||||||||||||||||||||
| 1150 | 3047 | ▶ | if ( $arg_desc_without =~ s/ \[ [^][]* \] //xms ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1151 | 1164 | push @arg_desc, $arg_desc_without; | |||||||||||||||||||||||||||||||||||||||||
| 1152 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1153 | 3047 | ▶ | if ( $arg_desc_with =~ m/ [[(] ([^][()]*) [])] /xms ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1154 | 1164 | my $option = $1; | |||||||||||||||||||||||||||||||||||||||||
| 1155 | 1164 | for my $alternative ( split /\|/, $option ) { | |||||||||||||||||||||||||||||||||||||||||
| 1156 | 1097 | my $arg_desc = $arg_desc_with; | |||||||||||||||||||||||||||||||||||||||||
| 1157 | 1097 | $arg_desc =~ s{[[(] [^][()]* [])]}{$alternative}xms; | |||||||||||||||||||||||||||||||||||||||||
| 1158 | 1097 | push @arg_desc, $arg_desc; | |||||||||||||||||||||||||||||||||||||||||
| 1159 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1160 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1161 | |||||||||||||||||||||||||||||||||||||||||||
| 1162 | 3047 | $arg_desc_with =~ s/[][]//gxms; | |||||||||||||||||||||||||||||||||||||||||
| 1163 | 3047 | $arg_desc_with =~ s/\b[^-\w] .* \z//xms; | |||||||||||||||||||||||||||||||||||||||||
| 1164 | 3047 | $variants{$arg_desc_with} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 1165 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1166 | |||||||||||||||||||||||||||||||||||||||||||
| 1167 | 761 | return keys %variants; | |||||||||||||||||||||||||||||||||||||||||
| 1168 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1169 | |||||||||||||||||||||||||||||||||||||||||||
| 1170 | |||||||||||||||||||||||||||||||||||||||||||
| 1171 | 565 | ▶ | sub _longestname { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_longestname: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||
| 1172 | 565 | ▶ | return ( sort { length $a <=> length $b || $a cmp $b } @_ )[-1]; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
length $a <=> length $b || $a cmp $b
MC/DC0%0/2
length $a <=> length $b || $a cmp $b
length $a <=> length $b
$a cmp $b
Truth table66%2/3
A || B
A= length $a <=> length $bB= $a cmp $b
| |||||||||||||||||||||||||||||||||||||||||||
| 1173 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1174 | |||||||||||||||||||||||||||||||||||||||||||
| 1175 | |||||||||||||||||||||||||||||||||||||||||||
| 1176 | 45 | ▶ | sub _export_var { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_export_var: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1177 | 45 | my ( $prefix, $key, $value ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 1178 | 45 | my $export_as = $prefix . $key; | |||||||||||||||||||||||||||||||||||||||||
| 1179 | 45 | $export_as =~ s{\W}{_}gxms; # mainly for '-' | |||||||||||||||||||||||||||||||||||||||||
| 1180 | 45 | ▶ | my $callpkg = caller( $export_lvl + ($Exporter::ExportLevel || 0) ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$Exporter::ExportLevel || 0
MC/DC0%0/1
$Exporter::ExportLevel || 0
$Exporter::ExportLevel
Truth table50%1/2
A
A= $Exporter::ExportLevel
| |||||||||||||||||||||||||||||||||||||||||||
| 1181 | 66 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1182 | 45 | ▶ | *{"$callpkg\::$export_as"} = ( ref $value ) ? $value : \$value; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1183 | 45 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 1184 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1185 | |||||||||||||||||||||||||||||||||||||||||||
| 1186 | |||||||||||||||||||||||||||||||||||||||||||
| 1187 | # Utility sub to factor out hash key aliasing... | ||||||||||||||||||||||||||||||||||||||||||
| 1188 | 132 | ▶ | sub _make_equivalent { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_make_equivalent: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1189 | 132 | my ( $hash_ref, %alias_hash ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 1190 | |||||||||||||||||||||||||||||||||||||||||||
| 1191 | 132 | ▶ | while ( my ( $name_re, $aliases ) = each %alias_hash ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1192 | 924 | for my $alias (@$aliases) { | |||||||||||||||||||||||||||||||||||||||||
| 1193 | 2772 | $hash_ref->{$alias} = $hash_ref->{$name_re}; | |||||||||||||||||||||||||||||||||||||||||
| 1194 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1195 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1196 | |||||||||||||||||||||||||||||||||||||||||||
| 1197 | 132 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 1198 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1199 | |||||||||||||||||||||||||||||||||||||||||||
| 1200 | |||||||||||||||||||||||||||||||||||||||||||
| 1201 | # Report problems in specification and die | ||||||||||||||||||||||||||||||||||||||||||
| 1202 | 12 | ▶ | sub _fail { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_fail: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1203 | 12 | my (@msg) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 1204 | 12 | croak "Getopt::Euclid: @msg"; | |||||||||||||||||||||||||||||||||||||||||
| 1205 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1206 | |||||||||||||||||||||||||||||||||||||||||||
| 1207 | |||||||||||||||||||||||||||||||||||||||||||
| 1208 | 67 | ▶ | sub _get_pod_names { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_pod_names: called CC=5 SCAR=16.3
| |||||||||||||||||||||||||||||||||||||||||||
| 1209 | # Parse the POD of the caller program and its modules. | ||||||||||||||||||||||||||||||||||||||||||
| 1210 | 67 | my @caller = caller(1); | |||||||||||||||||||||||||||||||||||||||||
| 1211 | |||||||||||||||||||||||||||||||||||||||||||
| 1212 | # Sanity check | ||||||||||||||||||||||||||||||||||||||||||
| 1213 | 67 | ▶ | if ($has_run) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1214 | 0 | carp 'Getopt::Euclid loaded a second time'; | |||||||||||||||||||||||||||||||||||||||||
| 1215 | 0 | warn "Second attempt to parse command-line was ignored\n"; | |||||||||||||||||||||||||||||||||||||||||
| 1216 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 1217 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1218 | |||||||||||||||||||||||||||||||||||||||||||
| 1219 | # Handle calls from .pm files | ||||||||||||||||||||||||||||||||||||||||||
| 1220 | 67 | ▶ | if ( $caller[1] =~ m/[.]pm \z/xms ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1221 | 4 | my @caller = caller(1); # at import()'s level | |||||||||||||||||||||||||||||||||||||||||
| 1222 | 4 | push @pod_names, $caller[1]; | |||||||||||||||||||||||||||||||||||||||||
| 1223 | # Install this import() sub as module's import sub... | ||||||||||||||||||||||||||||||||||||||||||
| 1224 | 66 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1225 | croak '.pm file cannot define an explicit import() when using Getopt::Euclid' | ||||||||||||||||||||||||||||||||||||||||||
| 1226 | 4 | ▶ | if *{"$caller[0]::import"}{CODE}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1227 | 4 | my $lambda; # Needed so the anon sub is generated at run-time | |||||||||||||||||||||||||||||||||||||||||
| 1228 | 4 | *{"$caller[0]::import"} | |||||||||||||||||||||||||||||||||||||||||
| 1229 | 4 | ▶ | = bless sub { $lambda = 1; goto &Getopt::Euclid::import }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1230 | 4 | 'Getopt::Euclid::Importer'; | |||||||||||||||||||||||||||||||||||||||||
| 1231 | |||||||||||||||||||||||||||||||||||||||||||
| 1232 | 4 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 1233 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1234 | |||||||||||||||||||||||||||||||||||||||||||
| 1235 | # Add name of caller program | ||||||||||||||||||||||||||||||||||||||||||
| 1236 | 63 | ▶ | push @pod_names, $0 if (-e $0); # When calling perl -e '...', $0 is '-e', i.e. not a actual file | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1237 | |||||||||||||||||||||||||||||||||||||||||||
| 1238 | 63 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 1239 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1240 | |||||||||||||||||||||||||||||||||||||||||||
| 1241 | |||||||||||||||||||||||||||||||||||||||||||
| 1242 | 103 | ▶ | sub _insert_default_values { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_insert_default_values: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||
| 1243 | 103 | my ($args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 1244 | 103 | my $pod_string = ''; | |||||||||||||||||||||||||||||||||||||||||
| 1245 | # Retrieve item names in sequential order | ||||||||||||||||||||||||||||||||||||||||||
| 1246 | 103 | for my $item_name ( sort { $args->{$a}->{'seq'} <=> $args->{$b}->{'seq'} } (keys %$args) ) { | |||||||||||||||||||||||||||||||||||||||||
| 1247 | 436 | my $item_spec = $args->{$item_name}->{'src'}; | |||||||||||||||||||||||||||||||||||||||||
| 1248 | 436 | $item_spec =~ s/=for(.*)//ms; | |||||||||||||||||||||||||||||||||||||||||
| 1249 | 436 | $pod_string .= "=item $item_name\n\n"; | |||||||||||||||||||||||||||||||||||||||||
| 1250 | # Get list of variable for this argument | ||||||||||||||||||||||||||||||||||||||||||
| 1251 | 436 | ▶ | while ( my ($var_name, $var) = each %{$args->{$item_name}->{var}} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1252 | # Get default for this variable | ||||||||||||||||||||||||||||||||||||||||||
| 1253 | 236 | for my $default_type ( 'default', 'opt_default' ) { | |||||||||||||||||||||||||||||||||||||||||
| 1254 | 472 | my $var_default; | |||||||||||||||||||||||||||||||||||||||||
| 1255 | 472 | ▶ | if (exists $var->{$default_type}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1256 | 132 | ▶ | if (ref($var->{$default_type}) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 1257 | 1 | $var_default = join(' ', @{$var->{$default_type}}); | |||||||||||||||||||||||||||||||||||||||||
| 1258 | } elsif (ref($var->{$default_type}) eq '') { | ||||||||||||||||||||||||||||||||||||||||||
| 1259 | 131 | $var_default = $var->{$default_type}; | |||||||||||||||||||||||||||||||||||||||||
| 1260 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 1261 | 0 | carp 'Getopt::Euclid found an unexpected default value type'; | |||||||||||||||||||||||||||||||||||||||||
| 1262 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1263 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 1264 | 340 | $var_default = 'none'; | |||||||||||||||||||||||||||||||||||||||||
| 1265 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1266 | 472 | $item_spec =~ s/$var_name\.$default_type/$var_default/g; | |||||||||||||||||||||||||||||||||||||||||
| 1267 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1268 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1269 | 436 | ▶ | if ($item_spec =~ m/(\S+(\.(?:opt_)?default))/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1270 | 1 | my ($reference, $default_type) = ($1, $2); | |||||||||||||||||||||||||||||||||||||||||
| 1271 | 1 | _fail( "Invalid reference to field $reference in argument ". | |||||||||||||||||||||||||||||||||||||||||
| 1272 | "description:\n$item_spec" ); | ||||||||||||||||||||||||||||||||||||||||||
| 1273 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1274 | 435 | $pod_string .= $item_spec; | |||||||||||||||||||||||||||||||||||||||||
| 1275 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1276 | 102 | $pod_string = "=over\n\n".$pod_string."=back\n\n"; | |||||||||||||||||||||||||||||||||||||||||
| 1277 | 102 | return $pod_string; | |||||||||||||||||||||||||||||||||||||||||
| 1278 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1279 | |||||||||||||||||||||||||||||||||||||||||||
| 1280 | |||||||||||||||||||||||||||||||||||||||||||
| 1281 | 66 | 1; # Magic true value required at end of module | |||||||||||||||||||||||||||||||||||||||||
| 1282 | |||||||||||||||||||||||||||||||||||||||||||
| 1283 | |||||||||||||||||||||||||||||||||||||||||||
| 1284 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 1285 | |||||||||||||||||||||||||||||||||||||||||||
| 1286 | Getopt::Euclid - Executable Uniform Command-Line Interface Descriptions | ||||||||||||||||||||||||||||||||||||||||||
| 1287 | |||||||||||||||||||||||||||||||||||||||||||
| 1288 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 1289 | |||||||||||||||||||||||||||||||||||||||||||
| 1290 | This document describes Getopt::Euclid version 0.4.5 | ||||||||||||||||||||||||||||||||||||||||||
| 1291 | |||||||||||||||||||||||||||||||||||||||||||
| 1292 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 1293 | |||||||||||||||||||||||||||||||||||||||||||
| 1294 | use Getopt::Euclid; | ||||||||||||||||||||||||||||||||||||||||||
| 1295 | |||||||||||||||||||||||||||||||||||||||||||
| 1296 | if ($ARGV{-i}) { | ||||||||||||||||||||||||||||||||||||||||||
| 1297 | print "Interactive mode...\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 1298 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1299 | |||||||||||||||||||||||||||||||||||||||||||
| 1300 | for my $x (0..$ARGV{-size}{h}-1) { | ||||||||||||||||||||||||||||||||||||||||||
| 1301 | for my $y (0..$ARGV{-size}{w}-1) { | ||||||||||||||||||||||||||||||||||||||||||
| 1302 | do_something_with($x, $y); | ||||||||||||||||||||||||||||||||||||||||||
| 1303 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1304 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1305 | |||||||||||||||||||||||||||||||||||||||||||
| 1306 | __END__ | ||||||||||||||||||||||||||||||||||||||||||
| 1307 | |||||||||||||||||||||||||||||||||||||||||||
| 1308 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 1309 | |||||||||||||||||||||||||||||||||||||||||||
| 1310 | yourprog - Your program here | ||||||||||||||||||||||||||||||||||||||||||
| 1311 | |||||||||||||||||||||||||||||||||||||||||||
| 1312 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 1313 | |||||||||||||||||||||||||||||||||||||||||||
| 1314 | This documentation refers to yourprog version 1.9.4 | ||||||||||||||||||||||||||||||||||||||||||
| 1315 | |||||||||||||||||||||||||||||||||||||||||||
| 1316 | =head1 USAGE | ||||||||||||||||||||||||||||||||||||||||||
| 1317 | |||||||||||||||||||||||||||||||||||||||||||
| 1318 | yourprog [options] -s[ize]=<h>x<w> -o[ut][file] <file> | ||||||||||||||||||||||||||||||||||||||||||
| 1319 | |||||||||||||||||||||||||||||||||||||||||||
| 1320 | =head1 REQUIRED ARGUMENTS | ||||||||||||||||||||||||||||||||||||||||||
| 1321 | |||||||||||||||||||||||||||||||||||||||||||
| 1322 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 1323 | |||||||||||||||||||||||||||||||||||||||||||
| 1324 | =item -s[ize]=<h>x<w> | ||||||||||||||||||||||||||||||||||||||||||
| 1325 | |||||||||||||||||||||||||||||||||||||||||||
| 1326 | Specify size of simulation | ||||||||||||||||||||||||||||||||||||||||||
| 1327 | |||||||||||||||||||||||||||||||||||||||||||
| 1328 | =for Euclid: | ||||||||||||||||||||||||||||||||||||||||||
| 1329 | h.type: int > 0 | ||||||||||||||||||||||||||||||||||||||||||
| 1330 | h.default: 24 | ||||||||||||||||||||||||||||||||||||||||||
| 1331 | w.type: int >= 10 | ||||||||||||||||||||||||||||||||||||||||||
| 1332 | w.default: 80 | ||||||||||||||||||||||||||||||||||||||||||
| 1333 | |||||||||||||||||||||||||||||||||||||||||||
| 1334 | =item -o[ut][file] <file> | ||||||||||||||||||||||||||||||||||||||||||
| 1335 | |||||||||||||||||||||||||||||||||||||||||||
| 1336 | Specify output file | ||||||||||||||||||||||||||||||||||||||||||
| 1337 | |||||||||||||||||||||||||||||||||||||||||||
| 1338 | =for Euclid: | ||||||||||||||||||||||||||||||||||||||||||
| 1339 | file.type: writable | ||||||||||||||||||||||||||||||||||||||||||
| 1340 | file.default: '-' | ||||||||||||||||||||||||||||||||||||||||||
| 1341 | |||||||||||||||||||||||||||||||||||||||||||
| 1342 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 1343 | |||||||||||||||||||||||||||||||||||||||||||
| 1344 | =head1 OPTIONS | ||||||||||||||||||||||||||||||||||||||||||
| 1345 | |||||||||||||||||||||||||||||||||||||||||||
| 1346 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 1347 | |||||||||||||||||||||||||||||||||||||||||||
| 1348 | =item -i | ||||||||||||||||||||||||||||||||||||||||||
| 1349 | |||||||||||||||||||||||||||||||||||||||||||
| 1350 | Specify interactive simulation | ||||||||||||||||||||||||||||||||||||||||||
| 1351 | |||||||||||||||||||||||||||||||||||||||||||
| 1352 | =item -l[[en][gth]] <l> | ||||||||||||||||||||||||||||||||||||||||||
| 1353 | |||||||||||||||||||||||||||||||||||||||||||
| 1354 | Length of simulation. The default is l.default | ||||||||||||||||||||||||||||||||||||||||||
| 1355 | |||||||||||||||||||||||||||||||||||||||||||
| 1356 | =for Euclid: | ||||||||||||||||||||||||||||||||||||||||||
| 1357 | l.type: int > 0 | ||||||||||||||||||||||||||||||||||||||||||
| 1358 | l.default: 99 | ||||||||||||||||||||||||||||||||||||||||||
| 1359 | |||||||||||||||||||||||||||||||||||||||||||
| 1360 | =item --debug [<log_level>] | ||||||||||||||||||||||||||||||||||||||||||
| 1361 | |||||||||||||||||||||||||||||||||||||||||||
| 1362 | Set the log level. Default is log_level.default but if you provide --debug, | ||||||||||||||||||||||||||||||||||||||||||
| 1363 | then it is log_level.opt_default. | ||||||||||||||||||||||||||||||||||||||||||
| 1364 | |||||||||||||||||||||||||||||||||||||||||||
| 1365 | =for Euclid: | ||||||||||||||||||||||||||||||||||||||||||
| 1366 | log_level.type: int | ||||||||||||||||||||||||||||||||||||||||||
| 1367 | log_level.default: 0 | ||||||||||||||||||||||||||||||||||||||||||
| 1368 | log_level.opt_default: 1 | ||||||||||||||||||||||||||||||||||||||||||
| 1369 | |||||||||||||||||||||||||||||||||||||||||||
| 1370 | =item --version | ||||||||||||||||||||||||||||||||||||||||||
| 1371 | |||||||||||||||||||||||||||||||||||||||||||
| 1372 | =item --usage | ||||||||||||||||||||||||||||||||||||||||||
| 1373 | |||||||||||||||||||||||||||||||||||||||||||
| 1374 | =item --help | ||||||||||||||||||||||||||||||||||||||||||
| 1375 | |||||||||||||||||||||||||||||||||||||||||||
| 1376 | =item --man | ||||||||||||||||||||||||||||||||||||||||||
| 1377 | |||||||||||||||||||||||||||||||||||||||||||
| 1378 | Print the usual program information | ||||||||||||||||||||||||||||||||||||||||||
| 1379 | |||||||||||||||||||||||||||||||||||||||||||
| 1380 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 1381 | |||||||||||||||||||||||||||||||||||||||||||
| 1382 | Remainder of documentation starts here... | ||||||||||||||||||||||||||||||||||||||||||
| 1383 | |||||||||||||||||||||||||||||||||||||||||||
| 1384 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 1385 | |||||||||||||||||||||||||||||||||||||||||||
| 1386 | Damian Conway (DCONWAY@CPAN.org) | ||||||||||||||||||||||||||||||||||||||||||
| 1387 | |||||||||||||||||||||||||||||||||||||||||||
| 1388 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||
| 1389 | |||||||||||||||||||||||||||||||||||||||||||
| 1390 | There are undoubtedly serious bugs lurking somewhere in this code. | ||||||||||||||||||||||||||||||||||||||||||
| 1391 | Bug reports and other feedback are most welcome. | ||||||||||||||||||||||||||||||||||||||||||
| 1392 | |||||||||||||||||||||||||||||||||||||||||||
| 1393 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 1394 | |||||||||||||||||||||||||||||||||||||||||||
| 1395 | Copyright (c) 2005, Damian Conway. All Rights Reserved. | ||||||||||||||||||||||||||||||||||||||||||
| 1396 | This module is free software. It may be used, redistributed | ||||||||||||||||||||||||||||||||||||||||||
| 1397 | and/or modified under the terms of the Perl Artistic License | ||||||||||||||||||||||||||||||||||||||||||
| 1398 | (see http://www.perl.com/perl/misc/Artistic.html) | ||||||||||||||||||||||||||||||||||||||||||
| 1399 | |||||||||||||||||||||||||||||||||||||||||||
| 1400 | |||||||||||||||||||||||||||||||||||||||||||
| 1401 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 1402 | |||||||||||||||||||||||||||||||||||||||||||
| 1403 | Getopt::Euclid uses your program's own POD documentation to create a powerful | ||||||||||||||||||||||||||||||||||||||||||
| 1404 | command-line argument parser. This ensures that your program's documented interface | ||||||||||||||||||||||||||||||||||||||||||
| 1405 | and its actual interface always agree. | ||||||||||||||||||||||||||||||||||||||||||
| 1406 | |||||||||||||||||||||||||||||||||||||||||||
| 1407 | The created command-line argument parser includes many features such as argument | ||||||||||||||||||||||||||||||||||||||||||
| 1408 | type checking, required arguments, exclusive arguments, optional arguments with | ||||||||||||||||||||||||||||||||||||||||||
| 1409 | default values, automatic usage message, ... | ||||||||||||||||||||||||||||||||||||||||||
| 1410 | |||||||||||||||||||||||||||||||||||||||||||
| 1411 | To use the module, simply write the following at the top of your program: | ||||||||||||||||||||||||||||||||||||||||||
| 1412 | |||||||||||||||||||||||||||||||||||||||||||
| 1413 | use Getopt::Euclid; | ||||||||||||||||||||||||||||||||||||||||||
| 1414 | |||||||||||||||||||||||||||||||||||||||||||
| 1415 | This will cause Getopt::Euclid to be require'd and its import method will be | ||||||||||||||||||||||||||||||||||||||||||
| 1416 | called. It is important that the import method be allowed to run, so do not | ||||||||||||||||||||||||||||||||||||||||||
| 1417 | invoke Getopt::Euclid in the following manner: | ||||||||||||||||||||||||||||||||||||||||||
| 1418 | |||||||||||||||||||||||||||||||||||||||||||
| 1419 | # Will not work | ||||||||||||||||||||||||||||||||||||||||||
| 1420 | use Getopt::Euclid (); | ||||||||||||||||||||||||||||||||||||||||||
| 1421 | |||||||||||||||||||||||||||||||||||||||||||
| 1422 | When the module is loaded within a regular Perl program, it will: | ||||||||||||||||||||||||||||||||||||||||||
| 1423 | |||||||||||||||||||||||||||||||||||||||||||
| 1424 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 1425 | |||||||||||||||||||||||||||||||||||||||||||
| 1426 | =item 1. | ||||||||||||||||||||||||||||||||||||||||||
| 1427 | |||||||||||||||||||||||||||||||||||||||||||
| 1428 | locate any POD in the same *.pl file or its associated *.pod file. | ||||||||||||||||||||||||||||||||||||||||||
| 1429 | |||||||||||||||||||||||||||||||||||||||||||
| 1430 | =item 2. | ||||||||||||||||||||||||||||||||||||||||||
| 1431 | |||||||||||||||||||||||||||||||||||||||||||
| 1432 | extract information from that POD, most especially from | ||||||||||||||||||||||||||||||||||||||||||
| 1433 | the C<=head1 REQUIRED ARGUMENTS> and C<=head1 OPTIONS> sections, | ||||||||||||||||||||||||||||||||||||||||||
| 1434 | |||||||||||||||||||||||||||||||||||||||||||
| 1435 | =item 3. | ||||||||||||||||||||||||||||||||||||||||||
| 1436 | |||||||||||||||||||||||||||||||||||||||||||
| 1437 | build a parser that parses the arguments and options the POD specifies, | ||||||||||||||||||||||||||||||||||||||||||
| 1438 | |||||||||||||||||||||||||||||||||||||||||||
| 1439 | =item 4. | ||||||||||||||||||||||||||||||||||||||||||
| 1440 | |||||||||||||||||||||||||||||||||||||||||||
| 1441 | remove the command-line arguments from C<@ARGV> and parse them, and | ||||||||||||||||||||||||||||||||||||||||||
| 1442 | |||||||||||||||||||||||||||||||||||||||||||
| 1443 | =item 5. | ||||||||||||||||||||||||||||||||||||||||||
| 1444 | |||||||||||||||||||||||||||||||||||||||||||
| 1445 | put the results in the global C<%ARGV> variable (or into specifically named | ||||||||||||||||||||||||||||||||||||||||||
| 1446 | optional variables, if you request that -- see L<Exporting option variables>). | ||||||||||||||||||||||||||||||||||||||||||
| 1447 | |||||||||||||||||||||||||||||||||||||||||||
| 1448 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 1449 | |||||||||||||||||||||||||||||||||||||||||||
| 1450 | As a special case, if the module is loaded within some other module | ||||||||||||||||||||||||||||||||||||||||||
| 1451 | (i.e. from within a C<.pm> file), it still locates and extracts POD | ||||||||||||||||||||||||||||||||||||||||||
| 1452 | information, but instead of parsing C<@ARGV> immediately, it caches that | ||||||||||||||||||||||||||||||||||||||||||
| 1453 | information and installs an C<import()> subroutine in the caller module. | ||||||||||||||||||||||||||||||||||||||||||
| 1454 | This new C<import()> acts just like Getopt::Euclid's own import, except | ||||||||||||||||||||||||||||||||||||||||||
| 1455 | that it adds the POD from the caller module to the POD of the callee. | ||||||||||||||||||||||||||||||||||||||||||
| 1456 | |||||||||||||||||||||||||||||||||||||||||||
| 1457 | All of which just means you can put some or all of your CLI specification | ||||||||||||||||||||||||||||||||||||||||||
| 1458 | in a module, rather than in the application's source file. | ||||||||||||||||||||||||||||||||||||||||||
| 1459 | See L<Module interface> for more details. | ||||||||||||||||||||||||||||||||||||||||||
| 1460 | |||||||||||||||||||||||||||||||||||||||||||
| 1461 | =head1 INTERFACE | ||||||||||||||||||||||||||||||||||||||||||
| 1462 | |||||||||||||||||||||||||||||||||||||||||||
| 1463 | =head2 Program interface | ||||||||||||||||||||||||||||||||||||||||||
| 1464 | |||||||||||||||||||||||||||||||||||||||||||
| 1465 | You write: | ||||||||||||||||||||||||||||||||||||||||||
| 1466 | |||||||||||||||||||||||||||||||||||||||||||
| 1467 | use Getopt::Euclid; | ||||||||||||||||||||||||||||||||||||||||||
| 1468 | |||||||||||||||||||||||||||||||||||||||||||
| 1469 | and your command-line is parsed automagically. | ||||||||||||||||||||||||||||||||||||||||||
| 1470 | |||||||||||||||||||||||||||||||||||||||||||
| 1471 | =head2 Module interface | ||||||||||||||||||||||||||||||||||||||||||
| 1472 | |||||||||||||||||||||||||||||||||||||||||||
| 1473 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 1474 | |||||||||||||||||||||||||||||||||||||||||||
| 1475 | =item import() | ||||||||||||||||||||||||||||||||||||||||||
| 1476 | |||||||||||||||||||||||||||||||||||||||||||
| 1477 | You write: | ||||||||||||||||||||||||||||||||||||||||||
| 1478 | |||||||||||||||||||||||||||||||||||||||||||
| 1479 | use Getopt::Euclid; | ||||||||||||||||||||||||||||||||||||||||||
| 1480 | |||||||||||||||||||||||||||||||||||||||||||
| 1481 | and your module will then act just like Getopt::Euclid (i.e. you can use | ||||||||||||||||||||||||||||||||||||||||||
| 1482 | your module I<instead> of Getopt::Euclid>, except that your module's POD | ||||||||||||||||||||||||||||||||||||||||||
| 1483 | will also be prepended to the POD of any module that loads yours. In | ||||||||||||||||||||||||||||||||||||||||||
| 1484 | other words, you can use Getopt::Euclid in a module to create a standard | ||||||||||||||||||||||||||||||||||||||||||
| 1485 | set of CLI arguments, which can then be added to any application simply | ||||||||||||||||||||||||||||||||||||||||||
| 1486 | by loading your module. | ||||||||||||||||||||||||||||||||||||||||||
| 1487 | |||||||||||||||||||||||||||||||||||||||||||
| 1488 | To accomplish this trick Getopt::Euclid installs an C<import()> | ||||||||||||||||||||||||||||||||||||||||||
| 1489 | subroutine in your module. If your module already has an C<import()> | ||||||||||||||||||||||||||||||||||||||||||
| 1490 | subroutine defined, terrible things happen. So do not do that. | ||||||||||||||||||||||||||||||||||||||||||
| 1491 | |||||||||||||||||||||||||||||||||||||||||||
| 1492 | You may also short-circuit the import method within your calling program to | ||||||||||||||||||||||||||||||||||||||||||
| 1493 | have the POD from several modules included for argument parsing. | ||||||||||||||||||||||||||||||||||||||||||
| 1494 | |||||||||||||||||||||||||||||||||||||||||||
| 1495 | use Module1::Getopt (); # No argument parsing | ||||||||||||||||||||||||||||||||||||||||||
| 1496 | use Module2::Getopt (); # No argument parsing | ||||||||||||||||||||||||||||||||||||||||||
| 1497 | use Getopt::Euclid; # Arguments parsed | ||||||||||||||||||||||||||||||||||||||||||
| 1498 | |||||||||||||||||||||||||||||||||||||||||||
| 1499 | =item process_args() | ||||||||||||||||||||||||||||||||||||||||||
| 1500 | |||||||||||||||||||||||||||||||||||||||||||
| 1501 | Alternatively, to parse arguments from a source different from C<@ARGV>, use the | ||||||||||||||||||||||||||||||||||||||||||
| 1502 | C<process_args()> subroutine. | ||||||||||||||||||||||||||||||||||||||||||
| 1503 | |||||||||||||||||||||||||||||||||||||||||||
| 1504 | use Getopt::Euclid qw(:defer); | ||||||||||||||||||||||||||||||||||||||||||
| 1505 | my @args = ( '-in', 'file.txt', '-out', 'results.txt' ); | ||||||||||||||||||||||||||||||||||||||||||
| 1506 | Getopt::Euclid->process_args(\@args); | ||||||||||||||||||||||||||||||||||||||||||
| 1507 | |||||||||||||||||||||||||||||||||||||||||||
| 1508 | If you want to use the :minimal or :vars mode in this type of scenario, you can | ||||||||||||||||||||||||||||||||||||||||||
| 1509 | pass extra options to C<process_args()>: | ||||||||||||||||||||||||||||||||||||||||||
| 1510 | |||||||||||||||||||||||||||||||||||||||||||
| 1511 | use Getopt::Euclid qw(:defer); | ||||||||||||||||||||||||||||||||||||||||||
| 1512 | my @args = ( '-in', 'file.txt', '-out', 'results.txt' ); | ||||||||||||||||||||||||||||||||||||||||||
| 1513 | Getopt::Euclid->process_args(\@args, {-minimal => 1, -vars => 'prefix_'}); | ||||||||||||||||||||||||||||||||||||||||||
| 1514 | |||||||||||||||||||||||||||||||||||||||||||
| 1515 | This is particularly when you plan on processing POD manually. | ||||||||||||||||||||||||||||||||||||||||||
| 1516 | |||||||||||||||||||||||||||||||||||||||||||
| 1517 | =item process_pods() | ||||||||||||||||||||||||||||||||||||||||||
| 1518 | |||||||||||||||||||||||||||||||||||||||||||
| 1519 | Similarly, to parse argument specifications from a source different than the | ||||||||||||||||||||||||||||||||||||||||||
| 1520 | current script (and its dependencies), use the C<process_pods()> subroutine. | ||||||||||||||||||||||||||||||||||||||||||
| 1521 | |||||||||||||||||||||||||||||||||||||||||||
| 1522 | use Getopt::Euclid (); | ||||||||||||||||||||||||||||||||||||||||||
| 1523 | my @pods = ( 'script.pl', 'Module.pm' ); | ||||||||||||||||||||||||||||||||||||||||||
| 1524 | $Getopt::Euclid::MAN = Getopt::Euclid->process_pods(\@pods, {-strict => 1}); | ||||||||||||||||||||||||||||||||||||||||||
| 1525 | my @args = ( '-in', 'file.txt', '-out', 'results.txt' ); | ||||||||||||||||||||||||||||||||||||||||||
| 1526 | Getopt::Euclid->process_args(\@args); | ||||||||||||||||||||||||||||||||||||||||||
| 1527 | |||||||||||||||||||||||||||||||||||||||||||
| 1528 | By default, this method will look for .pod files associated with the given .pl | ||||||||||||||||||||||||||||||||||||||||||
| 1529 | and .pm files and use these .pod files preferentially when available. Set | ||||||||||||||||||||||||||||||||||||||||||
| 1530 | -strict to 1 to only use the given files. | ||||||||||||||||||||||||||||||||||||||||||
| 1531 | |||||||||||||||||||||||||||||||||||||||||||
| 1532 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 1533 | |||||||||||||||||||||||||||||||||||||||||||
| 1534 | =head2 POD interface | ||||||||||||||||||||||||||||||||||||||||||
| 1535 | |||||||||||||||||||||||||||||||||||||||||||
| 1536 | This is where all the action is. POD markup can be placed in a .pod file that | ||||||||||||||||||||||||||||||||||||||||||
| 1537 | has the same prefix as the corresponding Perl file. Alternatively, POD can be | ||||||||||||||||||||||||||||||||||||||||||
| 1538 | inserted anywhere in the Perl code, but is typically added either after an | ||||||||||||||||||||||||||||||||||||||||||
| 1539 | __END__ statement (like in the L<SYNOPSIS>), or interspersed in the code: | ||||||||||||||||||||||||||||||||||||||||||