blib/lib/IO/Prompter.pm

filter: Statementstmt 52.8% 468 / 886 Branchbran 43.5% 238 / 546 Conditioncond 35.7% 114 / 319 MC/DCmcdc 19.0% 36 / 189 Subroutinesub 55.3% 52 / 94 Podpod 100% 1 / 1 totaltotal 44.6% 909 / 2035 SCAR 101.2
CC
404
Cov
47%
__ANON__
7826.3
_decode_args
1199.5
prompt
202.3
CRAP 24937.2
↑ Summary
1 37 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package IO::Prompter;
3 37 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 37 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 37 ▶no if $] >= 5.018000, warnings => 'experimental';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 37 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 37 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 37 ▶use Contextual::Return qw< PUREBOOL BOOL SCALAR METHOD VOID LIST RETOBJ >;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 37 ▶use Scalar::Util qw< openhandle looks_like_number >;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 37 ▶use Symbol qw< qualify_to_ref >;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 37 ▶use match::smart qw< match >;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 37 our $VERSION = '0.005000';
15  
16 37 my $fake_input; # Flag that we're faking input from the source
17  
18 37 my $DEFAULT_TERM_WIDTH = 80;
19 37 my $DEFAULT_VERBATIM_KEY = "\cV";
20  
21 # Completion control...
22 37 my $COMPLETE_DISPLAY_FIELDS = 4; #...per line
23 37 my $COMPLETE_DISPLAY_GAP = 3; #...spaces
24  
25 37 ▶my $COMPLETE_KEY = $ENV{IO_PROMPTER_COMPLETE_KEY} // qq{\t};
Condition50%1/2
$ENV{"IO_PROMPTER_COMPLETE_KEY"} // "\t"
l!l
037
MC/DC0%0/1
$ENV{"IO_PROMPTER_COMPLETE_KEY"} // "\t"
$ENV{"IO_PROMPTER_COMPLETE_KEY"}
Truth table50%1/2
A
A= $ENV{"IO_PROMPTER_COMPLETE_KEY"}
Aresult
11
00
26 37 ▶my $COMPLETE_HIST = $ENV{IO_PROMPTER_HISTORY_KEY} // qq{\cR};
Condition50%1/2
$ENV{"IO_PROMPTER_HISTORY_KEY"} // "\22"
l!l
037
MC/DC0%0/1
$ENV{"IO_PROMPTER_HISTORY_KEY"} // "\22"
$ENV{"IO_PROMPTER_HISTORY_KEY"}
Truth table50%1/2
A
A= $ENV{"IO_PROMPTER_HISTORY_KEY"}
Aresult
11
00
27 37 my $COMPLETE_NEXT = qq{\cN};
28 37 my $COMPLETE_PREV = qq{\cP};
29  
30 37 my $COMPLETE_INIT = qr{ [\Q$COMPLETE_KEY$COMPLETE_HIST\E] }xms;
31 37 my $COMPLETE_CYCLE = qr{ [$COMPLETE_NEXT$COMPLETE_PREV] }xms;
32  
33 my %COMPLETE_MODE = (
34 $COMPLETE_KEY
35 => [split /\s+/, $ENV{IO_PROMPTER_COMPLETE_MODES}//q{list+longest full}],
36 $COMPLETE_HIST
37 37 ▶ => [split /\s+/, $ENV{IO_PROMPTER_HISTORY_MODES} // q{full}],
Condition50%2/4
$ENV{"IO_PROMPTER_COMPLETE_MODES"} // "list+longest full"
l!l
037
$ENV{"IO_PROMPTER_HISTORY_MODES"} // "full"
l!l
037
MC/DC0%0/1
$ENV{"IO_PROMPTER_COMPLETE_MODES"} // "list+longest full"
$ENV{"IO_PROMPTER_COMPLETE_MODES"}
MC/DC0%0/1
$ENV{"IO_PROMPTER_HISTORY_MODES"} // "full"
$ENV{"IO_PROMPTER_HISTORY_MODES"}
Truth table50%1/2
A
A= $ENV{"IO_PROMPTER_COMPLETE_MODES"}
Aresult
11
00
Truth table50%1/2
A
A= $ENV{"IO_PROMPTER_HISTORY_MODES"}
Aresult
11
00
38 );
39  
40 37 my $FAKE_ESC = "\e";
41 37 my $FAKE_INSERT = "\cF";
42 37 my $MENU_ESC = "\e";
43 37 my $MENU_MK = '__M_E_N_U__';
44  
45 37 my %EDIT = (
46 BACK => qq{\cB},
47 FORWARD => qq{\cF},
48 START => qq{\cA},
49 END => qq{\cE},
50 );
51 37 my $EDIT_KEY = '['.join(q{},values %EDIT).']';
52  
53 # Extracting key letters...
54 37 my $KL_EXTRACT = qr{ (?| \[ ( [[:alnum:]]++ ) \]
55 | \( ( [[:alnum:]]++ ) \)
56 | \< ( [[:alnum:]]++ ) \>
57 | \{ ( [[:alnum:]]++ ) \}
58 )
59 }xms;
60 37 my $KL_DEF_EXTRACT = qr{ \[ ( [[:alnum:]]++ ) \] }xms;
61  
62  
63 # Auxiliary prompts for -Yes => N construct...
64 37 my @YESNO_PROMPTS = (
65 q{Really?},
66 q{You're quite certain?},
67 q{Definitely?},
68 q{You mean it?},
69 q{You truly mean it?},
70 q{You're sure?},
71 q{Have you thought this through?},
72 q{You understand the consequences?},
73 );
74  
75  
76 # Remember returned values for history completion...
77 37 my %history_cache;
78  
79 # Track lexically-scoped default options and wrapper subs...
80 37 my @lexical_options = [];
81 37 my @lexical_wrappers = [];
82  
83 # Export the prompt() sub...
84 sub import {
85 42 ▶ my (undef, $config_data, @other_args) = @_;
Subroutine import: called CC=7 SCAR=19.5
86  
87 # Handle -argv requests...
88 42 ▶ if (defined $config_data && $config_data eq '-argv') {
Branch: 4 branches
BranchTrueFalseTotal
if (defined $config_data and $config_data eq '-argv') { } 0 42 42
elsif (ref $config_data eq 'ARRAY') { } 3 39 42
elsif (ref $config_data eq 'HASH') { } 2 37 39
elsif (defined $config_data) { } 2 35 37
Condition66%2/3
defined $config_data and $config_data eq "-argv"
!ll&&!rl&&r
3570
MC/DC0%0/2
defined $config_data and $config_data eq "-argv"
defined $config_data $config_data eq "-argv"
Truth table66%2/3
A and B
A= defined $config_dataB= $config_data eq "-argv"
ABresult
0X0
100
111
89 0 scalar prompt(-argv, @other_args);
90 }
91  
92 # Handle lexical options...
93 elsif (ref $config_data eq 'ARRAY') {
94 3 push @lexical_options, $config_data;
95 3 $^H{'IO::Prompter::scope_number'} = $#lexical_options;
96 }
97  
98 # Handle lexical wrappers...
99 elsif (ref $config_data eq 'HASH') {
100 2 push @lexical_options, [];
101 2 $lexical_wrappers[ $#lexical_options ] = $config_data;
102 2 $^H{'IO::Prompter::scope_number'} = $#lexical_options;
103 2 for my $subname (keys %{$config_data}) {
104 2 my @args = @{$config_data->{$subname}};
105 37 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
106 37 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
107 2 *{caller().'::'.$subname} = sub {
108 3 ▶ my $scope_number = (caller 0)[10]{'IO::Prompter::scope_number'};
Subroutine __ANON__: called CC=2 SCAR=7.1
109 3 ▶ return prompt(@{$lexical_wrappers[$scope_number]{$subname}//[]}, @_);
Condition50%1/2
$lexical_wrappers[$scope_number]{$subname} // []
l!l
30
MC/DC0%0/1
$lexical_wrappers[$scope_number]{$subname} // []
$lexical_wrappers[$scope_number]{$subname}
Truth table50%1/2
A
A= $lexical_wrappers[$scope_number]{$subname}
Aresult
11
00
110 2 };
111 }
112 }
113  
114 # Handler faked input specifications...
115 elsif (defined $config_data) {
116 2 $fake_input = $config_data;
117 }
118  
119 37 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
120 42 *{caller().'::prompt'} = \&prompt;
121 }
122  
123 # Prompt for, read, vet, and return input...
124 sub prompt {
125 # Reclaim full control of print statements while prompting...
126 99 ▶ local $\ = '';
Subroutine prompt: called CC=55 SCAR=53.1
Pod documented
127  
128 # Locate any lexical default options...
129 99 ▶ my $hints_hash = (caller 0)[10] // {};
Condition100%2/2
(&CORE::GLOBAL::caller(0))[10] // {}
l!l
495
MC/DC100%1/1
(&CORE::GLOBAL::caller(0))[10] // {}
(&CORE::GLOBAL::caller(0))[10]
Truth table100%2/2
A
A= (&CORE::GLOBAL::caller(0))[10]
Aresult
11
00
130 99 ▶ my $scope_num = $hints_hash->{'IO::Prompter::scope_number'} // 0;
Condition100%2/2
$hints_hash->{"IO::Prompter::scope_number"} // 0
l!l
495
MC/DC100%1/1
$hints_hash->{"IO::Prompter::scope_number"} // 0
$hints_hash->{"IO::Prompter::scope_number"}
Truth table100%2/2
A
A= $hints_hash->{"IO::Prompter::scope_number"}
Aresult
11
00
131  
132 # Extract and sanitize configuration arguments...
133 99 my $opt_ref = _decode_args(@{$lexical_options[$scope_num]}, @_);
134  
135 _warn( void => 'Useless use of prompt() in void context' )
136 97 ▶ if VOID && !$opt_ref->{-void};
Branch: 1 branch
BranchTrueFalseTotal
if &VOID() and not $opt_ref->{"-void"} 2 95 97
Condition100%3/3
&VOID() and not $opt_ref->{"-void"}
!ll&&!rl&&r
9412
MC/DC100%2/2
&VOID() and not $opt_ref->{"-void"}
&VOID() not $opt_ref->{"-void"}
Truth table100%3/3
A and not(B)
A= &VOID()B= not $opt_ref->{"-void"}
ABresult
0X0
100
111
137  
138 # Set up yesno prompts if required...
139 my @yesno_prompts
140 97 ▶ = ($opt_ref->{-yesno}{count}//0) > 1 ? @YESNO_PROMPTS : ();
Branch: 1 branch
BranchTrueFalseTotal
($opt_ref->{'-yesno'}{'count'} // 0) > 1 ? : 0 97 97
Condition100%2/2
$opt_ref->{"-yesno"}{"count"} // 0
l!l
2770
MC/DC100%1/1
$opt_ref->{"-yesno"}{"count"} // 0
$opt_ref->{"-yesno"}{"count"}
Truth table100%2/2
A
A= $opt_ref->{"-yesno"}{"count"}
Aresult
11
00
141  
142 # Work out where the prompts go, and where the input comes from...
143 97 ▶ my $in_filehandle = $opt_ref->{-in} // _open_ARGV();
Condition66%2/3
$opt_ref->{"-in"} // _open_ARGV()
l!l&&r!l&&!r
17800
MC/DC0%0/2
$opt_ref->{"-in"} // _open_ARGV()
$opt_ref->{"-in"} _open_ARGV()
Truth table66%2/3
A // B
A= $opt_ref->{"-in"}B= _open_ARGV()
ABresult
1X1
011
000
144 97 ▶ my $out_filehandle = $opt_ref->{-out} // qualify_to_ref(select);
Condition66%2/3
$opt_ref->{"-out"} // &qualify_to_ref(select)
l!l&&r!l&&!r
6910
MC/DC0%0/2
$opt_ref->{"-out"} // &qualify_to_ref(select)
$opt_ref->{"-out"} &qualify_to_ref(select)
Truth table66%2/3
A // B
A= $opt_ref->{"-out"}B= &qualify_to_ref(select)
ABresult
1X1
011
000
145 97 ▶ if (!openhandle $in_filehandle) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&openhandle($in_filehandle)) 0 97 97
146 0 ▶ open my $fh, '<', $in_filehandle
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $in_filehandle or _opt_err("Unacceptable", "-in", "valid filehandle or filename") 0 0 0
147 or _opt_err('Unacceptable', '-in', 'valid filehandle or filename');
148 0 $in_filehandle = $fh;
149 }
150 97 ▶ if (!openhandle $out_filehandle) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&openhandle($out_filehandle)) 0 97 97
151 0 ▶ open my $fh, '>', $out_filehandle
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $out_filehandle or _opt_err("Unacceptable", "-out", "valid filehandle or filename") 0 0 0
152 or _opt_err('Unacceptable', '-out', 'valid filehandle or filename');
153 0 $out_filehandle = $fh;
154 }
155  
156 # Track timeouts...
157 37 ▶ my $in_pos = do { no warnings; tell $in_filehandle } // 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
Condition50%1/2
do { no warnings; tell $in_filehandle } // 0
l!l
970
MC/DC0%0/1
do { no warnings; tell $in_filehandle } // 0
do { no warnings; tell $in_filehandle }
Truth table50%1/2
A
A= do { no warnings; tell $in_filehandle }
Aresult
11
00
158  
159 # Short-circuit if not valid handles...
160 97 ▶ return if !openhandle($in_filehandle) || !openhandle($out_filehandle);
Branch: 1 branch
BranchTrueFalseTotal
unless &openhandle($in_filehandle) and &openhandle($out_filehandle) 0 97 97
Condition33%1/3
&openhandle($in_filehandle) and &openhandle($out_filehandle)
!ll&&!rl&&r
0097
MC/DC0%0/2
&openhandle($in_filehandle) and &openhandle($out_filehandle)
&openhandle($in_filehandle) &openhandle($out_filehandle)
Truth table33%1/3
A and B
A= &openhandle($in_filehandle)B= &openhandle($out_filehandle)
ABresult
0X0
100
111
161  
162 # Work out how they're arriving and departing...
163 97 ▶ my $outputter_ref = -t $in_filehandle && -t $out_filehandle
Branch: 1 branch
BranchTrueFalseTotal
-t $in_filehandle && -t $out_filehandle ? : 0 97 97
Condition33%1/3
-t $in_filehandle and -t $out_filehandle
!ll&&!rl&&r
9700
MC/DC0%0/2
-t $in_filehandle and -t $out_filehandle
-t $in_filehandle -t $out_filehandle
Truth table33%1/3
A and B
A= -t $in_filehandleB= -t $out_filehandle
ABresult
0X0
100
111
164 ? _std_printer_to($out_filehandle, $opt_ref)
165 : _null_printer()
166 ;
167 97 my $inputter_ref = _generate_unbuffered_reader_from(
168 $in_filehandle, $outputter_ref, $opt_ref
169 );
170  
171 # Clear the screen if requested to...
172 97 ▶ if ($opt_ref->{-wipe}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_ref->{"-wipe"}) 0 97 97
173 0 $outputter_ref->(-nostyle => "\n" x 1000);
174 }
175  
176 # Handle menu structures...
177 97 my $input;
178 eval {
179 REPROMPT_YESNO:
180 97 ▶ if ($opt_ref->{-menu}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_ref->{'-menu'}) { } 1 96 97
181 # Remember top of (possibly nested) menu...
182 1 my @menu = ( $opt_ref->{-menu} );
183 1 my $top_prompt = $opt_ref->{-prompt};
184 1 $top_prompt =~ s{$MENU_MK}{$opt_ref->{-menu}{prompt}}xms;
185 1 $menu[-1]{prompt} = $top_prompt;
186  
187 MENU:
188 1 while (1) {
189 # Track the current level...
190 1 $opt_ref->{-menu_curr_level} = $menu[-1]{value_for};
191  
192 # Show menu and retreive choice...
193 1 $outputter_ref->(-style => $menu[-1]{prompt});
194 1 my $tag = $inputter_ref->($menu[-1]{constraint});
195  
196 # Handle a failure by exiting the loop...
197 1 ▶ last MENU if !defined $tag;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tag 0 1 1
198 1 $tag =~ s{\A\s*(\S*).*}{$1}xms;
199  
200 # Handle <ESC> by moving up menu stack...
201 1 ▶ if ($tag eq $MENU_ESC) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq $MENU_ESC) 0 1 1
202 0 $input = undef;
203 0 ▶ last MENU if @menu <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if @menu <= 1 0 0 0
204 0 pop @menu;
205 0 next MENU;
206 }
207  
208 # Handle defaults by selecting and ejecting...
209 1 ▶ if ($tag =~ /\A\R?\Z/ && exists $opt_ref->{-def}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag =~ /\A\R?\Z/ and exists $opt_ref->{"-def"}) 1 0 1
Condition33%1/3
$tag =~ /\A\R?\Z/ and exists $opt_ref->{"-def"}
!ll&&!rl&&r
001
MC/DC0%0/2
$tag =~ /\A\R?\Z/ and exists $opt_ref->{"-def"}
$tag =~ /\A\R?\Z/ exists $opt_ref->{"-def"}
Truth table33%1/3
A and B
A= $tag =~ /\A\R?\Z/B= exists $opt_ref->{"-def"}
ABresult
0X0
100
111
210 1 $input = $tag;
211 1 last MENU;
212 }
213  
214 # Otherwise, retrieve value for selected tag and exit if not a nested menu...
215 0 $input = $menu[-1]{value_for}{$tag};
216 0 ▶ last MENU if !ref $input;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $input 0 0 0
217  
218 # Otherwise, go down the menu one level...
219 push @menu,
220 _build_menu($input,
221 "Select from $menu[-1]{key_for}{$tag}: ",
222 $opt_ref->{-number} || $opt_ref->{-integer}
223 0 ▶ );
Condition0%0/3
$opt_ref->{'-number'} || $opt_ref->{'-integer'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt_ref->{'-number'} || $opt_ref->{'-integer'}
$opt_ref->{'-number'} $opt_ref->{'-integer'}
Truth table0%0/3
A || B
A= $opt_ref->{'-number'}B= $opt_ref->{'-integer'}
ABresult
1X1
011
000
224 0 $menu[-1]{prompt} .= '> ';
225 }
226 }
227  
228 # Otherwise, simply ask and ye shall receive...
229 else {
230 96 $outputter_ref->(-style => $opt_ref->{-prompt});
231 96 $input = $inputter_ref->();
232 }
233 97 1;
234 }
235 97 ▶ // do {
Condition33%1/3
eval { do { REPROMPT_YESNO: if ($opt_ref->{"-menu"}) { my(@menu) = $opt_ref->{"-menu"}; my $top_prompt = $opt_ref->{"-prompt"}; $top_prompt =~ s/$MENU_MK/$opt_ref->{"-menu"}{"prompt"}/msx; $menu[-1]{"prompt"} = $top_prompt; MENU: while (1) { $opt_ref->{"-menu_curr_level"} = $menu[-1]{"value_for"}; &$outputter_ref("-style", $menu[-1]{"prompt"}); my $tag = &$inputter_ref($menu[-1]{"constraint"}); last MENU unless defined $tag; $tag =~ s/\A\s*(\S*).*/$1/msx; if ($tag eq $MENU_ESC) { $input = undef; last MENU if @menu <= 1; pop @menu; next MENU; } ; if ($tag =~ /\A\R?\Z/ and exists $opt_ref->{"-def"}) { $input = $tag; last MENU; } ; $input = $menu[-1]{"value_for"}{$tag}; last MENU unless ref $input; push @menu, _build_menu($input, qq[Select from $menu[-1]{"key_for"}{$tag}: ], $opt_ref->{"-number"} || $opt_ref->{"-integer"}); $menu[-1]{"prompt"} .= '> '; } ; } else { &$outputter_ref("-style", $opt_ref->{"-prompt"}); $input = &$inputter_ref(); } ; 1 } } // do { &$outputter_ref("-echostyle", $opt_ref->{"-return"}->("")) if exists $opt_ref->{"-return"}; my $error = $@; die $@ unless ref $error eq "IO::Prompter::Cancellation"; return if $opt_ref->{"-verbatim"}; return &PUREBOOL(sub { 0; } , scalar &BOOL(sub { 0; } , scalar &SCALAR(sub { ${$error;}; } , scalar &METHOD(sub { "defaulted", sub { 0; } , "timedout", sub { 0; } ; } )))) }
l!l&&r!l&&!r
9700
MC/DC0%0/2
eval { do { REPROMPT_YESNO: if ($opt_ref->{"-menu"}) { my(@menu) = $opt_ref->{"-menu"}; my $top_prompt = $opt_ref->{"-prompt"}; $top_prompt =~ s/$MENU_MK/$opt_ref->{"-menu"}{"prompt"}/msx; $menu[-1]{"prompt"} = $top_prompt; MENU: while (1) { $opt_ref->{"-menu_curr_level"} = $menu[-1]{"value_for"}; &$outputter_ref("-style", $menu[-1]{"prompt"}); my $tag = &$inputter_ref($menu[-1]{"constraint"}); last MENU unless defined $tag; $tag =~ s/\A\s*(\S*).*/$1/msx; if ($tag eq $MENU_ESC) { $input = undef; last MENU if @menu <= 1; pop @menu; next MENU; } ; if ($tag =~ /\A\R?\Z/ and exists $opt_ref->{"-def"}) { $input = $tag; last MENU; } ; $input = $menu[-1]{"value_for"}{$tag}; last MENU unless ref $input; push @menu, _build_menu($input, qq[Select from $menu[-1]{"key_for"}{$tag}: ], $opt_ref->{"-number"} || $opt_ref->{"-integer"}); $menu[-1]{"prompt"} .= '> '; } ; } else { &$outputter_ref("-style", $opt_ref->{"-prompt"}); $input = &$inputter_ref(); } ; 1 } } // do { &$outputter_ref("-echostyle", $opt_ref->{"-return"}->("")) if exists $opt_ref->{"-return"}; my $error = $@; die $@ unless ref $error eq "IO::Prompter::Cancellation"; return if $opt_ref->{"-verbatim"}; return &PUREBOOL(sub { 0; } , scalar &BOOL(sub { 0; } , scalar &SCALAR(sub { ${$error;}; } , scalar &METHOD(sub { "defaulted", sub { 0; } , "timedout", sub { 0; } ; } )))) }
eval { do { REPROMPT_YESNO: if ($opt_ref->{"-menu"}) { my(@menu) = $opt_ref->{"-menu"}; my $top_prompt = $opt_ref->{"-prompt"}; $top_prompt =~ s/$MENU_MK/$opt_ref->{"-menu"}{"prompt"}/msx; $menu[-1]{"prompt"} = $top_prompt; MENU: while (1) { $opt_ref->{"-menu_curr_level"} = $menu[-1]{"value_for"}; &$outputter_ref("-style", $menu[-1]{"prompt"}); my $tag = &$inputter_ref($menu[-1]{"constraint"}); last MENU unless defined $tag; $tag =~ s/\A\s*(\S*).*/$1/msx; if ($tag eq $MENU_ESC) { $input = undef; last MENU if @menu <= 1; pop @menu; next MENU; } ; if ($tag =~ /\A\R?\Z/ and exists $opt_ref->{"-def"}) { $input = $tag; last MENU; } ; $input = $menu[-1]{"value_for"}{$tag}; last MENU unless ref $input; push @menu, _build_menu($input, qq[Select from $menu[-1]{"key_for"}{$tag}: ], $opt_ref->{"-number"} || $opt_ref->{"-integer"}); $menu[-1]{"prompt"} .= '> '; } ; } else { &$outputter_ref("-style", $opt_ref->{"-prompt"}); $input = &$inputter_ref(); } ; 1 } } do { &$outputter_ref("-echostyle", $opt_ref->{"-return"}->("")) if exists $opt_ref->{"-return"}; my $error = $@; die $@ unless ref $error eq "IO::Prompter::Cancellation"; return if $opt_ref->{"-verbatim"}; return &PUREBOOL(sub { 0; } , scalar &BOOL(sub { 0; } , scalar &SCALAR(sub { ${$error;}; } , scalar &METHOD(sub { "defaulted", sub { 0; } , "timedout", sub { 0; } ; } )))) }
Truth table33%1/3
A // B
A= eval { do { REPROMPT_YESNO: if ($opt_ref->{"-menu"}) { my(@menu) = $opt_ref->{"-menu"}; my $top_prompt = $opt_ref->{"-prompt"}; $top_prompt =~ s/$MENU_MK/$opt_ref->{"-menu"}{"prompt"}/msx; $menu[-1]{"prompt"} = $top_prompt; MENU: while (1) { $opt_ref->{"-menu_curr_level"} = $menu[-1]{"value_for"}; &$outputter_ref("-style", $menu[-1]{"prompt"}); my $tag = &$inputter_ref($menu[-1]{"constraint"}); last MENU unless defined $tag; $tag =~ s/\A\s*(\S*).*/$1/msx; if ($tag eq $MENU_ESC) { $input = undef; last MENU if @menu <= 1; pop @menu; next MENU; } ; if ($tag =~ /\A\R?\Z/ and exists $opt_ref->{"-def"}) { $input = $tag; last MENU; } ; $input = $menu[-1]{"value_for"}{$tag}; last MENU unless ref $input; push @menu, _build_menu($input, qq[Select from $menu[-1]{"key_for"}{$tag}: ], $opt_ref->{"-number"} || $opt_ref->{"-integer"}); $menu[-1]{"prompt"} .= '> '; } ; } else { &$outputter_ref("-style", $opt_ref->{"-prompt"}); $input = &$inputter_ref(); } ; 1 } }B= do { &$outputter_ref("-echostyle", $opt_ref->{"-return"}->("")) if exists $opt_ref->{"-return"}; my $error = $@; die $@ unless ref $error eq "IO::Prompter::Cancellation"; return if $opt_ref->{"-verbatim"}; return &PUREBOOL(sub { 0; } , scalar &BOOL(sub { 0; } , scalar &SCALAR(sub { ${$error;}; } , scalar &METHOD(sub { "defaulted", sub { 0; } , "timedout", sub { 0; } ; } )))) }
ABresult
1X1
011
000
236 # Supply the missing newline if requested...
237 $outputter_ref->(-echostyle => $opt_ref->{-return}(q{}))
238 0 ▶ if exists $opt_ref->{-return};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opt_ref->{"-return"} 0 0 0
239  
240 # Rethrow any other exception...
241 0 my $error = $@;
242 0 ▶ die $@ unless ref($error) eq 'IO::Prompter::Cancellation';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $error eq "IO::Prompter::Cancellation" 0 0 0
243  
244 # Return failure on cancellation...
245 0 ▶ return if $opt_ref->{-verbatim};
Branch: 1 branch
BranchTrueFalseTotal
if $opt_ref->{"-verbatim"} 0 0 0
246 0 ▶ return PUREBOOL { 0 }
Subroutine __ANON__: not called CC=1 SCAR=6.9
247 0 ▶ BOOL { 0 }
Subroutine __ANON__: not called CC=1 SCAR=6.9
248 0 ▶ SCALAR { ${$error} }
Subroutine __ANON__: not called CC=1 SCAR=6.9
249 0 ▶ METHOD { defaulted => sub { 0 }, timedout => sub { 0 } };
Subroutine __ANON__: not called CC=1 SCAR=6.9
250 };
251  
252 # Provide default value if available and necessary...
253 97 my $defaulted = 0;
254 97 ▶ if (defined $input && $input =~ /\A\R?\Z/ && exists $opt_ref->{-def}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $input and $input =~ /\A\R?\Z/ and exists $opt_ref->{"-def"}) 5 92 97
Condition83%5/6
defined $input and $input =~ /\A\R?\Z/ and exists $opt_ref->{"-def"}
!ll&&!rl&&r
9205
defined $input and $input =~ /\A\R?\Z/
!ll&&!rl&&r
10825
MC/DC66%2/3
defined $input and $input =~ /\A\R?\Z/ and exists $opt_ref->{"-def"}
defined $input $input =~ /\A\R?\Z/ exists $opt_ref->{"-def"}
Truth table75%3/4
A and B and C
A= defined $inputB= $input =~ /\A\R?\Z/C= exists $opt_ref->{"-def"}
ABCresult
0XX0
10X0
1100
1111
255 5 $input = $opt_ref->{-def};
256 5 $defaulted = 1;
257 }
258  
259 # The input line is usually chomped before being returned...
260 97 ▶ if (defined $input && !$opt_ref->{-line}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $input and not $opt_ref->{"-line"}) 82 15 97
Condition100%3/3
defined $input and not $opt_ref->{"-line"}
!ll&&!rl&&r
10582
MC/DC100%2/2
defined $input and not $opt_ref->{"-line"}
defined $input not $opt_ref->{"-line"}
Truth table100%3/3
A and not(B)
A= defined $inputB= not $opt_ref->{"-line"}
ABresult
0X0
100
111
261 82 chomp $input;
262 }
263  
264 # Check for a value indicating failure...
265 97 ▶ if (exists $opt_ref->{-fail} && match($input, $opt_ref->{-fail})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt_ref->{"-fail"} and match($input, $opt_ref->{"-fail"})) 2 95 97
Condition100%3/3
exists $opt_ref->{"-fail"} and match($input, $opt_ref->{"-fail"})
!ll&&!rl&&r
9412
MC/DC100%2/2
exists $opt_ref->{"-fail"} and match($input, $opt_ref->{"-fail"})
exists $opt_ref->{"-fail"} match($input, $opt_ref->{"-fail"})
Truth table100%3/3
A and B
A= exists $opt_ref->{"-fail"}B= match($input, $opt_ref->{"-fail"})
ABresult
0X0
100
111
266 2 $input = undef;
267 }
268  
269 # Setting @ARGV is a special case; process it like a command-line...
270 97 ▶ if ($opt_ref->{-argv}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_ref->{"-argv"}) 1 96 97
271 5 @ARGV = map { _shell_expand($_) }
272 1 grep {defined}
273 $input =~ m{
274 ( ' [^'\\]* (?: \\. [^'\\]* )* ' )
275 | ( " [^"\\]* (?: \\. [^"\\]* )* " )
276 | (?: ^ | \s) ( [^\s"'] \S* )
277 }gxms;
278 1 return 1;
279 }
280  
281 # "Those who remember history are enabled to repeat it"...
282 96 ▶ if (defined $input and $opt_ref->{-history} ne 'NONE') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $input and $opt_ref->{"-history"} ne "NONE") 84 12 96
Condition66%2/3
defined $input and $opt_ref->{"-history"} ne "NONE"
!ll&&!rl&&r
12084
MC/DC50%1/2
defined $input and $opt_ref->{"-history"} ne "NONE"
defined $input $opt_ref->{"-history"} ne "NONE"
Truth table66%2/3
A and B
A= defined $inputB= $opt_ref->{"-history"} ne "NONE"
ABresult
0X0
100
111
283 84 ▶ my $history_set = $history_cache{ $opt_ref->{-history} } //= [] ;
Condition100%2/2
$history_cache{$opt_ref->{"-history"}} //= []
l!l
6123
MC/DC100%1/1
$history_cache{$opt_ref->{"-history"}} //= []
$history_cache{$opt_ref->{"-history"}}
Truth table100%2/2
A
A= $history_cache{$opt_ref->{"-history"}}
Aresult
11
00
284 84 @{ $history_set } = ($input, grep { $_ ne $input } @{ $history_set });
285 }
286  
287 # If input timed out insert the default, if any...
288 37 ▶ my $timedout = $in_pos == do{ no warnings; tell $in_filehandle } // 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
Condition50%1/2
$in_pos == do { no warnings; tell $in_filehandle } // 0
l!l
960
MC/DC0%0/1
$in_pos == do { no warnings; tell $in_filehandle } // 0
$in_pos == do { no warnings; tell $in_filehandle }
Truth table50%1/2
A
A= $in_pos == do { no warnings; tell $in_filehandle }
Aresult
11
00
289 96 ▶ if ($timedout && exists $opt_ref->{-def}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timedout and exists $opt_ref->{"-def"}) 0 96 96
Condition66%2/3
$timedout and exists $opt_ref->{"-def"}
!ll&&!rl&&r
9060
MC/DC0%0/2
$timedout and exists $opt_ref->{"-def"}
$timedout exists $opt_ref->{"-def"}
Truth table66%2/3
A and B
A= $timedoutB= exists $opt_ref->{"-def"}
ABresult
0X0
100
111
290 0 $input = $opt_ref->{-def};
291 0 $defaulted = 1;
292 }
293  
294 # A defined input is a successful input...
295 96 my $succeeded = defined $input;
296  
297 # The -yesno variants also need a 'y' to be successful...
298 96 ▶ if ($opt_ref->{-yesno}{count}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_ref->{"-yesno"}{"count"}) 27 69 96
299 27 ▶ $succeeded &&= $input =~ m{\A \s* y}ixms;
Condition66%2/3
$succeeded &&= $input =~ /\A \s* y/imsx
!ll&&!rl&&r
0720
MC/DC50%1/2
$succeeded &&= $input =~ /\A \s* y/imsx
$succeeded $input =~ /\A \s* y/imsx
Truth table66%2/3
A &&= B
A= $succeededB= $input =~ /\A \s* y/imsx
ABresult
0X0
100
111
300 27 ▶ if ($succeeded && $opt_ref->{-yesno}{count} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($succeeded and $opt_ref->{"-yesno"}{"count"} > 1) 0 27 27
Condition66%2/3
$succeeded and $opt_ref->{"-yesno"}{"count"} > 1
!ll&&!rl&&r
7200
MC/DC0%0/2
$succeeded and $opt_ref->{"-yesno"}{"count"} > 1
$succeeded $opt_ref->{"-yesno"}{"count"} > 1
Truth table66%2/3
A and B
A= $succeededB= $opt_ref->{"-yesno"}{"count"} > 1
ABresult
0X0
100
111
301 0 my $count = --$opt_ref->{-yesno}{count};
302 $opt_ref->{-prompt}
303 0 ▶ = @yesno_prompts ? shift(@yesno_prompts) . q{ }
Branch: 2 branches
BranchTrueFalseTotal
@yesno_prompts ? : 0 0 0
$count > 1 ? : 0 0 0
304 : $count > 1 ? qq{Please confirm $count more times }
305 : q{Please confirm one last time }
306 ;
307 0 goto REPROMPT_YESNO; # Gasp, yes goto is the cleanest way!
308 }
309 }
310  
311 # Verbatim return doesn't do fancy tricks...
312 96 ▶ if ($opt_ref->{-verbatim}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_ref->{"-verbatim"}) 6 90 96
313 6 ▶ return $input // ();
Condition66%2/3
$input // ()
l!l&&r!l&&!r
501
MC/DC50%1/2
$input // ()
$input ()
Truth table66%2/3
A // B
A= $inputB= ()
ABresult
1X1
011
000
314 }
315  
316 # Failure in a list context returns nothing...
317 90 ▶ return if LIST && !$succeeded;
Branch: 1 branch
BranchTrueFalseTotal
if &LIST() and not $succeeded 4 86 90
Condition100%3/3
&LIST() and not $succeeded
!ll&&!rl&&r
8424
MC/DC100%2/2
&LIST() and not $succeeded
&LIST() not $succeeded
Truth table100%3/3
A and not(B)
A= &LIST()B= not $succeeded
ABresult
0X0
100
111
318  
319 # Otherwise, be context sensitive...
320 return
321 43 ▶ PUREBOOL { $_ = RETOBJ; next handler; }
Subroutine __ANON__: called CC=1 SCAR=0.0
322 75 ▶ BOOL { $succeeded; }
Subroutine __ANON__: called CC=1 SCAR=0.0
323 48 ▶ SCALAR { $input; }
Subroutine __ANON__: called CC=1 SCAR=0.0
324 METHOD {
325 0 ▶ defaulted => sub { $defaulted },
Subroutine __ANON__: not called CC=1 SCAR=6.9
326 timedout => sub {
327 0 ▶ return q{} if !$timedout;
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
unless $timedout 0 0 0
328 return "timed out after $opt_ref->{-timeout} second"
329 0 ▶ . ($opt_ref->{-timeout} == 1 ? q{} : q{s});
Branch: 1 branch
BranchTrueFalseTotal
$opt_ref->{'-timeout'} == 1 ? : 0 0 0
330 },
331 86 ▶ };
Subroutine __ANON__: not called CC=1 SCAR=1.2
332 }
333  
334  
335 # Simulate a command line expansion for the -argv option...
336 sub _shell_expand {
337 5 ▶ my ($text) = @_;
Subroutine _shell_expand: called CC=3 SCAR=11.0
338  
339 # Single-quoted text is literal...
340 5 ▶ if ($text =~ m{\A ' (.*) ' \z}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\A ' (.*) ' \z/msx) 1 4 5
341 1 return $1;
342 }
343  
344 # Everything else has shell variables expanded...
345 4 my $ENV_PAT = join '|', reverse sort keys %ENV;
346 4 $text =~ s{\$ ($ENV_PAT)}{$ENV{$1}}gxms;
347  
348 # Double-quoted text isn't globbed...
349 4 ▶ if ($text =~ m{\A " (.*) " \z}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\A " (.*) " \z/msx) 2 2 4
350 2 return $1;
351 }
352  
353 # Everything else is...
354 2 return glob($text);
355 }
356  
357 # No completion is the default...
358 0 ▶my $DEFAULT_COMPLETER = sub { q{} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
359  
360 # Translate std constraints...
361 my %STD_CONSTRAINT = (
362 4 ▶ positive => sub { $_ > 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
363 0 ▶ negative => sub { $_ < 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
364 0 ▶ zero => sub { $_ == 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
365 0 ▶ even => sub { $_ % 2 == 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
366 2 ▶ odd => sub { $_ % 2 != 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
367 37 );
368  
369 # Create abbreviations...
370 37 $STD_CONSTRAINT{pos} = $STD_CONSTRAINT{positive};
371 37 $STD_CONSTRAINT{neg} = $STD_CONSTRAINT{negative};
372  
373 # Create antitheses...
374 37 for my $constraint (keys %STD_CONSTRAINT) {
375 259 my $implementation = $STD_CONSTRAINT{$constraint};
376 $STD_CONSTRAINT{"non$constraint"}
377 0 ▶ = sub { ! $implementation->(@_) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
378 }
379  
380 # Special style specifications require decoding...
381  
382 sub _decode_echo {
383 1 ▶ my $style = shift;
Subroutine _decode_echo: called CC=3 SCAR=17.3
384  
385 # Not a special style...
386 1 ▶ return $style if ref $style || $style !~ m{/};
Branch: 1 branch
BranchTrueFalseTotal
if ref $style or not $style =~ m[/] 1 0 1
Condition33%1/3
ref $style or not $style =~ m[/]
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $style or not $style =~ m[/]
ref $style not $style =~ m[/]
Truth table33%1/3
A or not(B)
A= ref $styleB= not $style =~ m[/]
ABresult
1X1
011
000
387  
388 # A slash means yes/no echoes...
389 0 my ($yes, $no) = split m{/}, $style;
390 0 ▶ return sub{ /y/i ? $yes : $no };
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
/y/i ? : 0 0 0
391 }
392  
393 sub _decode_echostyle {
394 0 ▶ my $style = shift;
Subroutine _decode_echostyle: not called CC=3 SCAR=24.8
395  
396 # Not a special style...
397 0 ▶ return $style if ref $style || $style !~ m{/};
Branch: 1 branch
BranchTrueFalseTotal
if ref $style or not $style =~ m[/] 0 0 0
Condition0%0/3
ref $style or not $style =~ m[/]
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $style or not $style =~ m[/]
ref $style not $style =~ m[/]
Truth table0%0/3
A or not(B)
A= ref $styleB= not $style =~ m[/]
ABresult
1X1
011
000
398  
399 # A slash means yes/no styles...
400 0 my ($yes, $no) = split m{/}, $style;
401 0 ▶ return sub{ /y/i ? $yes : $no };
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
/y/i ? : 0 0 0
402 }
403  
404 sub _decode_style {
405 # No special prompt styles (yet)...
406 0 ▶ return shift;
Subroutine _decode_style: not called CC=1 SCAR=6.9
407 }
408  
409 # Generate safe closure around active sub...
410 sub _gen_wrapper_for {
411 1 ▶ my ($arg) = @_;
Subroutine _gen_wrapper_for: called CC=2 SCAR=10.1
412 return ref $arg ne 'CODE'
413 0 ▶ ? sub { $arg }
Subroutine __ANON__: not called CC=1 SCAR=6.9
414 37 ▶ : sub { eval { for (shift) { no warnings; return $arg->($_) // $_ } } };
Subroutine BEGIN: called CC=1 SCAR=1.8
Subroutine __ANON__: not called CC=3 SCAR=15.7
Branch: 1 branch
BranchTrueFalseTotal
ref $arg ne 'CODE' ? : 1 0 1
Condition0%0/3
&$arg($_) // $_
l!l&&r!l&&!r
000
MC/DC0%0/2
&$arg($_) // $_
&$arg($_) $_
Truth table0%0/3
A // B
A= &$arg($_)B= $_
ABresult
1X1
011
000
415 }
416  
417 # Create recognizer...
418 37 my $STD_CONSTRAINT
419 = '^(?:' . join('|', reverse sort keys %STD_CONSTRAINT) . ')';
420  
421 # Translate name constraints to implementations...
422 sub _standardize_constraint {
423 3 ▶ my ($option_type, $constraint_spec) = @_;
Subroutine _standardize_constraint: called CC=3 SCAR=11.0
424  
425 3 ▶ return ("be an acceptable $option_type", $constraint_spec)
Branch: 1 branch
BranchTrueFalseTotal
if ref $constraint_spec 2 1 3
426 if ref $constraint_spec;
427  
428 1 my @constraint_names = split /\s+/, $constraint_spec;
429 my @constraints =
430 1 ▶ map { $STD_CONSTRAINT{$_}
Condition33%1/3
$STD_CONSTRAINT{$_} // _opt_err("invalid", -$option_type, "\"pos\", \"neg\", \"even\", etc.", qq["$_"])
l!l&&r!l&&!r
200
MC/DC0%0/2
$STD_CONSTRAINT{$_} // _opt_err("invalid", -$option_type, "\"pos\", \"neg\", \"even\", etc.", qq["$_"])
$STD_CONSTRAINT{$_} _opt_err("invalid", -$option_type, "\"pos\", \"neg\", \"even\", etc.", qq["$_"])
Truth table33%1/3
A // B
A= $STD_CONSTRAINT{$_}B= _opt_err("invalid", -$option_type, "\"pos\", \"neg\", \"even\", etc.", qq["$_"])
ABresult
1X1
011
000
431 // _opt_err('invalid',-$option_type,'"pos", "neg", "even", etc.', qq{"$_"})
432 } @constraint_names;
433  
434 return (
435 'be ' . join(' and ', @constraint_names),
436 sub {
437 4 ▶ my ($compare_val) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
438 4 for my $constraint (@constraints) {
439 6 ▶ return 0 if !$constraint->($compare_val);
Branch: 1 branch
BranchTrueFalseTotal
unless &$constraint($compare_val) 3 3 6
440 }
441 1 return 1;
442 }
443 1 );
444 }
445  
446  
447 # Convert args to prompt + options hash...
448 sub _decode_args {
449 my %option = (
450 -prompt => undef,
451 -complete => $DEFAULT_COMPLETER,
452 -must => {},
453 -history => 'DEFAULT',
454 0 ▶ -style => sub{ q{} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
455 0 ▶ -nostyle => sub{ q{} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
456 0 ▶ -echostyle => sub{ q{} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
457 30 ▶ -echo => sub { my $char = shift; $char eq "\t" ? q{ } : $char },
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$char eq "\t" ? : 0 30 30
458 0 ▶ -return => sub { "\n" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
459 99 ▶ );
Subroutine _decode_args: called CC=134 SCAR=70.9
460  
461 DECODING:
462 99 ▶ while (defined(my $arg = shift @_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $arg = shift @_)) 208 97 305
463 220 ▶ if (my $type = ref $arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = ref $arg) { } 0 220 220
464 0 _warn( reserved =>
465 'prompt(): Unexpected argument (' . lc($type) . ' ref) ignored'
466 );
467 }
468 else {
469 220 state $already_wiped;
470 220 my $redo;
471 # The sound of one hand clapping...
472 220 ▶ if ($arg =~ /^-_/) {
Branch: 55 branches
BranchTrueFalseTotal
if ($arg =~ /^-_/) { } 6 214 220
elsif ($arg =~ /^-line$/) { } 1 213 214
elsif ($arg =~ /^-l/) { } 4 209 213
elsif ($arg =~ /^-YesNo$/) { } 4 205 209
elsif ($arg =~ /^-YN/) { } 1 204 205
elsif ($arg =~ /^-yesno$/) { } 4 200 204
elsif ($arg =~ /^-yn/) { } 4 196 200
elsif ($arg =~ /^-Yes$/) { } 7 189 196
elsif ($arg =~ /^-Y/) { } 1 188 189
elsif ($arg =~ /^-yes$/) { } 5 183 188
elsif ($arg =~ /^-y/) { } 1 182 183
elsif ($arg =~ /^-argv$/) { } 1 181 182
elsif ($arg =~ /^-a/) { } 0 181 181
elsif ($arg =~ /^-wipe(first)?$/) { } 0 181 181
elsif ($arg =~ /^-w/) { } 0 181 181
elsif ($arg =~ /^-fail$/) { } 3 178 181
elsif ($arg =~ /^-cancel/) { } 0 178 178
elsif ($arg =~ /^-f(?:ilenames?)?$/) { } 0 178 178
elsif ($arg =~ /^-style/) { } 0 178 178
elsif ($arg =~ /^-echostyle/) { } 0 178 178
elsif ($arg =~ /^-stdio$/) { } 0 178 178
elsif ($arg =~ /^-in$/) { } 17 161 178
elsif ($arg =~ /^-out$/) { } 6 155 161
elsif ($arg =~ /^-integer$/) { } 6 149 155
elsif ($arg =~ /^-num(?:ber)?$/) { } 2 147 149
elsif ($arg =~ /^-i/) { } 1 146 147
elsif ($arg =~ /^-n/) { } 1 145 146
elsif ($arg =~ /^-void$/) { } 1 144 145
elsif ($arg =~ /^-verb(?:atim)?$/) { } 3 141 144
elsif ($arg =~ /^-v/) { } 3 138 141
elsif ($arg =~ /^-sing(?:le)?$/) { } 1 137 138
elsif ($arg =~ /^-[s1]/) { } 5 132 137
elsif ($arg =~ /^-DEF(?:AULT)?/) { } 0 132 132
elsif ($arg =~ /^-def(?:ault)?/) { } 5 127 132
elsif ($arg =~ /^-d(.+)$/) { } 2 125 127
elsif ($arg =~ /^-t(\d+)/) { } 1 124 125
elsif ($arg =~ /^-timeout$/) { } 2 122 124
elsif ($arg =~ /^-g.*/) { } 8 114 122
elsif ($arg =~ /-keyletters_implement/) { } 4 110 114
elsif ($arg =~ /^-key(?:let(?:ter)?)(?:s)?/) { } 2 108 110
elsif ($arg =~ /^-k/) { } 2 106 108
elsif ($arg =~ /^-must$/) { } 6 100 106
elsif ($arg =~ /^-history/) { } 0 100 100
elsif ($arg =~ /^-h(.*)/) { } 0 100 100
elsif ($arg =~ /^-comp(?:lete)?/) { } 0 100 100
elsif ($arg =~ /^-(echo|ret(?:urn)?)$/) { } 0 100 100
elsif ($arg =~ /^-e(.*)/) { } 1 99 100
elsif ($arg =~ /^-r(.+)/) { } 0 99 99
elsif ($arg =~ /^-r/) { } 0 99 99
elsif ($arg =~ /^-prefill/) { } 0 99 99
elsif ($arg =~ /^-prompt$/) { } 0 99 99
elsif ($arg =~ /^-p(\S*)$/) { } 0 99 99
elsif ($arg =~ /^-menu$/) { } 1 98 99
elsif ($arg =~ /^-monitor/) { } 0 98 98
elsif ($arg =~ /^-\w+$/) { } 2 96 98
473 6 $redo = 1;
474 }
475  
476 # Non-chomping option...
477 elsif ($arg =~ /^-line$/) {
478 1 $option{-line}++;
479 }
480 elsif ($arg =~ /^-l/) {
481 4 $option{-line}++;
482 4 $redo = 1;
483 }
484  
485 # The -yesno variants...
486 elsif ($arg =~ /^-YesNo$/) {
487 4 ▶ my $count = @_ && looks_like_number($_[0]) ? shift @_ : 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ && &looks_like_number($_[0]) ? : 0 4 4
Condition33%1/3
@_ and &looks_like_number($_[0])
!ll&&!rl&&r
400
MC/DC0%0/2
@_ and &looks_like_number($_[0])
@_ &looks_like_number($_[0])
Truth table33%1/3
A and B
A= @_B= &looks_like_number($_[0])
ABresult
0X0
100
111
488 $option{-yesno} = {
489 4 must => { '[YN]' => qr{\A \s* [YN] }xms },
490 count => $count,
491 };
492 }
493 elsif ($arg =~ /^-YN/) {
494 $option{-yesno} = {
495 1 must => { '[YN]' => qr{\A \s* [YN] }xms },
496 count => 1,
497 };
498 1 $redo = 2;
499 }
500 elsif ($arg =~ /^-yesno$/) {
501 4 ▶ my $count = @_ && looks_like_number($_[0]) ? shift @_ : 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ && &looks_like_number($_[0]) ? : 0 4 4
Condition33%1/3
@_ and &looks_like_number($_[0])
!ll&&!rl&&r
400
MC/DC0%0/2
@_ and &looks_like_number($_[0])
@_ &looks_like_number($_[0])
Truth table33%1/3
A and B
A= @_B= &looks_like_number($_[0])
ABresult
0X0
100
111
502 $option{-yesno} = {
503 4 must => { '[yn]' => qr{\A \s* [YN] }ixms },
504 count => $count,
505 };
506 }
507 elsif ($arg =~ /^-yn/) {
508 $option{-yesno} = {
509 4 must => { '[yn]' => qr{\A \s* [YN] }ixms },
510 count => 1,
511 };
512 4 $redo = 2;
513 }
514 elsif ($arg =~ /^-Yes$/) {
515 7 ▶ my $count = @_ && looks_like_number($_[0]) ? shift @_ : 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ && &looks_like_number($_[0]) ? : 0 7 7
Condition33%1/3
@_ and &looks_like_number($_[0])
!ll&&!rl&&r
700
MC/DC0%0/2
@_ and &looks_like_number($_[0])
@_ &looks_like_number($_[0])
Truth table33%1/3
A and B
A= @_B= &looks_like_number($_[0])
ABresult
0X0
100
111
516 $option{-yesno} = {
517 7 must => { '[Y for yes]' => qr{\A \s* (?: [^y] | \Z) }xms },
518 count => $count,
519 };
520 }
521 elsif ($arg =~ /^-Y/) {
522 $option{-yesno} = {
523 1 must => { '[Y for yes]' => qr{\A \s* (?: [^y] | \Z) }xms },
524 count => 1,
525 };
526 1 $redo = 1;
527 }
528 elsif ($arg =~ /^-yes$/) {
529 5 ▶ my $count = @_ && looks_like_number($_[0]) ? shift @_ : 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ && &looks_like_number($_[0]) ? : 0 5 5
Condition33%1/3
@_ and &looks_like_number($_[0])
!ll&&!rl&&r
500
MC/DC0%0/2
@_ and &looks_like_number($_[0])
@_ &looks_like_number($_[0])
Truth table33%1/3
A and B
A= @_B= &looks_like_number($_[0])
ABresult
0X0
100
111
530 5 $option{-yesno} = { count => $count };
531 }
532 elsif ($arg =~ /^-y/) {
533 1 $option{-yesno} = { count => 1 };
534 1 $redo = 1;
535 }
536  
537 # Load @ARGV...
538 elsif ($arg =~ /^-argv$/) {
539 1 $option{-argv} = 1;
540 }
541  
542 elsif ($arg =~ /^-a/) {
543 0 $option{-argv} = 1;
544 0 $redo = 1;
545 }
546  
547 # Clear screen before prompt...
548 elsif ($arg =~ /^-wipe(first)?$/) {
549 0 ▶ $option{-wipe} = $1 ? !$already_wiped : 1;
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 0 0 0
550 0 $already_wiped = 1;
551 }
552 elsif ($arg =~ /^-w/) {
553 0 $option{-wipe} = 1;
554 0 $already_wiped = 1;
555 0 $redo = 1;
556 }
557  
558 # Specify a failure condition...
559 elsif ($arg =~ /^-fail$/) {
560 3 ▶ _opt_err('Missing', -fail, 'failure condition') if !@_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 3 3
561 3 $option{-fail} = shift @_;
562 }
563  
564 # Specify an immediate failure condition...
565 elsif ($arg =~ /^-cancel/) {
566 0 ▶ _opt_err('Missing', -cancel, 'cancellation condition') if !@_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
567 0 $option{-cancel} = shift @_;
568 }
569  
570 # Specify a file request...
571 elsif ($arg =~ /^-f(?:ilenames?)?$/) {
572 0 ▶ $option{-must}{'0: be an existing file'} = sub { -e $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
573 0 ▶ $option{-must}{'1: be readable'} = sub { -r $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
574 0 $option{-complete} = 'filenames';
575 }
576  
577 # Specify prompt echoing colour/style...
578 elsif ($arg =~ /^-style/) {
579 0 ▶ _opt_err('Missing -style specification') if !@_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
580 0 my $style = _decode_style(shift @_);
581 0 $option{-style} = _gen_wrapper_for($style);
582 }
583  
584 # Specify input colour/style...
585 elsif ($arg =~ /^-echostyle/) {
586 0 ▶ _opt_err('Missing -echostyle specification') if !@_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
587 0 my $style = _decode_echostyle(shift @_);
588 0 $option{-echostyle} = _gen_wrapper_for($style);
589 }
590  
591  
592 # Specify input and output filehandles...
593 0 elsif ($arg =~ /^-stdio$/) { $option{-in} = *STDIN;
594 0 $option{-out} = *STDOUT;
595 }
596 17 elsif ($arg =~ /^-in$/) { $option{-in} = shift @_; }
597 6 elsif ($arg =~ /^-out$/) { $option{-out} = shift @_; }
598  
599  
600 # Specify integer and number return value...
601 elsif ($arg =~ /^-integer$/) {
602 6 $option{-integer} = 1;
603 6 ▶ if (@_ && (ref $_[0] || $_[0] =~ $STD_CONSTRAINT)) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and ref $_[0] || $_[0] =~ /$STD_CONSTRAINT/) 3 3 6
Condition83%5/6
@_ and ref $_[0] || $_[0] =~ /$STD_CONSTRAINT/
!ll&&!rl&&r
051
ref $_[0] or $_[0] =~ /$STD_CONSTRAINT/
l!l&&r!l&&!r
213
MC/DC33%1/3
@_ and ref $_[0] || $_[0] =~ /$STD_CONSTRAINT/
@_ ref $_[0] $_[0] =~ /$STD_CONSTRAINT/
Truth table50%2/4
A and B or C
A= @_B= ref $_[0]C= $_[0] =~ /$STD_CONSTRAINT/
ABCresult
0XX0
1000
11X1
1011
604 3 my ($errmsg, $constraint)
605 = _standardize_constraint('integer',shift);
606 3 $option{-must}{$errmsg} = $constraint;
607 }
608 }
609 elsif ($arg =~ /^-num(?:ber)?$/) {
610 2 $option{-number} = 1;
611 2 ▶ if (@_ && (ref $_[0] || $_[0] =~ $STD_CONSTRAINT)) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and ref $_[0] || $_[0] =~ /$STD_CONSTRAINT/) 0 2 2
Condition33%2/6
@_ and ref $_[0] || $_[0] =~ /$STD_CONSTRAINT/
!ll&&!rl&&r
020
ref $_[0] or $_[0] =~ /$STD_CONSTRAINT/
l!l&&r!l&&!r
002
MC/DC0%0/3
@_ and ref $_[0] || $_[0] =~ /$STD_CONSTRAINT/
@_ ref $_[0] $_[0] =~ /$STD_CONSTRAINT/
Truth table25%1/4
A and B or C
A= @_B= ref $_[0]C= $_[0] =~ /$STD_CONSTRAINT/
ABCresult
0XX0
1000
11X1
1011
612 0 my ($errmsg, $constraint)
613 = _standardize_constraint('number',shift);
614 0 $option{-must}{$errmsg} = $constraint;
615 }
616 }
617 1 elsif ($arg =~ /^-i/) { $option{-integer} = 1; $redo = 1; }
618 1 elsif ($arg =~ /^-n/) { $option{-number} = 1; $redo = 1; }
619  
620 # Specify void context is okay...
621 1 elsif ($arg =~ /^-void$/) { $option{-void} = 1; }
622  
623 # Specify verbatim return value...
624 3 elsif ($arg =~ /^-verb(?:atim)?$/) { $option{-verbatim} = 1; }
625 3 elsif ($arg =~ /^-v/) { $option{-verbatim} = 1; $redo = 1;}
626  
627 # Specify single character return...
628 1 elsif ($arg =~ /^-sing(?:le)?$/) { $option{-single} = 1; }
629 5 elsif ($arg =~ /^-[s1]/) { $option{-single} = 1; $redo = 1; }
630  
631 # Specify a default...
632 elsif ($arg =~ /^-DEF(?:AULT)?/) {
633 0 ▶ _opt_err('Missing', '-DEFAULT', 'string') if !@_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
634 0 $option{-def} = shift @_;
635 0 $option{-def_nocheck} = 1;
636 _opt_err('Invalid', '-DEFAULT', 'string', 'reference')
637 0 ▶ if ref($option{-def});
Branch: 1 branch
BranchTrueFalseTotal
if ref $option{"-def"} 0 0 0
638 }
639 elsif ($arg =~ /^-def(?:ault)?/) {
640 5 ▶ _opt_err('Missing', '-default', 'string') if !@_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 5 5
641 5 $option{-def} = shift @_;
642 _opt_err('Invalid', '-default', 'string', 'reference')
643 5 ▶ if ref($option{-def});
Branch: 1 branch
BranchTrueFalseTotal
if ref $option{"-def"} 0 5 5
644 }
645 2 elsif ($arg =~ /^-d(.+)$/) { $option{-def} = $1; }
646  
647 # Specify a timeout...
648 elsif ($arg =~ /^-t(\d+)/) {
649 1 $option{-timeout} = $1;
650 1 $arg =~ s{\d+}{}xms;
651 1 $redo = 1;
652 }
653 elsif ($arg =~ /^-timeout$/) {
654 2 ▶ _opt_err('Missing', -timeout, 'number of seconds') if !@_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 1 2
655 1 $option{-timeout} = shift @_;
656 _opt_err('Invalid', -timeout,'number of seconds', qq{'$option{-timeout}'})
657 1 ▶ if !looks_like_number($option{-timeout});
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($option{"-timeout"}) 1 0 1
658 }
659  
660 # Specify a set of input constraints...
661 elsif ($arg =~ /^-g.*/) {
662 8 ▶ _opt_err('Missing', -guarantee, 'input restriction') if !@_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 8 8
663 8 my $restriction = shift @_;
664 8 my $restriction_type = ref $restriction;
665  
666 8 $option{-must}{'be a valid input'} = $restriction;
667  
668 # Hashes restrict input to their keys...
669 8 ▶ if ($restriction_type eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($restriction_type eq "HASH") 2 6 8
670 2 $restriction_type = 'ARRAY';
671 2 $restriction = [ keys %{$restriction} ];
672 }
673 # Arrays of strings matched (and completed) char-by-char...
674 8 ▶ if ($restriction_type eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($restriction_type eq 'ARRAY') { } 7 1 8
elsif ($restriction_type eq 'Regexp') { } 1 0 1
675 7 my @restrictions = @{$restriction};
676 $option{-guarantee}
677 = '\A(?:'
678 . join('|', map {
679 7 join(q{}, map { "(?:\Q$_\E" } split(q{}, $_))
680 . ')?' x length($_)
681 } @restrictions)
682 . ')\z'
683 ;
684 7 ▶ if ($option{-complete} == $DEFAULT_COMPLETER) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option{"-complete"} == $DEFAULT_COMPLETER) 7 0 7
685 7 $option{-complete} = \@restrictions;
686 }
687 }
688 # Regexes matched as-is...
689 elsif ($restriction_type eq 'Regexp') {
690 1 $option{-guarantee} = $restriction;
691 }
692 else {
693 0 _opt_err( 'Invalid', -guarantee,
694 'array or hash reference, or regex'
695 );
696 }
697 }
698  
699 # Specify a set of key letters...
700 elsif ($arg =~ '-keyletters_implement') {
701 # Extract all keys and default keys...
702 4 my @keys = ($option{-prompt} =~ m{$KL_EXTRACT}gxms);
703  
704 # Convert default to a -default...
705 4 my @defaults = ($option{-prompt} =~ m{$KL_DEF_EXTRACT}gxms);
706 4 ▶ if (@defaults > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@defaults > 1) { } 0 4 4
elsif (@defaults) { } 2 2 4
707 0 _warn( ambiguous =>
708 "prompt(): -keyletters found too many defaults"
709 )
710 }
711 elsif (@defaults) {
712 2 push @_, -default => $defaults[0];
713 }
714  
715 # Convert key letters to a -guarantee...
716 4 @keys = ( map({uc} @keys), map({lc} @keys) );
717 4 ▶ if (@defaults == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@defaults == 1) 2 2 4
718 2 push @keys, q{};
719 }
720 4 push @_, -guarantee => \@keys;
721  
722 }
723 elsif ($arg =~ /^-key(?:let(?:ter)?)(?:s)?/) {
724 2 push @_, '-keyletters_implement';
725 }
726 elsif ($arg =~ /^-k/) {
727 2 push @_, '-keyletters_implement';
728 2 $redo = 1;
729 }
730  
731 # Specify a set of return constraints...
732 elsif ($arg =~ /^-must$/) {
733 6 ▶ _opt_err('Missing', -must, 'constraint hash') if !@_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 6 6
734 6 my $must = shift @_;
735 6 ▶ _opt_err('Invalid', -must, 'hash reference')
Branch: 1 branch
BranchTrueFalseTotal
if ref $must ne "HASH" 0 6 6
736 if ref($must) ne 'HASH';
737 6 for my $errmsg (keys %{$must}) {
738 6 $option{-must}{$errmsg} = $must->{$errmsg};
739 }
740 }
741  
742 # Specify a history set...
743 elsif ($arg =~ /^-history/) {
744 $option{-history}
745 0 ▶ = @_ && $_[0] !~ /^-/ ? shift @_
Branch: 1 branch
BranchTrueFalseTotal
@_ && !($_[0] =~ /^-/) ? : 0 0 0
Condition0%0/3
@_ and not $_[0] =~ /^-/
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and not $_[0] =~ /^-/
@_ not $_[0] =~ /^-/
Truth table0%0/3
A and not(B)
A= @_B= not $_[0] =~ /^-/
ABresult
0X0
100
111
746 : undef;
747 _opt_err('Invalid', -history, 'history set name', qq{'$option{-history}'})
748 0 ▶ if ref($option{-history});
Branch: 1 branch
BranchTrueFalseTotal
if ref $option{"-history"} 0 0 0
749 }
750 0 ▶ elsif ($arg =~ /^-h(.*)/) { $option{-history} = length($1) ? $1 : undef; }
Branch: 1 branch
BranchTrueFalseTotal
length $1 ? : 0 0 0
751  
752 # Specify completions...
753 elsif ($arg =~ /^-comp(?:lete)?/) {
754 0 ▶ _opt_err('Missing', -complete, 'completions') if !@_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
755 0 my $comp_spec = shift @_;
756 0 ▶ my $comp_type = ref($comp_spec) || $comp_spec || '???';
Condition0%0/5
ref $comp_spec || $comp_spec || '???'
l!l
00
ref $comp_spec or $comp_spec
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $comp_spec || $comp_spec || '???'
ref $comp_spec $comp_spec
Truth table0%0/3
A or B
A= ref $comp_specB= $comp_spec
ABresult
1X1
011
000
757 0 ▶ if ($comp_type =~ m{\A(?: file\w* | dir\w* | ARRAY | HASH | CODE )\Z}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comp_type =~ /\A(?: file\w* | dir\w* | ARRAY | HASH | CODE )\Z/msx) { } 0 0 0
758 0 $option{-complete} = $comp_spec;
759 }
760 else {
761 0 _opt_err( 'Invalid', -complete,
762 '"filenames", "dirnames", or reference to array, hash, or subroutine');
763 }
764 }
765  
766 # Specify what to echo when a character is keyed...
767 elsif ($arg =~ /^-(echo|ret(?:urn)?)$/) {
768 0 ▶ my $flag = $1 eq 'echo' ? '-echo' : '-return';
Branch: 1 branch
BranchTrueFalseTotal
$1 eq 'echo' ? : 0 0 0
769 37 ▶ if ($flag eq '-echo' && !eval { no warnings 'deprecated'; require Term::ReadKey }) {
Subroutine BEGIN: called CC=1 SCAR=2.9
Branch: 1 branch
BranchTrueFalseTotal
if ($flag eq "-echo" and not eval { do { BEGIN {${^WARNING_BITS} = "\x45\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x54\x10\x54\x10\x00\x40\x00\x00\x55\x55"} require Term::ReadKey } }) 0 0 0
Condition0%0/3
$flag eq "-echo" and not eval { do { BEGIN {${^WARNING_BITS} = "\x45\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x54\x10\x54\x10\x00\x40\x00\x00\x55\x55"} require Term::ReadKey } }
!ll&&!rl&&r
000
MC/DC0%0/2
$flag eq "-echo" and not eval { do { BEGIN {${^WARNING_BITS} = "\x45\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x54\x10\x54\x10\x00\x40\x00\x00\x55\x55"} require Term::ReadKey } }
$flag eq "-echo" not eval { do { BEGIN {${^WARNING_BITS} = "\x45\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x54\x10\x54\x10\x00\x40\x00\x00\x55\x55"} require Term::ReadKey } }
Truth table0%0/3
A and not(B)
A= $flag eq "-echo"B= not eval { do { BEGIN {${^WARNING_BITS} = "\x45\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x54\x10\x54\x10\x00\x40\x00\x00\x55\x55"} require Term::ReadKey } }
ABresult
0X0
100
111
770 0 _warn( bareword => "Warning: next input will be in plaintext\n");
771 }
772 0 ▶ my $arg = @_ && $_[0] !~ /^-/ ? shift(@_)
Branch: 2 branches
BranchTrueFalseTotal
@_ && !($_[0] =~ /^-/) ? : 0 0 0
$flag eq '-echo' ? : 0 0 0
Condition0%0/3
@_ and not $_[0] =~ /^-/
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and not $_[0] =~ /^-/
@_ not $_[0] =~ /^-/
Truth table0%0/3
A and not(B)
A= @_B= not $_[0] =~ /^-/
ABresult
0X0
100
111
773 : $flag eq '-echo' ? q{}
774 : qq{\n};
775 0 $option{$flag} = _gen_wrapper_for(_decode_echo($arg));
776 }
777 elsif ($arg =~ /^-e(.*)/) {
778 37 ▶ if (!eval { no warnings 'deprecated'; require Term::ReadKey }) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { BEGIN {${^WARNING_BITS} = "\x45\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x54\x10\x54\x10\x00\x40\x00\x00\x55\x55"} require Term::ReadKey } }) 1 0 1
779 1 _warn( bareword => "Warning: next input will be in plaintext\n");
780 }
781 1 my $arg = $1;
782 1 $option{-echo} = _gen_wrapper_for(_decode_echo($arg));
783 }
784 elsif ($arg =~ /^-r(.+)/) {
785 0 my $arg = $1;
786 0 $option{-return} = _gen_wrapper_for(_decode_echo($arg));
787 }
788 elsif ($arg =~ /^-r/) {
789 0 ▶ $option{-return} = sub{ "\n" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
790 }
791  
792 # Specify an initial input...
793 elsif ($arg =~ /^-prefill/) {
794 0 ▶ _opt_err('Missing', '-prefill', 'string') if !@_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
795 0 $option{-prefill} = shift @_;
796 _opt_err('Invalid', '-prefill', 'string', 'reference')
797 0 ▶ if ref($option{-prefill});
Branch: 1 branch
BranchTrueFalseTotal
if ref $option{"-prefill"} 0 0 0
798 }
799  
800 # Explicit prompt replaces implicit prompts...
801 elsif ($arg =~ /^-prompt$/) {
802 0 ▶ _opt_err('Missing', '-prompt', 'prompt string') if !@_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
803 0 $option{-prompt} = shift @_;
804 _opt_err('Invalid', '-prompt', 'string', 'reference')
805 0 ▶ if ref($option{-prompt});
Branch: 1 branch
BranchTrueFalseTotal
if ref $option{"-prompt"} 0 0 0
806 }
807 elsif ($arg =~ /^-p(\S*)$/) {
808 0 $option{-prompt} = $1;
809 }
810  
811 # Menus inject a placeholder in the prompt string...
812 elsif ($arg =~ /^-menu$/) {
813 1 ▶ _opt_err('Missing', '-menu', 'menu specification') if !@_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 1 1
814 1 ▶ $option{-menu} = ref $_[0] ? shift(@_) : \shift(@_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 1 0 1
815 1 $option{-prompt} .= $MENU_MK;
816 1 $option{-def_nocheck} = 1;
817 }
818  
819 # A monitoring sub is called on every input character...
820 elsif ($arg =~ /^-monitor/) {
821 0 ▶ _opt_err('Missing', '-monitor', 'a monitor subref')
Branch: 1 branch
BranchTrueFalseTotal
if not @_ or ref $_[0] ne "CODE" 0 0 0
Condition0%0/3
not @_ or ref $_[0] ne "CODE"
l!l&&r!l&&!r
000
MC/DC0%0/2
not @_ or ref $_[0] ne "CODE"
not @_ ref $_[0] ne "CODE"
Truth table0%0/3
not(A) or B
A= not @_B= ref $_[0] ne "CODE"
ABresult
1X1
011
000
822 if !@_ || ref $_[0] ne 'CODE';
823 0 $option{-monitor} = shift(@_);
824 }
825  
826 # Anything else of the form '-...' is a misspelt option...
827 2 elsif ($arg =~ /^-\w+$/) { _warn(misc => "prompt(): Unknown option $arg ignored"); }
828  
829 # Anything else is part fo the prompt...
830 96 else { $option{-prompt} .= $arg; }
831  
832 # Handle option bundling...
833 218 ▶ redo DECODING if $redo && $arg =~ s{\A -.{$redo} (?=.)}{-}xms;
Branch: 1 branch
BranchTrueFalseTotal
if $redo and $arg =~ s/\A -.{$redo} (?=.)/-/msx 12 206 218
Condition100%3/3
$redo and $arg =~ s/\A -.{$redo} (?=.)/-/msx
!ll&&!rl&&r
1881812
MC/DC100%2/2
$redo and $arg =~ s/\A -.{$redo} (?=.)/-/msx
$redo $arg =~ s/\A -.{$redo} (?=.)/-/msx
Truth table100%3/3
A and B
A= $redoB= $arg =~ s/\A -.{$redo} (?=.)/-/msx
ABresult
0X0
100
111
834 }
835 }
836  
837 # Precompute top-level menu, if menuing...
838 97 ▶ if (exists $option{-menu}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option{"-menu"}) 1 96 97
839 $option{-menu} = _build_menu($option{-menu},
840 undef,
841 $option{-number}||$option{-integer}
842 1 ▶ );
Condition33%1/3
$option{'-number'} || $option{'-integer'}
l!l&&r!l&&!r
100
MC/DC0%0/2
$option{'-number'} || $option{'-integer'}
$option{'-number'} $option{'-integer'}
Truth table33%1/3
A || B
A= $option{'-number'}B= $option{'-integer'}
ABresult
1X1
011
000
843 }
844  
845 # Handle return magic on -single...
846 97 ▶ if (defined $option{-single} && length($option{-echo}('X')//'echoself')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $option{"-single"} and length($option{"-echo"}->("X") // "echoself")) 6 91 97
Condition60%3/5
defined $option{"-single"} and length($option{"-echo"}->("X") // "echoself")
!ll&&!rl&&r
9106
$option{"-echo"}->("X") // "echoself"
l!l
60
MC/DC50%1/2
defined $option{"-single"} and length($option{"-echo"}->("X") // "echoself")
defined $option{"-single"} $option{"-echo"}->("X")
Truth table66%2/3
A and B
A= defined $option{"-single"}B= $option{"-echo"}->("X")
ABresult
0X0
100
111
847 6 ▶ $option{-return} //= sub{ "\n" };
Subroutine __ANON__: not called CC=1 SCAR=2.0
Condition33%1/3
$option{"-return"} //= sub { "\n"; }
l!l&&r!l&&!r
600
MC/DC0%0/2
$option{"-return"} //= sub { "\n"; }
$option{"-return"} sub { "\n"; }
Truth table33%1/3
A //= B
A= $option{"-return"}B= sub { "\n"; }
ABresult
1X1
011
000
848 }
849  
850 # Adjust prompt as necessary...
851 97 ▶ if ($option{-argv}) {
Branch: 4 branches
BranchTrueFalseTotal
if ($option{'-argv'}) { } 1 96 97
elsif (not defined $option{'-prompt'}) { } 3 93 96
elsif ($option{'-prompt'} =~ / \S \z/msx) { } 79 14 93
elsif ($option{'-prompt'} =~ / (.*) \n \z/msx) { } 0 14 14
852 1 ▶ my $progname = $option{-prompt} // $0;
Condition33%1/3
$option{"-prompt"} // $0
l!l&&r!l&&!r
100
MC/DC0%0/2
$option{"-prompt"} // $0
$option{"-prompt"} $0
Truth table33%1/3
A // B
A= $option{"-prompt"}B= $0
ABresult
1X1
011
000
853 1 $progname =~ s{^.*/}{}xms;
854  
855 1 my $HINT = '[enter command line args here]';
856 1 $option{-prompt} = "> $progname $HINT\r> $progname ";
857  
858 1 $option{-complete} = 'filenames';
859  
860 1 my $not_first;
861 $option{-echo} = sub{
862 0 ▶ my $char = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
863 0 $option{-prompt} = "> $progname "; # Sneaky resetting to handle completions
864 0 ▶ return $char if $not_first++;
Branch: 1 branch
BranchTrueFalseTotal
if $not_first++ 0 0 0
865 0 return "\r> $progname " . (q{ } x length $HINT) . "\r> $progname $char";
866 }
867 1 }
868 elsif (!defined $option{-prompt}) {
869 3 $option{-prompt} = '> ';
870 }
871 elsif ($option{-prompt} =~ m{ \S \z}xms) {
872 # If prompt doesn't end in whitespace, make it so...
873 79 $option{-prompt} .= ' ';
874 }
875 elsif ($option{-prompt} =~ m{ (.*) \n \z}xms) {
876 # If prompt ends in a newline, remove it...
877 0 $option{-prompt} = $1;
878 }
879  
880 # Steal history set name if -h given without a specification...
881 97 ▶ $option{-history} //= $option{-prompt};
Condition33%1/3
$option{"-history"} //= $option{"-prompt"}
l!l&&r!l&&!r
9700
MC/DC0%0/2
$option{"-history"} //= $option{"-prompt"}
$option{"-history"} $option{"-prompt"}
Truth table33%1/3
A //= B
A= $option{"-history"}B= $option{"-prompt"}
ABresult
1X1
011
000
882  
883 # Verify any default satisfies any constraints...
884 97 ▶ if (exists $option{-def} && !$option{-def_nocheck}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $option{"-def"} and not $option{"-def_nocheck"}) 6 91 97
Condition100%3/3
exists $option{"-def"} and not $option{"-def_nocheck"}
!ll&&!rl&&r
9016
MC/DC100%2/2
exists $option{"-def"} and not $option{"-def_nocheck"}
exists $option{"-def"} not $option{"-def_nocheck"}
Truth table100%3/3
A and not(B)
A= exists $option{"-def"}B= not $option{"-def_nocheck"}
ABresult
0X0
100
111
885 6 ▶ if (!_verify_input_constraints(\q{},undef,undef,\%option)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_verify_input_constraints(\"", undef, undef, \%option)) 0 6 6
886 0 _warn( misc =>
887 'prompt(): -default value does not satisfy -must constraints'
888 );
889 }
890 }
891  
892 97 return \%option;
893 }
894  
895 #====[ Error Handlers ]=========================================
896  
897 sub _opt_err {
898 2 ▶ my ($problem, $option, $expectation, $found) = @_;
Subroutine _opt_err: called CC=2 SCAR=6.9
899 2 ▶ if (@_ > 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 3) { } 1 1 2
900 1 Carp::croak "prompt(): $problem value for $option (expected $expectation, but found $found)";
901 }
902 else {
903 1 Carp::croak "prompt(): $problem value for $option (expected $expectation)";
904 }
905 }
906  
907 sub _warn {
908 5 ▶ my ($category, @message) = @_;
Subroutine _warn: called CC=3 SCAR=11.0
909  
910 5 ▶ return if !warnings::enabled($category);
Branch: 1 branch
BranchTrueFalseTotal
unless warnings::enabled($category) 2 3 5
911  
912 3 my $message = join(q{},@message);
913 3 ▶ warn $message =~ /\n$/ ? $message : Carp::shortmess($message);
Branch: 1 branch
BranchTrueFalseTotal
$message =~ /\n$/ ? : 1 2 3
914 }
915  
916  
917 #====[ Utility subroutines ]====================================
918  
919 # Return the *ARGV filehandle, "magic-opening" it if necessary...
920 sub _open_ARGV {
921 80 ▶ if (!openhandle \*ARGV) {
Subroutine _open_ARGV: called CC=4 SCAR=22.5
Branch: 1 branch
BranchTrueFalseTotal
unless (&openhandle(\*ARGV)) 0 80 80
922 0 ▶ $ARGV = shift @ARGV // '-';
Condition0%0/2
shift @ARGV // "-"
l!l
00
MC/DC0%0/1
shift @ARGV // "-"
shift @ARGV
Truth table0%0/2
A
A= shift @ARGV
Aresult
11
00
923 0 ▶ open *ARGV or Carp::croak(qq{prompt(): Can't open *ARGV: $!});
Branch: 1 branch
BranchTrueFalseTotal
open *ARGV or Carp::croak("prompt(): Can't open *ARGV: $!") 0 0 0
924 }
925 80 return \*ARGV;
926 }
927  
928 37 my $INTEGER_PAT = qr{ \A \s*+ [+-]?+ \d++ (?: [Ee] \+? \d+ )? \s*+ \Z }xms;
929  
930 37 my $NUMBER_PAT = qr{
931 \A \s*+ [+-]?+
932 (?:
933 \d++ (?: [.,] \d*+ )?
934 | [.,] \d++
935 )
936 (?: [eE] [+-]?+ \d++ )?
937 \s*+ \Z
938 }xms;
939  
940 # Verify interactive constraints...
941 sub _verify_input_constraints {
942 121 ▶ my ($input_ref, $local_fake_input_ref, $outputter_ref, $opt_ref, $extras)
Subroutine _verify_input_constraints: called CC=25 SCAR=32.6
943 = @_;
944  
945 # Use default if appropriate (but short-circuit checks if -DEFAULT set)...
946 121 my $input = ${$input_ref};
947 121 ▶ if (${$input_ref} =~ m{^\R?$}xms && exists $opt_ref->{-def}) {
Branch: 1 branch
BranchTrueFalseTotal
if (${$input_ref;} =~ /^\R?$/msx and exists $opt_ref->{"-def"}) 11 110 121
Condition66%2/3
${$input_ref;} =~ /^\R?$/msx and exists $opt_ref->{"-def"}
!ll&&!rl&&r
110011
MC/DC50%1/2
${$input_ref;} =~ /^\R?$/msx and exists $opt_ref->{"-def"}
${$input_ref;} =~ /^\R?$/msx exists $opt_ref->{"-def"}
Truth table66%2/3
A and B
A= ${$input_ref;} =~ /^\R?$/msxB= exists $opt_ref->{"-def"}
ABresult
0X0
100
111
948 11 ▶ return 1 if $opt_ref->{-def_nocheck};
Branch: 1 branch
BranchTrueFalseTotal
if $opt_ref->{"-def_nocheck"} 1 10 11
949 $input = $opt_ref->{-def}
950 10 }
951 120 chomp $input;
952  
953 120 my $failed;
954 # Integer constraint is hard-coded...
955 120 ▶ if ($opt_ref->{-integer} && $input !~ $INTEGER_PAT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_ref->{"-integer"} and not $input =~ /$INTEGER_PAT/) 4 116 120
Condition100%3/3
$opt_ref->{"-integer"} and not $input =~ /$INTEGER_PAT/
!ll&&!rl&&r
102144
MC/DC100%2/2
$opt_ref->{"-integer"} and not $input =~ /$INTEGER_PAT/
$opt_ref->{"-integer"} not $input =~ /$INTEGER_PAT/
Truth table100%3/3
A and not(B)
A= $opt_ref->{"-integer"}B= not $input =~ /$INTEGER_PAT/
ABresult
0X0
100
111
956 4 $failed = $opt_ref->{-prompt} . "(must be an integer) ";
957 }
958  
959 # Numeric constraint is hard-coded...
960 120 ▶ if (!$failed && $opt_ref->{-number} && $input !~ $NUMBER_PAT) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $failed and $opt_ref->{"-number"} and not $input =~ /$NUMBER_PAT/) 2 118 120
Condition100%6/6
not $failed and $opt_ref->{"-number"} and not $input =~ /$NUMBER_PAT/
!ll&&!rl&&r
11622
not $failed and $opt_ref->{"-number"}
!ll&&!rl&&r
41124
MC/DC100%3/3
not $failed and $opt_ref->{"-number"} and not $input =~ /$NUMBER_PAT/
not $failed $opt_ref->{"-number"} not $input =~ /$NUMBER_PAT/
Truth table100%4/4
not(A) and B and not(C)
A= not $failedB= $opt_ref->{"-number"}C= not $input =~ /$NUMBER_PAT/
ABCresult
0XX0
10X0
1100
1111
961 2 $failed = $opt_ref->{-prompt} . "(must be a number) ";
962 }
963  
964 # Sort and clean up -must list...
965 120 ▶ my $must_ref = $opt_ref->{-must} // {};
Condition50%1/2
$opt_ref->{"-must"} // {}
l!l
1200
MC/DC0%0/1
$opt_ref->{"-must"} // {}
$opt_ref->{"-must"}
Truth table50%1/2
A
A= $opt_ref->{"-must"}
Aresult
11
00
966 120 my @must_keys = sort keys %{$must_ref};
967 120 ▶ my %clean_key_for = map { $_ => (/^\d+[.:]?\s*(.*)/s ? $1 : $_) } @must_keys;
Branch: 1 branch
BranchTrueFalseTotal
/^\d+[.:]?\s*(.*)/s ? : 0 36 36
968 120 my @must_kv_list = map { $clean_key_for{$_} => $must_ref->{$_} } @must_keys;
969  
970 # Combine -yesno and -must constraints...
971 my %constraint_for = (
972 120 ▶ %{ $extras // {} },
Condition50%1/2
$extras // {}
l!l
0120
MC/DC0%0/1
$extras // {}
$extras
Truth table50%1/2
A
A= $extras
Aresult
11
00
973 120 ▶ %{ $opt_ref->{-yesno}{must} // {} },
Condition100%2/2
$opt_ref->{"-yesno"}{"must"} // {}
l!l
2694
MC/DC100%1/1
$opt_ref->{"-yesno"}{"must"} // {}
$opt_ref->{"-yesno"}{"must"}
Truth table100%2/2
A
A= $opt_ref->{"-yesno"}{"must"}
Aresult
11
00
974 @must_kv_list,
975 );
976 my @constraints = (
977 120 ▶ keys %{ $extras // {} },
Condition50%1/2
$extras // {}
l!l
0120
MC/DC0%0/1
$extras // {}
$extras
Truth table50%1/2
A
A= $extras
Aresult
11
00
978 120 ▶ keys %{ $opt_ref->{-yesno}{must} // {} },
Condition100%2/2
$opt_ref->{"-yesno"}{"must"} // {}
l!l
2694
MC/DC100%1/1
$opt_ref->{"-yesno"}{"must"} // {}
$opt_ref->{"-yesno"}{"must"}
Truth table100%2/2
A
A= $opt_ref->{"-yesno"}{"must"}
Aresult
11
00
979 120 @clean_key_for{@must_keys},
980 );
981  
982 # User-specified constraints...
983 120 ▶ if (!$failed && keys %constraint_for) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $failed and keys %constraint_for) 62 58 120
Condition100%3/3
not $failed and keys %constraint_for
!ll&&!rl&&r
65262
MC/DC100%2/2
not $failed and keys %constraint_for
not $failed keys %constraint_for
Truth table100%3/3
not(A) and B
A= not $failedB= keys %constraint_for
ABresult
0X0
100
111
984 CONSTRAINT:
985 62 for my $msg (@constraints) {
986 62 my $constraint = $constraint_for{$msg};
987 37 ▶ next CONSTRAINT if eval { no warnings; local $_ = $input; match($input, $constraint); };
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { no warnings; local $_ = $input; match($input, $constraint) } } 40 22 62
988 $failed = $msg =~ m{\A [[:upper:]] }xms ? "$msg "
989 : $msg =~ m{\A \W }xms ? $opt_ref->{-prompt}
990 . "$msg "
991 : $opt_ref->{-prompt}
992 22 ▶ . "(must $msg) "
Branch: 2 branches
BranchTrueFalseTotal
$msg =~ /\A [[:upper:]] /msx ? : 2 20 22
$msg =~ /\A \W /msx ? : 5 15 20
993 ;
994 22 last CONSTRAINT;
995 }
996 }
997  
998 # If any constraint not satisfied...
999 120 ▶ if ($failed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($failed) 28 92 120
1000 # Return failure if not actually prompting at the moment...
1001 28 ▶ return 0 if !$outputter_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $outputter_ref 0 28 28
1002  
1003 # Redraw post-menu prompt with failure message appended...
1004 28 $failed =~ s{.*$MENU_MK}{}xms;
1005 28 $outputter_ref->(-style => _wipe_line(), $failed);
1006  
1007 # Reset input collector...
1008 28 ${$input_ref} = q{};
1009  
1010 # Reset faked input, if any...
1011 28 ▶ if (defined $fake_input && length($fake_input) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fake_input and length $fake_input > 0) 0 28 28
Condition33%1/3
defined $fake_input and length $fake_input > 0
!ll&&!rl&&r
2800
MC/DC0%0/2
defined $fake_input and length $fake_input > 0
defined $fake_input length $fake_input > 0
Truth table33%1/3
A and B
A= defined $fake_inputB= length $fake_input > 0
ABresult
0X0
100
111
1012 0 $fake_input =~ s{ \A (.*) \R? }{}xm;
1013 0 ${$local_fake_input_ref} = $1;
1014 }
1015  
1016 37 ▶ no warnings 'exiting';
Subroutine BEGIN: called CC=1 SCAR=0.0
1017 28 next INPUT;
1018 }
1019  
1020 # Otherwise succeed...
1021 92 return 1;
1022 }
1023  
1024 # Build a sub to read from specified filehandle, with or without timeout...
1025 sub _generate_buffered_reader_from {
1026 97 ▶ my ($in_fh, $outputter_ref, $opt_ref) = @_;
Subroutine _generate_buffered_reader_from: called CC=7 SCAR=20.7
1027  
1028 # Set-up for timeouts...
1029 97 ▶ my $fileno = fileno($in_fh) // -1;
Condition50%1/2
fileno $in_fh // -1
l!l
970
MC/DC0%0/1
fileno $in_fh // -1
fileno $in_fh
Truth table50%1/2
A
A= fileno $in_fh
Aresult
11
00
1030 97 ▶ my $has_timeout = exists $opt_ref->{-timeout} && $fileno >= 0;
Condition66%2/3
exists $opt_ref->{'-timeout'} && $fileno >= 0
!ll&&!rl&&r
9610
MC/DC0%0/2
exists $opt_ref->{'-timeout'} && $fileno >= 0
exists $opt_ref->{'-timeout'} $fileno >= 0
Truth table66%2/3
A && B
A= exists $opt_ref->{'-timeout'}B= $fileno >= 0
ABresult
0X0
100
111
1031 97 my $timeout = $opt_ref->{-timeout};
1032 97 my $readbits = q{};
1033 97 ▶ if ($has_timeout && $fileno >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_timeout and $fileno >= 0) 0 97 97
Condition33%1/3
$has_timeout and $fileno >= 0
!ll&&!rl&&r
9700
MC/DC0%0/2
$has_timeout and $fileno >= 0
$has_timeout $fileno >= 0
Truth table33%1/3
A and B
A= $has_timeoutB= $fileno >= 0
ABresult
0X0
100
111
1034 0 vec($readbits,$fileno,1) = 1;
1035 }
1036  
1037 # Set up local faked input, if any...
1038 97 my $local_fake_input;
1039 my $orig_fake_input;
1040 97 ▶ if (defined $fake_input && length($fake_input) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fake_input and length $fake_input > 0) 4 93 97
Condition66%2/3
defined $fake_input and length $fake_input > 0
!ll&&!rl&&r
9304
MC/DC50%1/2
defined $fake_input and length $fake_input > 0
defined $fake_input length $fake_input > 0
Truth table66%2/3
A and B
A= defined $fake_inputB= length $fake_input > 0
ABresult
0X0
100
111
1041 4 $fake_input =~ s{ \A (.*) \R? }{}xm;
1042 4 $orig_fake_input = $local_fake_input = $1;
1043 }
1044  
1045 return sub {
1046 97 ▶ my ($extra_constraints) = @_;
Subroutine __ANON__: called CC=12 SCAR=26.9
1047  
1048 INPUT:
1049 97 while (1) {
1050 125 ▶ if (!$has_timeout || select $readbits, undef, undef, $timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $has_timeout or select $readbits, undef, undef, $timeout) { } 125 0 125
Condition33%1/3
not $has_timeout or select $readbits, undef, undef, $timeout
l!l&&r!l&&!r
12500
MC/DC0%0/2
not $has_timeout or select $readbits, undef, undef, $timeout
not $has_timeout select $readbits, undef, undef, $timeout
Truth table33%1/3
not(A) or B
A= not $has_timeoutB= select $readbits, undef, undef, $timeout
ABresult
1X1
011
000
1051 125 my $input;
1052  
1053 # Real input comes from real filehandles...
1054 125 ▶ if (!defined $local_fake_input) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $local_fake_input) { } 121 4 125
1055 121 $input = readline $in_fh;
1056 }
1057 # Fake input has to be typed...
1058 else {
1059 4 $input = $local_fake_input;
1060 4 sleep 1;
1061 4 for ($local_fake_input =~ m/\X/g) {
1062 24 _simulate_typing();
1063 24 $outputter_ref->(-echostyle => $opt_ref->{-echo}($_));
1064 }
1065 4 readline $in_fh;
1066  
1067 # Check for simulated EOF...
1068 4 ▶ if ($input =~ m{^ \s* (?: \cD | \cZ ) }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input =~ /^ \s* (?: \cD | \cZ ) /msx) 0 4 4
1069 0 $input = undef;
1070 }
1071 }
1072  
1073 125 ▶ if (exists $opt_ref->{-cancel}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt_ref->{"-cancel"}) 0 125 125
1074 0 for my $nextchar (split q{}, $input) {
1075 die bless \$input, 'IO::Prompter::Cancellation'
1076 0 ▶ if match($nextchar, $opt_ref->{-cancel});
Branch: 1 branch
BranchTrueFalseTotal
if match($nextchar, $opt_ref->{"-cancel"}) 0 0 0
1077 }
1078 }
1079  
1080 125 ▶ if (defined $input) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $input) 115 10 125
1081 115 _verify_input_constraints(
1082 \$input, \$local_fake_input, $outputter_ref, $opt_ref, $extra_constraints
1083 );
1084 }
1085  
1086 return defined $input && $opt_ref->{-single}
1087 97 ▶ ? substr($input, 0, 1)
Branch: 1 branch
BranchTrueFalseTotal
defined $input && $opt_ref->{'-single'} ? : 6 91 97
Condition100%3/3
defined $input and $opt_ref->{"-single"}
!ll&&!rl&&r
10816
MC/DC100%2/2
defined $input and $opt_ref->{"-single"}
defined $input $opt_ref->{"-single"}
Truth table100%3/3
A and B
A= defined $inputB= $opt_ref->{"-single"}
ABresult
0X0
100
111
1088 : $input;
1089 }
1090 else {
1091 0 return;
1092 }
1093 }
1094 }
1095 97 }
1096  
1097 sub _autoflush {
1098 0 ▶ my ($fh) = @_;
Subroutine _autoflush: not called CC=1 SCAR=6.9
1099 0 my $prev_selected = select $fh;
1100 0 $| = 1;
1101 0 select $prev_selected;
1102 0 return;
1103 }
1104  
1105 sub _simulate_typing {
1106 24 ▶ state $TYPING_SPEED = 0.07; # seconds per character
Subroutine _simulate_typing: called CC=1 SCAR=0.0
1107 24 select undef, undef, undef, rand $TYPING_SPEED;
1108 }
1109  
1110 sub _term_width {
1111 37 ▶ my ($term_width) = eval { no warnings 'deprecated'; Term::ReadKey::GetTerminalSize(\*STDERR) };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine _term_width: called CC=2 SCAR=7.1
1112 28 ▶ return $term_width // $DEFAULT_TERM_WIDTH;
Condition33%1/3
$term_width // $DEFAULT_TERM_WIDTH
l!l&&r!l&&!r
0280
MC/DC0%0/2
$term_width // $DEFAULT_TERM_WIDTH
$term_width $DEFAULT_TERM_WIDTH
Truth table33%1/3
A // B
A= $term_widthB= $DEFAULT_TERM_WIDTH
ABresult
1X1
011
000
1113 }
1114  
1115 sub _wipe_line {
1116 28 ▶ return qq{\r} . q{ } x (_term_width()-1) . qq{\r};
Subroutine _wipe_line: called CC=1 SCAR=0.0
1117 }
1118  
1119 # Convert a specification into a list of possible completions...
1120 sub _current_completions_for {
1121 0 ▶ my ($input_text, $opt_ref) = @_;
Subroutine _current_completions_for: not called CC=9 SCAR=45.0
1122 0 my $completer = $opt_ref->{-complete};
1123  
1124 # Isolate the final whitespace-separated word...
1125 0 my ($prefix, $lastword)
1126 = $input_text =~ m{
1127 (?| ^ (.*\s+) (.*)
1128 | ^ () (.*)
1129 )
1130 }xms;
1131  
1132 # Find candidates...
1133 0 my @candidates;
1134 0 ▶ for my $completer_type (ref($completer) || $completer // q{}) {
Condition0%0/5
(ref $completer || $completer) // ""
l!l
00
ref $completer or $completer
l!l&&r!l&&!r
000
MC/DC0%0/2
(ref $completer || $completer) // ""
ref $completer $completer
Truth table0%0/3
A or B
A= ref $completerB= $completer
ABresult
1X1
011
000
1135 # If completer is sub, recursively call it with input words...
1136 0 ▶ if ($completer_type eq 'CODE') {
Branch: 5 branches
BranchTrueFalseTotal
if ($completer_type eq 'CODE') { } 0 0 0
elsif ($completer_type eq 'ARRAY') { } 0 0 0
elsif ($completer_type eq 'HASH') { } 0 0 0
elsif ($completer_type eq /^file\w*$/) { } 0 0 0
elsif ($completer_type eq /^dir\w*$/) { } 0 0 0
1137 ($prefix, @candidates)
1138 = _current_completions_for(
1139 $input_text,
1140 0 { %{$opt_ref},
1141 -complete => $completer->(split /\s+/, $input_text, -1)
1142 }
1143 );
1144 }
1145  
1146 # If completer is array, grep the appropriate elements...
1147 elsif ($completer_type eq 'ARRAY') {
1148 0 @candidates = grep { /\A\Q$lastword\E/ } @{$completer};
1149 }
1150  
1151 # If completer is hash, grep the appropriate keys...
1152 elsif ($completer_type eq 'HASH') {
1153 0 @candidates = grep { /\A\Q$lastword\E/ } keys %{$completer};
1154 }
1155  
1156 # If completer is 'file...', glob up the appropriate filenames...
1157 elsif ($completer_type eq /^file\w*$/) {
1158 0 @candidates = glob($lastword.'*');
1159 }
1160  
1161 # If completer is 'dir...', glob up the appropriate directories...
1162 elsif ($completer_type eq /^dir\w*$/) {
1163 0 @candidates = grep {-d} glob($lastword.'*');
1164 }
1165 }
1166  
1167 0 chomp @candidates;
1168 0 return ($prefix, @candidates);
1169 }
1170  
1171  
1172 sub _current_history_for {
1173 0 ▶ my ($prefix, $opt_ref) = @_;
Subroutine _current_history_for: not called CC=2 SCAR=17.9
1174  
1175 0 my $prefix_len = length($prefix);
1176 0 ▶ return q{}, map { /\A (.*?) \R \Z/x ? $1 : $_ }
Branch: 1 branch
BranchTrueFalseTotal
/\A (.*?) \R \Z/x ? : 0 0 0
1177 0 grep { substr($_,0,$prefix_len) eq $prefix }
1178 0 @{ $history_cache{$opt_ref->{-history}} };
1179 }
1180  
1181 sub _longest_common_prefix_for {
1182 0 ▶ my $prefix = shift @_;
Subroutine _longest_common_prefix_for: not called CC=3 SCAR=24.8
1183 0 for my $comparison (@_) {
1184 0 ($comparison ^ $prefix) =~ m{ \A (\0*) }xms;
1185 0 my $common_length = length($1);
1186 0 ▶ return q{} if !$common_length;
Branch: 1 branch
BranchTrueFalseTotal
unless $common_length 0 0 0
1187 0 $prefix = substr($prefix, 0, $common_length);
1188 }
1189 0 return $prefix;
1190 }
1191  
1192 sub _display_completions {
1193 0 ▶ my ($input, @candidates) = @_;
Subroutine _display_completions: not called CC=4 SCAR=30.0
1194  
1195 0 ▶ return q{} if @candidates <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if @candidates <= 1 0 0 0
1196  
1197 # How big is each field in the table?
1198 0 my $field_width
1199 = _term_width() / $COMPLETE_DISPLAY_FIELDS - $COMPLETE_DISPLAY_GAP;
1200  
1201 # Crop the possibilities intelligently to that width...
1202 0 for my $candidate (@candidates) {
1203 0 substr($candidate, 0, length($input)) =~ s{ \A .* [/\\] }{}xms;
1204 0 $candidate
1205 = sprintf "%-*s", $field_width, substr($candidate,0,$field_width);
1206 }
1207  
1208 # Collect them into rows...
1209 0 my $display = "\n";
1210 0 my $gap = q{ } x $COMPLETE_DISPLAY_GAP;
1211 0 ▶ while (@candidates) {
Branch: 1 branch
BranchTrueFalseTotal
if (@candidates) 0 0 0
1212 0 $display .= $gap
1213 . join($gap, splice(@candidates, 0, $COMPLETE_DISPLAY_FIELDS))
1214 . "\n";
1215 }
1216  
1217 0 return $display;
1218 }
1219  
1220 sub _generate_unbuffered_reader_from {
1221 97 ▶ my ($in_fh, $outputter_ref, $opt_ref) = @_;
Subroutine _generate_unbuffered_reader_from: called CC=11 SCAR=48.0
1222  
1223 37 ▶ my $has_readkey = eval { no warnings 'deprecated'; require Term::ReadKey };
Subroutine BEGIN: called CC=1 SCAR=0.0
1224  
1225 # If no per-character reads, fall back on buffered input...
1226 97 ▶ if (!-t $in_fh || !$has_readkey) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-t $in_fh and $has_readkey) 97 0 97
Condition33%1/3
-t $in_fh and $has_readkey
!ll&&!rl&&r
9700
MC/DC0%0/2
-t $in_fh and $has_readkey
-t $in_fh $has_readkey
Truth table33%1/3
A and B
A= -t $in_fhB= $has_readkey
ABresult
0X0
100
111
1227 97 return _generate_buffered_reader_from($in_fh, $outputter_ref, $opt_ref);
1228 }
1229  
1230 # Adapt to local control characters...
1231 0 my %ctrl = eval { Term::ReadKey::GetControlChars($in_fh) };
1232 0 delete $ctrl{$_} for grep { $ctrl{$_} eq "\cA" } keys %ctrl;
1233  
1234 0 ▶ $ctrl{EOF} //= "\4";
Condition0%0/2
$ctrl{"EOF"} //= "\4"
l!l
00
MC/DC0%0/1
$ctrl{"EOF"} //= "\4"
$ctrl{"EOF"}
Truth table0%0/2
A
A= $ctrl{"EOF"}
Aresult
11
00
1235 0 ▶ $ctrl{INTERRUPT} //= "\3";
Condition0%0/2
$ctrl{"INTERRUPT"} //= "\3"
l!l
00
MC/DC0%0/1
$ctrl{"INTERRUPT"} //= "\3"
$ctrl{"INTERRUPT"}
Truth table0%0/2
A
A= $ctrl{"INTERRUPT"}
Aresult
11
00
1236 0 ▶ $ctrl{ERASE} //= $^O eq 'MSWin32' ? "\10" : "0177";
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 0 0
Condition0%0/3
$ctrl{"ERASE"} //= $^O eq "MSWin32" ? "\b" : "0177"
l!l&&r!l&&!r
000
MC/DC0%0/2
$ctrl{"ERASE"} //= $^O eq "MSWin32" ? "\b" : "0177"
$ctrl{"ERASE"} $^O eq "MSWin32" ? "\b" : "0177"
Truth table0%0/3
A //= B
A= $ctrl{"ERASE"}B= $^O eq "MSWin32" ? "\b" : "0177"
ABresult
1X1
011
000
1237  
1238 0 my $ctrl = join '|', values %ctrl;
1239  
1240 0 ▶ my $VERBATIM_KEY = $ctrl{QUOTENEXT} // $DEFAULT_VERBATIM_KEY;
Condition0%0/3
$ctrl{"QUOTENEXT"} // $DEFAULT_VERBATIM_KEY
l!l&&r!l&&!r
000
MC/DC0%0/2
$ctrl{"QUOTENEXT"} // $DEFAULT_VERBATIM_KEY
$ctrl{"QUOTENEXT"} $DEFAULT_VERBATIM_KEY
Truth table0%0/3
A // B
A= $ctrl{"QUOTENEXT"}B= $DEFAULT_VERBATIM_KEY
ABresult
1X1
011
000
1241  
1242 # Translate timeout for ReadKey (with 32-bit MAXINT workaround for Windows)...
1243 my $timeout = !defined $opt_ref->{-timeout} ? 0x7FFFFFFF # 68 years
1244 : $opt_ref->{-timeout} == 0 ? -1
1245 : $opt_ref->{-timeout}
1246 0 ▶ ;
Branch: 2 branches
BranchTrueFalseTotal
!defined($opt_ref->{'-timeout'}) ? : 0 0 0
$opt_ref->{'-timeout'} == 0 ? : 0 0 0
1247  
1248 return sub {
1249 0 ▶ my ($extra_constraints) = @_;
Subroutine __ANON__: not called CC=89 SCAR=89.7
1250  
1251 # Short-circuit on unreadable filehandle...
1252 0 ▶ return if !openhandle($in_fh);
Branch: 1 branch
BranchTrueFalseTotal
unless &openhandle($in_fh) 0 0 0
1253  
1254 # Set up direct reading, and prepare to clean up on abnormal exit...
1255 0 Term::ReadKey::ReadMode('raw', $in_fh);
1256 0 my $prev_SIGINT = $SIG{INT};
1257 0 ▶ local $SIG{INT} = sub { return if $prev_SIGINT eq 'IGNORE';
Subroutine __ANON__: not called CC=4 SCAR=25.0
Branch: 1 branch
BranchTrueFalseTotal
if $prev_SIGINT eq "IGNORE" 0 0 0
1258 0 Term::ReadKey::ReadMode('restore', $in_fh);
1259 0 ▶ exit(1) if !defined $prev_SIGINT
Branch: 1 branch
BranchTrueFalseTotal
if not defined $prev_SIGINT or $prev_SIGINT eq "DEFAULT" 0 0 0
Condition0%0/3
not defined $prev_SIGINT or $prev_SIGINT eq "DEFAULT"
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $prev_SIGINT or $prev_SIGINT eq "DEFAULT"
not defined $prev_SIGINT $prev_SIGINT eq "DEFAULT"
Truth table0%0/3
not(A) or B
A= not defined $prev_SIGINTB= $prev_SIGINT eq "DEFAULT"
ABresult
1X1
011
000
1260 || $prev_SIGINT eq 'DEFAULT';
1261 {
1262 0 package main;
1263 37 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1264 0 $prev_SIGINT->()
1265 }
1266 0 };
1267  
1268 # Set up local faked input, if any...
1269 0 my $local_fake_input;
1270 my $orig_fake_input;
1271 0 ▶ if (defined $fake_input && length($fake_input) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fake_input and length $fake_input > 0) 0 0 0
Condition0%0/3
defined $fake_input and length $fake_input > 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $fake_input and length $fake_input > 0
defined $fake_input length $fake_input > 0
Truth table0%0/3
A and B
A= defined $fake_inputB= length $fake_input > 0
ABresult
0X0
100
111
1272 0 $fake_input =~ s{ \A (.*) \R? }{}xm;
1273 0 $orig_fake_input = $local_fake_input = $1;
1274 }
1275  
1276 0 ▶ my $input = exists $opt_ref->{-prefill} ? $opt_ref->{-prefill} : q{};
Branch: 1 branch
BranchTrueFalseTotal
exists $opt_ref->{'-prefill'} ? : 0 0 0
1277 0 ▶ if (exists $opt_ref->{-prefill}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt_ref->{"-prefill"}) 0 0 0
1278 0 ▶ if (exists $opt_ref->{-monitor}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt_ref->{"-monitor"}) 0 0 0
1279 my %opts = ( -cursor_pos => length($input),
1280 -prompt => $opt_ref->{-prompt},
1281 -style => $opt_ref->{-style}->(),
1282 0 -echostyle => $opt_ref->{-echostyle}->(),
1283 );
1284 0 my $input_copy = $input;
1285 0 eval { $opt_ref->{-monitor}->($input_copy, \%opts) };
1286 }
1287 0 $outputter_ref->( -style => $opt_ref->{-style}, _wipe_line(), $opt_ref->{-prompt});
1288 0 $outputter_ref->( -echostyle => join(q{}, map { $opt_ref->{-echo}($_) } $input =~ m/\X/g) );
1289 }
1290  
1291 0 my $insert_offset = 0;
1292 INPUT:
1293 0 while (1) {
1294 0 state $prev_was_verbatim = 0;
1295 0 state $completion_level = 0;
1296 0 state $completion_type = q{};
1297  
1298 # Get next character entered...
1299 0 my $next = Term::ReadKey::ReadKey($timeout, $in_fh);
1300  
1301 # Check for cancellation...
1302 0 ▶ if (exists $opt_ref->{-cancel} && match($next, $opt_ref->{-cancel})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt_ref->{"-cancel"} and match($next, $opt_ref->{"-cancel"})) 0 0 0
Condition0%0/3
exists $opt_ref->{"-cancel"} and match($next, $opt_ref->{"-cancel"})
!ll&&!rl&&r
000
MC/DC0%0/2
exists $opt_ref->{"-cancel"} and match($next, $opt_ref->{"-cancel"})
exists $opt_ref->{"-cancel"} match($next, $opt_ref->{"-cancel"})
Truth table0%0/3
A and B
A= exists $opt_ref->{"-cancel"}B= match($next, $opt_ref->{"-cancel"})
ABresult
0X0
100
111
1303 0 Term::ReadKey::ReadMode('restore', $in_fh);
1304 0 die bless \$input, 'IO::Prompter::Cancellation';
1305 }
1306  
1307 # Finished with completion mode?
1308 0 ▶ if (($next//q{}) !~ m{ $COMPLETE_INIT | $COMPLETE_CYCLE }xms) {
Branch: 1 branch
BranchTrueFalseTotal
unless (($next // "") =~ / $COMPLETE_INIT | $COMPLETE_CYCLE /msx) 0 0 0
Condition0%0/2
$next // ""
l!l
00
MC/DC0%0/1
$next // ""
$next
Truth table0%0/2
A
A= $next
Aresult
11
00
1309 0 $completion_level = 0;
1310 0 $completion_type = q{};
1311 }
1312  
1313 # Are we faking input?
1314 0 my $faking = defined $local_fake_input;
1315  
1316 # If not EOF...
1317 0 ▶ if (defined $next) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $next) 0 0 0
1318 # Remember where we were parked...
1319 0 my $prev_insert_offset = $insert_offset;
1320  
1321 # Handle interrupts...
1322 0 ▶ if ($next eq $ctrl{INTERRUPT}) {
Branch: 8 branches
BranchTrueFalseTotal
if ($next eq $ctrl{'INTERRUPT'}) { } 0 0 0
elsif (not $prev_was_verbatim and $next eq $VERBATIM_KEY) { } 0 0 0
elsif (not $prev_was_verbatim and $next =~ /$COMPLETE_INIT/ || $completion_level > 0 && $next =~ /$COMPLETE_CYCLE/) { } 0 0 0
elsif (not $prev_was_verbatim and $next eq $ctrl{'ERASE'}) { } 0 0 0
elsif (not $prev_was_verbatim and $next eq $ctrl{'EOF'}) { } 0 0 0
elsif (not $prev_was_verbatim and $faking and $next eq $FAKE_ESC) { } 0 0 0
elsif (not $prev_was_verbatim and $next =~ /\A\R\z/) { } 0 0 0
elsif ($prev_was_verbatim or not $next =~ /$ctrl/) { } 0 0 0
1323 0 $SIG{INT}();
1324 0 next INPUT;
1325 }
1326  
1327 # Handle verbatim quoter...
1328 ▶ elsif (!$prev_was_verbatim && $next eq $VERBATIM_KEY) {
Condition0%0/3
not $prev_was_verbatim and $next eq $VERBATIM_KEY
!ll&&!rl&&r
000
MC/DC0%0/2
not $prev_was_verbatim and $next eq $VERBATIM_KEY
not $prev_was_verbatim $next eq $VERBATIM_KEY
Truth table0%0/3
not(A) and B
A= not $prev_was_verbatimB= $next eq $VERBATIM_KEY
ABresult
0X0
100
111
1329 0 $prev_was_verbatim = 1;
1330 0 next INPUT;
1331 }
1332  
1333 # Handle completions...
1334 ▶ elsif (!$prev_was_verbatim
Condition0%0/9
not $prev_was_verbatim and $next =~ /$COMPLETE_INIT/ || $completion_level > 0 && $next =~ /$COMPLETE_CYCLE/
!ll&&!rl&&r
000
$next =~ /$COMPLETE_INIT/ or $completion_level > 0 and $next =~ /$COMPLETE_CYCLE/
l!l&&r!l&&!r
000
$completion_level > 0 and $next =~ /$COMPLETE_CYCLE/
!ll&&!rl&&r
000
MC/DC0%0/4
not $prev_was_verbatim and $next =~ /$COMPLETE_INIT/ || $completion_level > 0 && $next =~ /$COMPLETE_CYCLE/
not $prev_was_verbatim $next =~ /$COMPLETE_INIT/ $completion_level > 0 $next =~ /$COMPLETE_CYCLE/
Truth table0%0/5
not(A) and B or C and D
A= not $prev_was_verbatimB= $next =~ /$COMPLETE_INIT/C= $completion_level > 0D= $next =~ /$COMPLETE_CYCLE/
ABCDresult
0XXX0
100X0
10100
11XX1
10111
1335 && ( $next =~ $COMPLETE_INIT
1336 || $completion_level > 0 && $next =~ $COMPLETE_CYCLE
1337 )
1338 ) {
1339 0 state @completion_list; # ...all candidates for completion
1340 0 state @completion_ring; # ..."next" candidate cycle
1341 0 state $completion_ring_first; # ...special case first time
1342 0 state $completion_prefix; # ...skipped before completing
1343  
1344 # Track completion type and level (switch if necessary)...
1345 0 ▶ if ($next =~ $COMPLETE_INIT && index($completion_type, $next) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next =~ /$COMPLETE_INIT/ and index($completion_type, $next) == -1) { } 0 0 0
Condition0%0/3
$next =~ /$COMPLETE_INIT/ and index($completion_type, $next) == -1
!ll&&!rl&&r
000
MC/DC0%0/2
$next =~ /$COMPLETE_INIT/ and index($completion_type, $next) == -1
$next =~ /$COMPLETE_INIT/ index($completion_type, $next) == -1
Truth table0%0/3
A and B
A= $next =~ /$COMPLETE_INIT/B= index($completion_type, $next) == -1
ABresult
0X0
100
111
1346 0 ▶ $completion_type = index($COMPLETE_KEY, $next) >= 0 ? $COMPLETE_KEY : $COMPLETE_HIST;
Branch: 1 branch
BranchTrueFalseTotal
index($COMPLETE_KEY, $next) != -1 ? : 0 0 0
1347 0 $completion_level = 1;
1348 }
1349 else {
1350 0 $completion_level++;
1351 }
1352  
1353 # If starting completion, cache completions...
1354 0 ▶ if ($completion_level == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($completion_level == 1) 0 0 0
1355 0 ▶ ($completion_prefix, @completion_list)
Branch: 1 branch
BranchTrueFalseTotal
index($COMPLETE_KEY, $next) != -1 ? : 0 0 0
1356 = index($COMPLETE_KEY, $next) >= 0
1357 ? _current_completions_for($input, $opt_ref)
1358 : _current_history_for($input, $opt_ref);
1359 0 @completion_ring = (@completion_list, q{});
1360 0 $completion_ring_first = 1;
1361 }
1362  
1363 # Can only complete if there are completions to be had...
1364 0 ▶ if (@completion_list) {
Branch: 1 branch
BranchTrueFalseTotal
if (@completion_list) 0 0 0
1365 # Select the appropriate mode...
1366 my $mode = $COMPLETE_MODE{$completion_type}[$completion_level-1]
1367 0 ▶ // $COMPLETE_MODE{$completion_type}[-1];
Condition0%0/3
$COMPLETE_MODE{$completion_type}[$completion_level - 1] // $COMPLETE_MODE{$completion_type}[-1]
l!l&&r!l&&!r
000
MC/DC0%0/2
$COMPLETE_MODE{$completion_type}[$completion_level - 1] // $COMPLETE_MODE{$completion_type}[-1]
$COMPLETE_MODE{$completion_type}[$completion_level - 1] $COMPLETE_MODE{$completion_type}[-1]
Truth table0%0/3
A // B
A= $COMPLETE_MODE{$completion_type}[$completion_level - 1]B= $COMPLETE_MODE{$completion_type}[-1]
ABresult
1X1
011
000
1368  
1369 # 'longest mode' finds longest consistent prefix...
1370 0 ▶ if ($mode =~ /longest/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mode =~ /longest/) { } 0 0 0
elsif ($mode =~ /full/) { } 0 0 0
1371 0 $input
1372 = $completion_prefix
1373 . _longest_common_prefix_for(@completion_list);
1374 }
1375 # 'full mode' suggests next full match...
1376 elsif ($mode =~ /full/) {
1377 0 ▶ if (!$completion_ring_first) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($completion_ring_first) 0 0 0
1378 0 ▶ if ($next eq $COMPLETE_PREV) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next eq $COMPLETE_PREV) { } 0 0 0
1379 0 unshift @completion_ring,
1380 pop @completion_ring;
1381 }
1382 else {
1383 0 push @completion_ring,
1384 shift @completion_ring;
1385 }
1386 }
1387 0 $input = $completion_prefix . $completion_ring[0];
1388 0 $completion_ring_first = 0;
1389 }
1390 # 'list mode' lists all possibilities...
1391 0 ▶ my $list_display = $mode =~ /list/
Branch: 1 branch
BranchTrueFalseTotal
$mode =~ /list/ ? : 0 0 0
1392 ? _display_completions($input, @completion_list)
1393 : q{};
1394  
1395 # Update prompt with selected completion...
1396 $outputter_ref->( -style =>
1397 $list_display,
1398 _wipe_line(),
1399 0 $opt_ref->{-prompt}, $input
1400 );
1401  
1402 # If last completion was unique choice, completed...
1403 0 ▶ if (@completion_list <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@completion_list <= 1) 0 0 0
1404 0 $completion_level = 0;
1405 }
1406 }
1407 0 next INPUT;
1408 }
1409  
1410 # Handle erasures (including pushbacks if faking)...
1411 ▶ elsif (!$prev_was_verbatim && $next eq $ctrl{ERASE}) {
Condition0%0/3
not $prev_was_verbatim and $next eq $ctrl{"ERASE"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $prev_was_verbatim and $next eq $ctrl{"ERASE"}
not $prev_was_verbatim $next eq $ctrl{"ERASE"}
Truth table0%0/3
not(A) and B
A= not $prev_was_verbatimB= $next eq $ctrl{"ERASE"}
ABresult
0X0
100
111
1412 0 ▶ if (!length $input) {
Branch: 2 branches
BranchTrueFalseTotal
if (not length $input) { } 0 0 0
elsif ($insert_offset) { } 0 0 0
1413 # Do nothing...
1414 }
1415 elsif ($insert_offset) {
1416 # Can't erase past start of input...
1417 0 ▶ next INPUT if $insert_offset >= length($input);
Branch: 1 branch
BranchTrueFalseTotal
if $insert_offset >= length $input 0 0 0
1418  
1419 # Erase character just before cursor...
1420 0 substr($input, -$insert_offset-1, 1, q{});
1421  
1422 # Redraw...
1423 0 my $input_pre = substr($input.' ',0,length($input)-$insert_offset+1);
1424 0 my $input_post = substr($input.' ',length($input)-$insert_offset);
1425 my $display_pre
1426 0 = join q{}, map { $opt_ref->{-echo}($_) } $input_pre =~ m/\X/g;
1427 my $display_post
1428 0 = join q{}, map { $opt_ref->{-echo}($_) } $input_post =~ m/\X/g;
1429 $outputter_ref->( -echostyle =>
1430 "\b" x length($display_pre)
1431 0 . join(q{}, map { $opt_ref->{-echo}($_) } $input =~ m/\X/g)
1432 0 . q{ } x length($opt_ref->{-echo}(q{ }))
1433 . "\b" x length($display_post)
1434 );
1435 }
1436 else {
1437 0 my $erased = substr($input, -1, 1, q{});
1438 0 ▶ if ($faking) {
Branch: 1 branch
BranchTrueFalseTotal
if ($faking) 0 0 0
1439 0 substr($local_fake_input,0,0,$erased);
1440 }
1441 $outputter_ref->( -nostyle =>
1442 0 ▶ map { $_ x (length($opt_ref->{-echo}($_)//'X')) }
Condition0%0/2
$opt_ref->{"-echo"}->($_) // "X"
l!l
00
MC/DC0%0/1
$opt_ref->{"-echo"}->($_) // "X"
$opt_ref->{"-echo"}->($_)
Truth table0%0/2
A
A= $opt_ref->{"-echo"}->($_)
Aresult
11
00
1443 "\b", ' ', "\b"
1444 );
1445 }
1446 0 next INPUT;
1447 }
1448  
1449 # Handle EOF (including cancelling any remaining fake input)...
1450 ▶ elsif (!$prev_was_verbatim && $next eq $ctrl{EOF}) {
Condition0%0/3
not $prev_was_verbatim and $next eq $ctrl{"EOF"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $prev_was_verbatim and $next eq $ctrl{"EOF"}
not $prev_was_verbatim $next eq $ctrl{"EOF"}
Truth table0%0/3
not(A) and B
A= not $prev_was_verbatimB= $next eq $ctrl{"EOF"}
ABresult
0X0
100
111
1451 0 Term::ReadKey::ReadMode('restore', $in_fh);
1452 0 close $in_fh;
1453 0 undef $fake_input;
1454 0 ▶ return length($input) ? $input : undef;
Branch: 1 branch
BranchTrueFalseTotal
length $input ? : 0 0 0
1455 }
1456  
1457 # Handle escape from faking...
1458 ▶ elsif (!$prev_was_verbatim && $faking && $next eq $FAKE_ESC) {
Condition0%0/6
not $prev_was_verbatim and $faking and $next eq $FAKE_ESC
!ll&&!rl&&r
000
not $prev_was_verbatim and $faking
!ll&&!rl&&r
000
MC/DC0%0/3
not $prev_was_verbatim and $faking and $next eq $FAKE_ESC
not $prev_was_verbatim $faking $next eq $FAKE_ESC
Truth table0%0/4
not(A) and B and C
A= not $prev_was_verbatimB= $fakingC= $next eq $FAKE_ESC
ABCresult
0XX0
10X0
1100
1111
1459 0 my $lookahead = Term::ReadKey::ReadKey(0, $in_fh);
1460  
1461 # Two <ESC> implies the current faked line is deferred...
1462 0 ▶ if ($lookahead eq $FAKE_ESC) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lookahead eq $FAKE_ESC) { } 0 0 0
1463 0 $fake_input =~ s{ \A }{$orig_fake_input\n}xm;
1464 }
1465 # Only one <ESC> implies the current faked line is replaced...
1466 else {
1467 0 $in_fh->ungetc(ord($lookahead));
1468 }
1469 0 undef $local_fake_input;
1470 0 $faking = 0;
1471 0 next INPUT;
1472 }
1473  
1474 # Handle returns...
1475 ▶ elsif (!$prev_was_verbatim && $next =~ /\A\R\z/) {
Condition0%0/3
not $prev_was_verbatim and $next =~ /\A\R\z/
!ll&&!rl&&r
000
MC/DC0%0/2
not $prev_was_verbatim and $next =~ /\A\R\z/
not $prev_was_verbatim $next =~ /\A\R\z/
Truth table0%0/3
not(A) and B
A= not $prev_was_verbatimB= $next =~ /\A\R\z/
ABresult
0X0
100
111
1476 # Complete faked line, if faked input incomplete...
1477 0 ▶ if ($faking && length($local_fake_input)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($faking and length $local_fake_input) 0 0 0
Condition0%0/3
$faking and length $local_fake_input
!ll&&!rl&&r
000
MC/DC0%0/2
$faking and length $local_fake_input
$faking length $local_fake_input
Truth table0%0/3
A and B
A= $fakingB= length $local_fake_input
ABresult
0X0
100
111
1478 0 for ($local_fake_input =~ m/\X/g) {
1479 0 _simulate_typing();
1480 0 $outputter_ref->(-echostyle => $opt_ref->{-echo}($_));
1481 }
1482 0 $input .= $local_fake_input;
1483 }
1484  
1485 # Add newline to the accumulated input string...
1486 0 $input .= $next;
1487  
1488 # Check that input satisfied any constraints...
1489 0 _verify_input_constraints(
1490 \$input, \$local_fake_input, $outputter_ref,
1491 $opt_ref, $extra_constraints,
1492 );
1493  
1494 # Echo a default value if appropriate...
1495 0 ▶ if ($input =~ m{\A\R?\Z}xms && defined $opt_ref->{-def}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input =~ /\A\R?\Z/msx and defined $opt_ref->{"-def"}) 0 0 0
Condition0%0/3
$input =~ /\A\R?\Z/msx and defined $opt_ref->{"-def"}
!ll&&!rl&&r
000
MC/DC0%0/2
$input =~ /\A\R?\Z/msx and defined $opt_ref->{"-def"}
$input =~ /\A\R?\Z/msx defined $opt_ref->{"-def"}
Truth table0%0/3
A and B
A= $input =~ /\A\R?\Z/msxB= defined $opt_ref->{"-def"}
ABresult
0X0
100
111
1496 0 my $def_val = $opt_ref->{-def};
1497  
1498 # Try to find the key, for a menu...
1499 0 ▶ if (exists $opt_ref->{-menu_curr_level}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt_ref->{"-menu_curr_level"}) 0 0 0
1500 0 for my $key ( keys %{$opt_ref->{-menu_curr_level}}) {
1501 0 ▶ if (match($def_val, $opt_ref->{-menu_curr_level}{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (match($def_val, $opt_ref->{"-menu_curr_level"}{$key})) 0 0 0
1502 0 $def_val = $key;
1503 0 last;
1504 }
1505 }
1506 }
1507  
1508 # Echo it as if it had been typed...
1509 0 $outputter_ref->(-echostyle => $opt_ref->{-echo}($def_val));
1510 }
1511  
1512 # Echo the return (or otherwise, as specified)...
1513 0 $outputter_ref->(-echostyle => $opt_ref->{-return}($next));
1514  
1515 # Clean up, and return the input...
1516 0 Term::ReadKey::ReadMode('restore', $in_fh);
1517  
1518 # Handle fake EOF...
1519 0 ▶ if ($faking && $input =~ m{^ (?: \cD | \cZ) }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($faking and $input =~ /^ (?: \cD | \cZ) /msx) 0 0 0
Condition0%0/3
$faking and $input =~ /^ (?: \cD | \cZ) /msx
!ll&&!rl&&r
000
MC/DC0%0/2
$faking and $input =~ /^ (?: \cD | \cZ) /msx
$faking $input =~ /^ (?: \cD | \cZ) /msx
Truth table0%0/3
A and B
A= $fakingB= $input =~ /^ (?: \cD | \cZ) /msx
ABresult
0X0
100
111
1520 0 return undef;
1521 }
1522  
1523 0 return $input;
1524 }
1525  
1526 # Handle anything else...
1527 ▶ elsif ($prev_was_verbatim || $next !~ /$ctrl/) {
Condition0%0/3
$prev_was_verbatim or not $next =~ /$ctrl/
l!l&&r!l&&!r
000
MC/DC0%0/2
$prev_was_verbatim or not $next =~ /$ctrl/
$prev_was_verbatim not $next =~ /$ctrl/
Truth table0%0/3
A or not(B)
A= $prev_was_verbatimB= not $next =~ /$ctrl/
ABresult
1X1
011
000
1528 # If so, get the next fake character...
1529 0 ▶ if ($faking) {
Branch: 1 branch
BranchTrueFalseTotal
if ($faking) 0 0 0
1530 0 ▶ $next = length($local_fake_input)
Branch: 1 branch
BranchTrueFalseTotal
length $local_fake_input ? : 0 0 0
1531 ? substr($local_fake_input,0,1,q{})
1532 : q{};
1533 }
1534  
1535 # Handle editing...
1536 0 ▶ if ($next eq $EDIT{BACK}) {
Branch: 4 branches
BranchTrueFalseTotal
if ($next eq $EDIT{'BACK'}) { } 0 0 0
elsif ($next eq $EDIT{'FORWARD'}) { } 0 0 0
elsif ($next eq $EDIT{'START'}) { } 0 0 0
elsif ($next eq $EDIT{'END'}) { } 0 0 0
1537 0 ▶ $insert_offset += ($insert_offset < length $input) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$insert_offset < length $input ? : 0 0 0
1538 }
1539 elsif ($next eq $EDIT{FORWARD}) {
1540 0 ▶ $insert_offset += ($insert_offset > 0) ? -1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$insert_offset > 0 ? : 0 0 0
1541 }
1542 elsif ($next eq $EDIT{START}) {
1543 0 $insert_offset = length($input);
1544 }
1545 elsif ($next eq $EDIT{END}) {
1546 0 $insert_offset = 0;
1547 }
1548  
1549 # Handle non-editing...
1550 else {
1551 # Check for input restrictions...
1552 0 ▶ if (exists $opt_ref->{-guarantee}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt_ref->{"-guarantee"}) 0 0 0
1553 0 ▶ next INPUT if ($input.$next) !~ $opt_ref->{-guarantee};
Branch: 1 branch
BranchTrueFalseTotal
unless ($input . $next) =~ /$opt_ref->{"-guarantee"}/ 0 0 0
1554 }
1555  
1556 # Add the new input char to the accumulated input string...
1557 0 ▶ if ($insert_offset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($insert_offset) { } 0 0 0
1558 0 substr($input, -$insert_offset, 0) = $next;
1559 0 $prev_insert_offset++;
1560 }
1561 else {
1562 0 $input .= $next;
1563 }
1564 }
1565  
1566 # Display the character (or whatever was specified)...
1567  
1568 0 ▶ if ($insert_offset || $prev_insert_offset) {
Branch: 2 branches
BranchTrueFalseTotal
if ($insert_offset or $prev_insert_offset) { } 0 0 0
elsif (not $next =~ /$EDIT_KEY/) { } 0 0 0
Condition0%0/3
$insert_offset or $prev_insert_offset
l!l&&r!l&&!r
000
MC/DC0%0/2
$insert_offset or $prev_insert_offset
$insert_offset $prev_insert_offset
Truth table0%0/3
A or B
A= $insert_offsetB= $prev_insert_offset
ABresult
1X1
011
000
1569 0 my $input_pre = substr($input,0,length($input)-$prev_insert_offset);
1570 0 my $input_post = substr($input,length($input)-$insert_offset);
1571 my $display_pre
1572 0 = join q{}, map { $opt_ref->{-echo}($_) } $input_pre =~ m/\X/g;
1573 my $display_post
1574 0 = join q{}, map { $opt_ref->{-echo}($_) } $input_post =~ m/\X/g;
1575 $outputter_ref->( -echostyle =>
1576 "\b" x length($display_pre)
1577 0 . join(q{}, map { $opt_ref->{-echo}($_) } $input =~ m/\X/g)
1578 . "\b" x length($display_post)
1579 );
1580 }
1581 elsif ($next !~ $EDIT_KEY) {
1582 0 $outputter_ref->(-echostyle => $opt_ref->{-echo}($next));
1583 }
1584  
1585 # Not verbatim after this...
1586 0 $prev_was_verbatim = 0;
1587 }
1588 else {
1589 # Not verbatim after mysterious ctrl input...
1590 0 $prev_was_verbatim = 0;
1591  
1592 0 next INPUT;
1593 }
1594 }
1595  
1596 0 ▶ if ($opt_ref->{-single} || !defined $next || $input =~ m{\Q$/\E$}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_ref->{"-single"} or not defined $next or $input =~ m[\Q$/\E$]) 0 0 0
Condition0%0/6
$opt_ref->{"-single"} or not defined $next or $input =~ m[\Q$/\E$]
l!l&&r!l&&!r
000
$opt_ref->{"-single"} or not defined $next
l!l&&r!l&&!r
000
MC/DC0%0/3
$opt_ref->{"-single"} or not defined $next or $input =~ m[\Q$/\E$]
$opt_ref->{"-single"} not defined $next $input =~ m[\Q$/\E$]
Truth table0%0/4
A or not(B) or C
A= $opt_ref->{"-single"}B= not defined $nextC= $input =~ m[\Q$/\E$]
ABCresult
1XX1
01X1
0011
0000
1597 # Did we get an acceptable value?
1598 0 ▶ if (defined $next) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $next) 0 0 0
1599 0 _verify_input_constraints(
1600 \$input, \$local_fake_input, $outputter_ref,
1601 $opt_ref, $extra_constraints,
1602 );
1603 }
1604  
1605 # Reset terminal...
1606 0 Term::ReadKey::ReadMode('restore', $in_fh);
1607  
1608 # Return failure if failed before input or cancelled...
1609 0 ▶ if (!defined $next && length($input) == 0
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $next and length $input == 0 or exists $opt_ref->{"-cancel"} and match($next, $opt_ref->{"-cancel"})) 0 0 0
Condition0%0/9
not defined $next and length $input == 0 or exists $opt_ref->{"-cancel"} and match($next, $opt_ref->{"-cancel"})
l!l&&r!l&&!r
000
not defined $next and length $input == 0
!ll&&!rl&&r
000
exists $opt_ref->{"-cancel"} and match($next, $opt_ref->{"-cancel"})
!ll&&!rl&&r
000
MC/DC0%0/4
not defined $next and length $input == 0 or exists $opt_ref->{"-cancel"} and match($next, $opt_ref->{"-cancel"})
not defined $next length $input == 0 exists $opt_ref->{"-cancel"} match($next, $opt_ref->{"-cancel"})
Truth table0%0/7
not(A) and B or C and D
A= not defined $nextB= length $input == 0C= exists $opt_ref->{"-cancel"}D= match($next, $opt_ref->{"-cancel"})
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
1610 || exists $opt_ref->{-cancel} && match($next, $opt_ref->{-cancel})) {
1611 0 ▶ return if $opt_ref->{-verbatim};
Branch: 1 branch
BranchTrueFalseTotal
if $opt_ref->{"-verbatim"} 0 0 0
1612 0 ▶ return PUREBOOL { 0 }
Subroutine __ANON__: not called CC=1 SCAR=6.9
1613 0 ▶ BOOL { 0 }
Subroutine __ANON__: not called CC=1 SCAR=6.9
1614 0 ▶ SCALAR { undef }
Subroutine __ANON__: not called CC=1 SCAR=6.9
1615 0 ▶ METHOD { defaulted => sub { 0 }, timedout => sub { 0 } };
Subroutine __ANON__: not called CC=1 SCAR=6.9
1616 }
1617  
1618 # Otherwise supply a final newline if necessary...
1619 0 ▶ if ( $opt_ref->{-single}
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_ref->{"-single"} and exists $opt_ref->{"-return"} and not $input =~ /\A\R\z/) 0 0 0
Condition0%0/6
$opt_ref->{"-single"} and exists $opt_ref->{"-return"} and not $input =~ /\A\R\z/
!ll&&!rl&&r
000
$opt_ref->{"-single"} and exists $opt_ref->{"-return"}
!ll&&!rl&&r
000
MC/DC0%0/3
$opt_ref->{"-single"} and exists $opt_ref->{"-return"} and not $input =~ /\A\R\z/
$opt_ref->{"-single"} exists $opt_ref->{"-return"} not $input =~ /\A\R\z/
Truth table0%0/4
A and B and not(C)
A= $opt_ref->{"-single"}B= exists $opt_ref->{"-return"}C= not $input =~ /\A\R\z/
ABCresult
0XX0
10X0
1100
1111
1620 && exists $opt_ref->{-return}
1621 && $input !~ /\A\R\z/ ) {
1622 0 $outputter_ref->(-echostyle => $opt_ref->{-return}(q{}));
1623 }
1624  
1625 0 return $input;
1626 }
1627 }
1628 continue {
1629 # Perform monitor (if any) and redraw prompt (if required)...
1630 0 ▶ if ($opt_ref->{-monitor}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_ref->{"-monitor"}) 0 0 0
1631 my %opts = ( -cursor_pos => length($input) - $insert_offset,
1632 -prompt => $opt_ref->{-prompt},
1633 -style => $opt_ref->{-style}->(),
1634 0 -echostyle => $opt_ref->{-echostyle}->(),
1635 );
1636 0 my $input_copy = $input;
1637 0 my $output_pos = $outputter_ref->(-tell);
1638 0 ▶ if (!defined eval { $opt_ref->{-monitor}->($input_copy, \%opts) }
Branch: 1 branch
BranchTrueFalseTotal
if (not defined eval { do { $opt_ref->{"-monitor"}->($input_copy, \%opts) } } or $output_pos != &$outputter_ref(-(tell))) 0 0 0
Condition0%0/3
not defined eval { do { $opt_ref->{"-monitor"}->($input_copy, \%opts) } } or $output_pos != &$outputter_ref(-(tell))
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined eval { do { $opt_ref->{"-monitor"}->($input_copy, \%opts) } } or $output_pos != &$outputter_ref(-(tell))
not defined eval { do { $opt_ref->{"-monitor"}->($input_copy, \%opts) } } $output_pos != &$outputter_ref(-(tell))
Truth table0%0/3
not(A) or B
A= not defined eval { do { $opt_ref->{"-monitor"}->($input_copy, \%opts) } }B= $output_pos != &$outputter_ref(-(tell))
ABresult
1X1
011
000
1639 || $output_pos != $outputter_ref->(-tell)) {
1640 0 my $input_pre = substr($input.' ',0,length($input)-$insert_offset+1);
1641 0 my $input_post = substr($input.' ',length($input)-$insert_offset);
1642 my $display_pre
1643 0 = join q{}, map { $opt_ref->{-echo}($_) } $input_pre =~ m/\X/g;
1644 my $display_post
1645 0 = join q{}, map { $opt_ref->{-echo}($_) } $input_post =~ m/\X/g;
1646 0 $outputter_ref->( -style => $opt_ref->{-style}, _wipe_line(), $opt_ref->{-prompt});
1647 $outputter_ref->( -echostyle =>
1648 0 join(q{}, map { $opt_ref->{-echo}($_) } $input =~ m/\X/g)
1649 . "\b" x (length($display_post)-1)
1650 );
1651 }
1652 }
1653 }
1654 }
1655 0 }
1656  
1657 # Build a menu...
1658 sub _build_menu {
1659 1 ▶ my ($source_ref, $initial_prompt, $is_numeric) = @_;
Subroutine _build_menu: called CC=15 SCAR=36.3
1660 1 ▶ my $prompt = ($initial_prompt//q{}) . qq{\n};
Condition50%1/2
$initial_prompt // ""
l!l
01
MC/DC0%0/1
$initial_prompt // ""
$initial_prompt
Truth table50%1/2
A
A= $initial_prompt
Aresult
11
00
1661 1 my $final = q{};
1662 1 my %value_for;
1663 my %key_for;
1664 1 my @selectors;
1665  
1666 1 for my $source_type (ref $source_ref) {
1667 1 ▶ if ($source_type eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if ($source_type eq 'HASH') { } 0 1 1
elsif ($source_type eq 'SCALAR') { } 0 1 1
elsif ($source_type eq 'ARRAY') { } 1 0 1
1668 0 my @sorted_keys = sort(keys(%{$source_ref}));
1669 0 ▶ @selectors = $is_numeric ? (1..@sorted_keys) : ('a'..'z','A'..'Z');
Branch: 1 branch
BranchTrueFalseTotal
$is_numeric ? : 0 0 0
1670 0 @key_for{@selectors} = @sorted_keys;
1671 0 @value_for{@selectors} = @{$source_ref}{@sorted_keys};
1672 0 $source_ref = \@sorted_keys;
1673 0 $_ = 'ARRAY';
1674 0 continue;
1675 }
1676 elsif ($source_type eq 'SCALAR') {
1677 0 $source_ref = [ split "\n", ${$source_ref} ];
1678 0 $_ = 'ARRAY';
1679 0 continue;
1680 }
1681 elsif ($source_type eq 'ARRAY') {
1682 1 my @source = @{$source_ref};
1683 1 ▶ @selectors = $is_numeric ? (1..@source) : ('a'..'z','A'..'Z');
Branch: 1 branch
BranchTrueFalseTotal
$is_numeric ? : 1 0 1
1684 1 ▶ if (!keys %value_for) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %value_for) 1 0 1
1685 1 @value_for{@selectors} = @source;
1686 }
1687 ITEM:
1688 1 for my $tag (@selectors) {
1689 10 ▶ my $item = shift(@source) // last ITEM;
Condition50%1/2
shift @source // (last ITEM)
l!l
100
MC/DC0%0/1
shift @source // (last ITEM)
shift @source
Truth table50%1/2
A
A= shift @source
Aresult
11
00
1690 10 chomp $item;
1691 10 $prompt .= sprintf("%4s. $item\n", $tag);
1692 10 $final = $tag;
1693 }
1694 1 ▶ if (@source) {
Branch: 1 branch
BranchTrueFalseTotal
if (@source) 0 1 1
1695 0 _warn( misc =>
1696 "prompt(): Too many menu items. Ignoring the final " . @source
1697 );
1698 }
1699 }
1700 }
1701  
1702 1 ▶ my $constraint = $is_numeric ? '(?:' . join('|',@selectors) .')'
Branch: 2 branches
BranchTrueFalseTotal
$is_numeric ? : 1 0 1
$final =~ /[A-Z]/ ? : 0 0 0
1703 : $final =~ /[A-Z]/ ? "[a-zA-$final]"
1704 : "[a-$final]";
1705 1 ▶ my $constraint_desc = $is_numeric ? "[1-$selectors[-1]]" : $constraint;
Branch: 1 branch
BranchTrueFalseTotal
$is_numeric ? : 1 0 1
1706 1 $constraint = '\A\s*' . $constraint . '\s*\Z';
1707  
1708 return {
1709 1 data => $source_ref,
1710 key_for => \%key_for,
1711 value_for => \%value_for,
1712 prompt => "$prompt\n",
1713 is_numeric => $is_numeric,
1714 constraint => { "Enter $constraint_desc: " => qr/$constraint|$MENU_ESC/ },
1715 };
1716 }
1717  
1718 # Vocabulary that _stylize understands...
1719 37 my %synonyms = (
1720 bold => [qw<boldly strong heavy emphasis emphatic highlight highlighted fort forte>],
1721 dark => [qw<darkly dim deep>],
1722 faint => [qw<faintly light soft>],
1723 underline => [qw<underlined underscore underscored>],
1724 italic => [qw<italics>],
1725 blink => [qw<blinking flicker flickering flash flashing>],
1726 reverse => [qw<reversed inverse inverted>],
1727 concealed => [qw<hidden blank invisible>],
1728 reset => [qw<normal default standard usual ordinary regular>],
1729 bright_ => [qw< bright\s+ vivid\s+ >],
1730 red => [qw< scarlet vermilion crimson ruby cherry cerise cardinal carmine
1731 burgundy claret chestnut copper garnet geranium russet
1732 salmon titian coral cochineal rose cinnamon ginger gules >],
1733 yellow => [qw< gold golden lemon cadmium daffodil mustard primrose tawny
1734 amber aureate canary champagne citrine citron cream goldenrod honey straw >],
1735 green => [qw< olive jade pea emerald lime chartreuse forest sage vert >],
1736 cyan => [qw< aqua aquamarine teal turquoise ultramarine >],
1737 blue => [qw< azure cerulean cobalt indigo navy sapphire >],
1738 magenta => [qw< amaranthine amethyst lavender lilac mauve mulberry orchid periwinkle
1739 plum pomegranate violet purple aubergine cyclamen fuchsia modena puce
1740 purpure >],
1741 black => [qw< charcoal ebon ebony jet obsidian onyx raven sable slate >],
1742 white => [qw< alabaster ash chalk ivory milk pearl silver argent >],
1743 );
1744  
1745 # Back-mapping to standard terms...
1746 my %normalize
1747 37 = map { join('|', map { "$_\\b" } reverse sort @{$synonyms{$_}}) => $_ }
1748 keys %synonyms;
1749  
1750 37 my $BACKGROUND = qr{
1751 (\S+) \s+ (?: behind | beneath | below | under(?:neath)? )\b
1752 | \b (?:upon|over|on) \s+ (?:an?)? \s+ (.*?) \s+ (?:background|bg|field) \b
1753 | \b (?:upon\s+ | over\s+ | (?:(on|upon|over)\s+a\s+)? (?:background|bg|field) \s+ (?:of\s+|in\s+)? | on\s+) (\S+)
1754 }ixms;
1755  
1756 # Convert a description to ANSI colour codes...
1757 sub _stylize {
1758 0 ▶ my $spec = shift // q{};
Subroutine _stylize: not called CC=9 SCAR=45.0
Condition0%0/2
shift() // ""
l!l
00
MC/DC0%0/1
shift() // ""
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1759  
1760 # Handle arrays and hashes as args...
1761 0 ▶ if (ref($spec) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $spec eq 'ARRAY') { } 0 0 0
elsif (ref $spec eq 'HASH') { } 0 0 0
1762 0 $spec = join q{ }, @{$spec};
1763 }
1764 elsif (ref($spec) eq 'HASH') {
1765 0 $spec = join q{ }, keys %{$spec};
1766 }
1767  
1768 # Ignore punctuation...
1769 0 $spec =~ s/[^\w\s]//g;
1770  
1771 # Handle backgrounds...
1772 0 $spec =~ s/$BACKGROUND/on_$+/g;
1773  
1774 # Apply standard translations...
1775 0 for my $pattern (keys %normalize) {
1776 0 ▶ $spec =~ s{\b(on_|\b) $pattern}{($1//q{}).$normalize{$pattern}}geixms;
Condition0%0/2
$1 // ""
l!l
00
MC/DC0%0/1
$1 // ""
$1
Truth table0%0/2
A
A= $1
Aresult
11
00
1777 }
1778  
1779 # Ignore anything unknown...
1780 0 ▶ $spec =~ s{((?:on_)?(?:(ansi\d+|rgb\d+)|(\S+)))}{ $2 || exists $synonyms{$3} ? $1 : q{} }gxmse;
Branch: 1 branch
BranchTrueFalseTotal
$2 || exists $synonyms{$3} ? : 0 0 0
Condition0%0/3
$2 or exists $synonyms{$3}
l!l&&r!l&&!r
000
MC/DC0%0/2
$2 or exists $synonyms{$3}
$2 exists $synonyms{$3}
Truth table0%0/3
A or B
A= $2B= exists $synonyms{$3}
ABresult
1X1
011
000
1781  
1782 # Build ANSI terminal codes around text...
1783 0 my $raw_text = join q{}, @_;
1784 0 my ($prews, $text, $postws) = $raw_text =~ m{\A (\s*) (.*?) (\s*) \Z}xms;
1785 0 my @style = split /\s+/, $spec;
1786 0 ▶ return $prews
Branch: 1 branch
BranchTrueFalseTotal
@style ? : 0 0 0
1787 . ( @style ? Term::ANSIColor::colored(\@style, $text) : $text )
1788 . $postws;
1789 }
1790  
1791 # Build a subroutine that prints printable chars to the specified filehandle...
1792 sub _std_printer_to {
1793 0 ▶ my ($out_filehandle, $opt_ref) = @_;
Subroutine _std_printer_to: not called CC=2 SCAR=15.7
1794 37 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1795 0 _autoflush($out_filehandle);
1796 0 ▶ if (eval { require Term::ANSIColor}) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Term::ANSIColor } }) { } 0 0 0
1797 return sub {
1798 0 ▶ my $style = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
1799 0 ▶ return tell($out_filehandle) if $style eq -tell;
Branch: 1 branch
BranchTrueFalseTotal
if $style eq -(tell) 0 0 0
1800 0 my @loc = (@_);
1801 0 s{\e}{^}gxms for @loc;
1802 0 print {$out_filehandle} _stylize($opt_ref->{$style}(@loc), @loc);
1803 0 };
1804 }
1805 else {
1806 return sub {
1807 0 ▶ my $style = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
1808 0 ▶ return tell($out_filehandle) if $style eq -tell;
Branch: 1 branch
BranchTrueFalseTotal
if $style eq -(tell) 0 0 0
1809 0 my @loc = (@_);
1810 0 s{\e}{^}gxms for @loc;
1811 0 print {$out_filehandle} @loc;
1812 0 };
1813 }
1814 }
1815  
1816 # Build a subroutine that prints to nowhere...
1817 sub _null_printer {
1818 97 ▶ return sub {};
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine _null_printer: called CC=1 SCAR=0.0
1819 }
1820  
1821 37 1; # Magic true value required at end of module
1822 __END__
↑ Summary