blib/lib/IO/Prompt.pm

filter: Statementstmt 16.9% 72 / 424 Branchbran 1.4% 4 / 274 Conditioncond 0.0% 0 / 143 MC/DCmcdc 0.0% 0 / 83 Subroutinesub 38.4% 15 / 39 Podpod 100% 3 / 3 totaltotal 9.7% 94 / 966 CC 195 SCAR 61.0
CC
195
Cov
9%
_get_prompt
1056.0
get_input
930.0
_menu
756.0
CRAP 447.9
↑ Summary
1 package IO::Prompt;
2  
3 2 our $VERSION = '0.997004';
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶no warnings 'utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 our @EXPORT = qw( prompt );
12 2 our @EXPORT_OK = qw( hand_print get_input );
13  
14 2 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Term::ReadKey;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 0 ▶sub _isprint { $_[0] =~ qr/ ^ [[:print:]]+ $ /x }
Subroutine _isprint: not called CC=1 SCAR=6.9
18  
19 2 my $clearfirst;
20 my %input;
21  
22 0 ▶sub _clear {
Subroutine _clear: not called CC=3 SCAR=24.8
23 0 ▶ return unless $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] 0 0 0
24 0 ▶ open my $OUT, ">/dev/tty" or croak "Cannot write to terminal: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $OUT, ">/dev/tty" or croak("Cannot write to terminal: $!") 0 0 0
25 0 print {$OUT} "\n" x 60;
26 0 $clearfirst = 0;
27 }
28  
29 2 our %flags_arg = (
30 p => 'prompt',
31 s => 'speed',
32 e => 'echo',
33 r => 'require',
34 d => 'default',
35 u => 'until',
36 w => 'while',
37 nl => 'newline',
38 m => 'menu',
39 );
40  
41 2 our %flags_alias = (
42 '-okayif' => '-while', '-okay_if' => '-while',
43 '-failif' => '-until', '-fail_if' => '-until',
44 );
45  
46 2 our %flags_noarg = (
47 y => 'yes',
48 n => 'no',
49 i => 'integer',
50 num => 'number',
51 raw => 'raw_input',
52 1 => 'onechar',
53 c => 'clear',
54 f => 'clearfirst',
55 a => 'argv',
56 l => 'line',
57 t => 'tty',
58 x => 'escape',
59 );
60  
61 2 my $RECORD; # store filehandle for __PROMPT__ file supporting -record flag
62  
63 2 $flags_arg{$_} = $_ for values %flags_arg;
64 2 $flags_noarg{$_} = $_ for values %flags_noarg;
65  
66 2 my $flag_with_arg = join '|', reverse sort keys %flags_arg;
67 2 my $flag_no_arg = join '|', reverse sort keys %flags_noarg;
68  
69 2 my %yespat = (
70 'y' => qr/^\s*[yY]/,
71 'Y' => qr/^\s*Y/,
72 );
73  
74 2 my %nopat = (
75 'n' => qr/^\s*[nN]/,
76 'N' => qr/^\s*N/,
77 );
78  
79 2 my %num_pat = (
80 integer => qr{[+-]? \d+ (?:[Ee]+?\d+ )?}x,
81 number => qr{[+-]? (?:\d+[.]?\d* | [.]\d+) (?:[Ee][+-]?\d+)? }x,
82 );
83  
84 0 ▶sub _get_prompt (\%@) {
Subroutine _get_prompt: not called CC=32 SCAR=69.6
85 0 my ($flags, @data) = @_;
86 0 my ($OUT);
87 0 ▶ @data = map { $flags_alias{$_} || defined($_) ? $_ : "" } @data;
Branch: 1 branch
BranchTrueFalseTotal
$IO::Prompt::flags_alias{$_} || defined $_ ? : 0 0 0
Condition0%0/3
$IO::Prompt::flags_alias{$_} or defined $_
l!l&&r!l&&!r
000
MC/DC0%0/2
$IO::Prompt::flags_alias{$_} or defined $_
$IO::Prompt::flags_alias{$_} defined $_
Truth table0%0/3
A or B
A= $IO::Prompt::flags_alias{$_}B= defined $_
ABresult
1X1
011
000
88 0 ▶ for (my $i = 0 ; $i < @data ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @data) 0 0 0
89 0 local *_ = \$data[$i];
90 0 ▶ if (ref eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $_ eq 'HASH') { } 0 0 0
elsif (ref $_ eq 'GLOB' or UNIVERSAL::isa($_, 'IO::Handle')) { } 0 0 0
elsif (/^-/) { } 0 0 0
91 0 splice @data, $i + 1, 0, %$_;
92 }
93 ▶ elsif (ref eq 'GLOB' or UNIVERSAL::isa($_, 'IO::Handle')) {
Condition0%0/3
ref $_ eq "GLOB" or UNIVERSAL::isa($_, "IO::Handle")
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $_ eq "GLOB" or UNIVERSAL::isa($_, "IO::Handle")
ref $_ eq "GLOB" UNIVERSAL::isa($_, "IO::Handle")
Truth table0%0/3
A or B
A= ref $_ eq "GLOB"B= UNIVERSAL::isa($_, "IO::Handle")
ABresult
1X1
011
000
94 0 ▶ croak "Can't write prompt to read-only $_" unless -w;
Branch: 1 branch
BranchTrueFalseTotal
unless -w $_ 0 0 0
95 0 $OUT = $_;
96 }
97 elsif (/^-/) { # a flag
98 0 s/_//g;
99 0 ▶ if (s/^-(f|clearfirst)/-/) {
Branch: 10 branches
BranchTrueFalseTotal
if (s/^-(f|clearfirst)/-/) { } 0 0 0
elsif (s/^-(yes|y)/-/i) { } 0 0 0
elsif (s/^-(?:nl|newline)/-/i) { } 0 0 0
elsif (s/^-escape|-x/-/i) { } 0 0 0
elsif (s/^-raw_?(?:input)?/-/i) { } 0 0 0
elsif (s/^-number|-num/-/i) { } 0 0 0
elsif (s/^-integer|-i/-/i) { } 0 0 0
elsif (s/^-(no|n)/-/i) { } 0 0 0
elsif (/^-($flag_with_arg)/) { } 0 0 0
elsif (s/^-($flag_no_arg)/-/) { } 0 0 0
100 0 ▶ $clearfirst = 1 unless defined $clearfirst;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $clearfirst 0 0 0
101 }
102 elsif (s/^-(yes|y)/-/i) {
103 0 $flags->{-yesno}{yes} = $yespat{ substr $1, 0, 1 };
104 0 $flags->{-yesno}{yesprompt} = substr $1, 0, 1;
105 }
106 elsif (s/^-(?:nl|newline)/-/i) {
107 0 $flags->{-nlstr} = $data[ $i + 1 ];
108 0 undef $data[ $i++ ];
109 }
110 elsif (s/^-escape|-x/-/i) {
111 0 $flags->{-escape} = 1;
112 }
113 elsif (s/^-raw_?(?:input)?/-/i) {
114 0 $flags->{-raw_input} = 1;
115 }
116 elsif (s/^-number|-num/-/i) {
117 0 $flags->{-number} = 'number';
118 }
119 elsif (s/^-integer|-i/-/i) {
120 0 $flags->{-number} = 'integer';
121 }
122 elsif (s/^-(no|n)/-/i) {
123 0 $flags->{-yesno}{no} = $nopat{ substr $1, 0, 1 };
124 0 $flags->{-yesno}{noprompt} = substr $1, 0, 1;
125 }
126 elsif (m/^-($flag_with_arg)/) {
127 0 ▶ croak "Missing argument for $_ option" if @data < $i+2
Branch: 1 branch
BranchTrueFalseTotal
if @data < $i + 2 or not defined $data[$i + 1] 0 0 0
Condition0%0/3
@data < $i + 2 or not defined $data[$i + 1]
l!l&&r!l&&!r
000
MC/DC0%0/2
@data < $i + 2 or not defined $data[$i + 1]
@data < $i + 2 not defined $data[$i + 1]
Truth table0%0/3
A or not(B)
A= @data < $i + 2B= not defined $data[$i + 1]
ABresult
1X1
011
000
128 || !defined $data[$i+1];
129 0 s/^-($flag_with_arg)/-/;
130 0 $flags->{ -$flags_arg{$1} } = $data[$i+1];
131 0 undef $data[$i++];
132 }
133 elsif (s/^-($flag_no_arg)/-/) {
134 0 $flags->{ -$flags_noarg{$1} } = 1;
135 }
136 0 else { croak "Unknown flag ($_) in prompt" }
137  
138 0 ▶ redo if defined $_ && /^-./;
Branch: 1 branch
BranchTrueFalseTotal
if defined $_ and /^-./ 0 0 0
Condition0%0/3
defined $_ and /^-./
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ and /^-./
defined $_ /^-./
Truth table0%0/3
A and B
A= defined $_B= /^-./
ABresult
0X0
100
111
139 }
140 0 else { next }
141 0 undef $data[$i];
142 }
143 $_ =
144 !defined() ? undef
145 : ref eq 'Regexp' ? $_
146 : qr/^\Q$_\E$/
147 0 ▶ for @{$flags}{qw(-while -until -failif -okayif)};
Branch: 2 branches
BranchTrueFalseTotal
!defined($_) ? : 0 0 0
ref $_ eq 'Regexp' ? : 0 0 0
148  
149 0 for (grep { defined } $flags->{ -require }) {
150 0 ▶ croak "Argument to -require must be hash reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ eq "HASH" 0 0 0
151 unless ref eq 'HASH';
152 0 my %reqs = %$_;
153 0 ▶ $_ = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
154 0 my ($input) = @_;
155 0 for (keys %reqs) {
156 0 ▶ return $_ unless _smartmatch($input, $reqs{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless _smartmatch($input, $reqs{$_}) 0 0 0
157 }
158 0 return;
159 0 };
160 }
161 0 my @prompt = grep { defined } @data;
162 0 ▶ if (@prompt && exists $flags->{-default}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@prompt and exists $flags->{"-default"}) 0 0 0
Condition0%0/3
@prompt and exists $flags->{"-default"}
!ll&&!rl&&r
000
MC/DC0%0/2
@prompt and exists $flags->{"-default"}
@prompt exists $flags->{"-default"}
Truth table0%0/3
A and B
A= @promptB= exists $flags->{"-default"}
ABresult
0X0
100
111
163 0 my $prompt = join "", @prompt;
164 0 ▶ $prompt =~ s/(:?\s*)$/ [$flags->{-default}]$1/ if $prompt !~ /\[.*\]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $prompt =~ /\[.*\]/ 0 0 0
165 0 @prompt = $prompt;
166 }
167 0 return $OUT, @prompt;
168 }
169  
170 2 my $prompt_req = "(The value entered is not acceptable) ";
171  
172 0 ▶sub prompt {
Subroutine prompt: not called CC=26 SCAR=62.2
Pod documented
173 0 my $caller = caller;
174  
175 0 local $\ = q{}; # Make sure no funny business on print statements
176  
177 0 my %flags;
178 0 my ($OUT, @prompt) = _get_prompt(%flags, @_);
179 0 ▶ open $OUT, ">/dev/tty" or croak "Cannot write to terminal: $!" if !$OUT;
Branch: 1 branch
BranchTrueFalseTotal
unless $OUT 0 0 0
Condition0%0/5
$OUT || (open($OUT, '>/dev/tty') || croak("Cannot write to terminal: $!"))
l!l&&r!l&&!r
000
open $OUT, ">/dev/tty" or croak("Cannot write to terminal: $!")
l!l
00
MC/DC0%0/2
$OUT || (open($OUT, '>/dev/tty') || croak("Cannot write to terminal: $!"))
$OUT open $OUT, ">/dev/tty"
Truth table0%0/3
A || B
A= $OUTB= open $OUT, ">/dev/tty"
ABresult
1X1
011
000
180 0 $OUT->autoflush(1);
181 0 ▶ @prompt = $flags{ -prompt } if !@prompt and $flags{ -prompt };
Branch: 1 branch
BranchTrueFalseTotal
if not @prompt and $flags{"-prompt"} 0 0 0
Condition0%0/3
not @prompt and $flags{"-prompt"}
!ll&&!rl&&r
000
MC/DC0%0/2
not @prompt and $flags{"-prompt"}
not @prompt $flags{"-prompt"}
Truth table0%0/3
not(A) and B
A= not @promptB= $flags{"-prompt"}
ABresult
0X0
100
111
182 0 my $IN;
183 0 ▶ if ($flags{-tty} || $flags{-argv}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags{'-tty'} or $flags{'-argv'}) { } 0 0 0
Condition0%0/3
$flags{"-tty"} or $flags{"-argv"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$flags{"-tty"} or $flags{"-argv"}
$flags{"-tty"} $flags{"-argv"}
Truth table0%0/3
A or B
A= $flags{"-tty"}B= $flags{"-argv"}
ABresult
1X1
011
000
184 0 ▶ open $IN, "</dev/tty" or croak "Cannot read from terminal: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $IN, "</dev/tty" or croak("Cannot read from terminal: $!") 0 0 0
185 }
186 else {
187 2 ▶ use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
188 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
189 0 my $ARGV = $caller . "::ARGV";
190 0 ▶ unless (Scalar::Util::openhandle(*$ARGV)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&Scalar::Util::openhandle(*$ARGV)) 0 0 0
191 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
192 0 ▶ open $ARGV or croak "Can't open $$ARGV: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $ARGV or croak("Can't open $${ARGV}: $!") 0 0 0
193 }
194 0 $IN = \*$ARGV;
195 0 ▶ @prompt = () unless -t $IN;
Branch: 1 branch
BranchTrueFalseTotal
unless -t $IN 0 0 0
196 }
197 0 ▶ $flags{-speed} = 0.075 unless defined $flags{-speed};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $flags{"-speed"} 0 0 0
198 2 ▶ use Want qw( want );
Subroutine BEGIN: called CC=1 SCAR=0.0
199 0 ▶ $flags{-set_underscore} ||= want('BOOL');
Condition0%0/3
$flags{"-set_underscore"} ||= want("BOOL")
l!l&&r!l&&!r
000
MC/DC0%0/2
$flags{"-set_underscore"} ||= want("BOOL")
$flags{"-set_underscore"} want("BOOL")
Truth table0%0/3
A ||= B
A= $flags{"-set_underscore"}B= want("BOOL")
ABresult
1X1
011
000
200  
201 0 ▶ $clearfirst = 1 if !defined($clearfirst) && $flags{-clearfirst};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $clearfirst and $flags{"-clearfirst"} 0 0 0
Condition0%0/3
not defined $clearfirst and $flags{"-clearfirst"}
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $clearfirst and $flags{"-clearfirst"}
not defined $clearfirst $flags{"-clearfirst"}
Truth table0%0/3
not(A) and B
A= not defined $clearfirstB= $flags{"-clearfirst"}
ABresult
0X0
100
111
202 0 ▶ _clear($flags{ -clear } || $clearfirst);
Condition0%0/3
$flags{'-clear'} || $clearfirst
l!l&&r!l&&!r
000
MC/DC0%0/2
$flags{'-clear'} || $clearfirst
$flags{'-clear'} $clearfirst
Truth table0%0/3
A || B
A= $flags{'-clear'}B= $clearfirst
ABresult
1X1
011
000
203 0 my $input;
204 0 ▶ if (-t $IN and defined $input{$caller}) {
Branch: 5 branches
BranchTrueFalseTotal
if (-t $IN and defined $input{$caller}) { } 0 0 0
elsif ($flags{'-argv'}) { } 0 0 0
elsif ($flags{'-yesno'}) { } 0 0 0
elsif ($flags{'-number'}) { } 0 0 0
elsif ($flags{'-menu'}) { } 0 0 0
Condition0%0/3
-t $IN and defined $input{$caller}
!ll&&!rl&&r
000
MC/DC0%0/2
-t $IN and defined $input{$caller}
-t $IN defined $input{$caller}
Truth table0%0/3
A and B
A= -t $INB= defined $input{$caller}
ABresult
0X0
100
111
205 0 $input = _fake_from_DATA($caller, $IN, $OUT, \%flags, @prompt);
206 }
207 elsif ($flags{-argv}) {
208 0 ▶ return if @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
if @ARGV 0 0 0
209 0 ▶ @prompt = "Args for $0: " if -t $IN and !@prompt;
Branch: 1 branch
BranchTrueFalseTotal
if -t $IN and not @prompt 0 0 0
Condition0%0/3
-t $IN and not @prompt
!ll&&!rl&&r
000
MC/DC0%0/2
-t $IN and not @prompt
-t $IN not @prompt
Truth table0%0/3
A and not(B)
A= -t $INB= not @prompt
ABresult
0X0
100
111
210 0 print {$OUT} @prompt;
211 0 ▶ @ARGV = map glob, split /\s+/, get_input($IN, $OUT, \%flags, @prompt);
Subroutine BEGIN: called CC=1 SCAR=0.2
212 0 return @ARGV;
213 }
214 elsif ($flags{-yesno}) {
215 0 return _yesno($IN, $OUT, \%flags, @prompt);
216 }
217 elsif ($flags{-number}) {
218 0 return _number($IN, $OUT, \%flags, @prompt);
219 }
220 elsif ($flags{-menu}) {
221 0 return _menu($IN, $OUT, \%flags, @prompt);
222 }
223 else {
224 0 print {$OUT} @prompt;
225 0 $input = get_input($IN, $OUT, \%flags, @prompt);
226 }
227 0 return _tidy($input, %flags);
228 }
229  
230 0 ▶sub _tidy {
Subroutine _tidy: not called CC=10 SCAR=47.0
231 0 my ($input, %flags) = @_;
232 0 my $defined = defined $input;
233 0 ▶ chomp $input if $defined && !$flags{-line};
Branch: 1 branch
BranchTrueFalseTotal
if $defined and not $flags{"-line"} 0 0 0
Condition0%0/3
$defined and not $flags{"-line"}
!ll&&!rl&&r
000
MC/DC0%0/2
$defined and not $flags{"-line"}
$defined not $flags{"-line"}
Truth table0%0/3
A and not(B)
A= $definedB= not $flags{"-line"}
ABresult
0X0
100
111
234 my $success = $defined
235 && (!$flags{ -while } || $input =~ $flags{ -while })
236 0 ▶ && (!$flags{ -until } || $input !~ $flags{ -until });
Condition0%0/12
$defined && (!$flags{'-while'} || $input =~ /$flags{'-while'}/) && !($flags{'-until'} && $input =~ /$flags{'-until'}/)
!ll&&!rl&&r
000
$defined and !$flags{"-while"} || $input =~ /$flags{"-while"}/
!ll&&!rl&&r
000
not $flags{"-while"} or $input =~ /$flags{"-while"}/
l!l&&r!l&&!r
000
$flags{'-until'} && $input =~ /$flags{'-until'}/
!ll&&!rl&&r
000
MC/DC0%0/5
$defined && (!$flags{'-while'} || $input =~ /$flags{'-while'}/) && !($flags{'-until'} && $input =~ /$flags{'-until'}/)
$defined not $flags{"-while"} $input =~ /$flags{"-while"}/ $flags{'-until'} $input =~ /$flags{'-until'}/
Truth table0%0/8
A and not(B) or C && not(D && E)
A= $definedB= not $flags{"-while"}C= $input =~ /$flags{"-while"}/D= $flags{'-until'}E= $input =~ /$flags{'-until'}/
ABCDEresult
0XXXX0
100XX0
11X110
101110
11X0X1
11X101
1010X1
101101
237 0 ▶ print {$RECORD} $input, "\n" if $success && $RECORD;
Branch: 1 branch
BranchTrueFalseTotal
if $success and $RECORD 0 0 0
Condition0%0/3
$success and $RECORD
!ll&&!rl&&r
000
MC/DC0%0/2
$success and $RECORD
$success $RECORD
Truth table0%0/3
A and B
A= $successB= $RECORD
ABresult
0X0
100
111
238 0 ▶ return "$input" if $flags{-raw_input};
Branch: 1 branch
BranchTrueFalseTotal
if $flags{"-raw_input"} 0 0 0
239 return bless {
240 value => $input,
241 success => $success,
242 set_val => $flags{ -set_underscore },
243 },
244 0 'IO::Prompt::ReturnVal';
245 }
246  
247 0 ▶sub _success {
Subroutine _success: not called CC=4 SCAR=30.0
248 0 my ($val, $no_set, $raw) = @_;
249 0 ▶ print {$RECORD} $val, "\n" if $val && $RECORD;
Branch: 1 branch
BranchTrueFalseTotal
if $val and $RECORD 0 0 0
Condition0%0/3
$val and $RECORD
!ll&&!rl&&r
000
MC/DC0%0/2
$val and $RECORD
$val $RECORD
Truth table0%0/3
A and B
A= $valB= $RECORD
ABresult
0X0
100
111
250 0 ▶ return "$val" if $raw;
Branch: 1 branch
BranchTrueFalseTotal
if $raw 0 0 0
251 0 return bless {
252 value => $val,
253 success => 1,
254 set_val => !$no_set,
255 },
256 'IO::Prompt::ReturnVal';
257 }
258  
259 0 ▶sub _failure {
Subroutine _failure: not called CC=2 SCAR=17.9
260 0 my ($val, $raw) = @_;
261 0 ▶ return "$val" if $raw;
Branch: 1 branch
BranchTrueFalseTotal
if $raw 0 0 0
262 0 return bless {
263 value => $val,
264 success => 0,
265 set_val => 0,
266 },
267 'IO::Prompt::ReturnVal';
268 }
269  
270 1 ▶sub import {
Subroutine import: called CC=10 SCAR=34.2
271 1 my $class = shift;
272  
273 {
274 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
275 1 *{ caller() . "::$_" } = \&{$_} for @EXPORT;
276  
277 1 foreach my $sym (@_) {
278 0 ▶ grep { $_ eq $sym } @EXPORT_OK or next;
Branch: 1 branch
BranchTrueFalseTotal
grep {$_ eq $sym;} @IO::Prompt::EXPORT_OK or next 0 0 0
279 0 *{ caller() . "::$sym" } = \&{$sym};
280 }
281 }
282  
283 1 @_ = grep /^-/, @_;
284 1 $input{ caller() } = undef;
285 1 ▶ if ("@_" eq "-clearfirst") {
Branch: 1 branch
BranchTrueFalseTotal
if (join($", @_) eq "-clearfirst") 0 1 1
286 0 $clearfirst = 1;
287 0 return;
288 }
289 1 for my $i (0 .. $#_) {
290 0 ▶ last if $RECORD;
Branch: 1 branch
BranchTrueFalseTotal
if $RECORD 0 0 0
291 0 ▶ if ($_[$i] eq '-record') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[$i] eq "-record") 0 0 0
292 0 splice @_, $i, 1;
293 0 ▶ open $RECORD, '>', '__PROMPT__'
Branch: 1 branch
BranchTrueFalseTotal
open $RECORD, ">", "__PROMPT__" or croak("Can't open __PROMPT__ recording file: $!") 0 0 0
294 or croak "Can't open __PROMPT__ recording file: $!";
295 0 print {$RECORD} "__DATA__\n__PROMPT__\n";
296 }
297 }
298 1 ▶ prompt @_ if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 1 1
299 }
300  
301 1 ▶CHECK {
Subroutine CHECK: called CC=6 SCAR=24.7
302 1 for my $pkg (keys %input) {
303 1 ▶ next if defined $input{$pkg};
Branch: 1 branch
BranchTrueFalseTotal
if defined $input{$pkg} 0 1 1
304  
305 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
306 1 ▶ if (my $datahandle = *{"${pkg}::DATA"}{IO}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $datahandle = *{"${pkg}::DATA";}{'IO'}) { } 0 1 1
307 0 local $/;
308 0 my $data = <$datahandle>;
309 0 ▶ if ($data =~ s/(\A|\n) __PROMPT__ \s*? \n (.*)/$1/xs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ s/(\A|\n) __PROMPT__ \s*? \n (.*)/$1/sx) { } 0 0 0
310 0 $input{$pkg} = "$2";
311 }
312 else {
313 0 delete $input{$pkg};
314 }
315 0 ▶ open "${pkg}::DATA", "<", \$data or die "Internal error: $!";
Branch: 1 branch
BranchTrueFalseTotal
open "${pkg}::DATA", "<", \$data or die "Internal error: $!" 0 0 0
316 }
317 else {
318 1 delete $input{$pkg};
319 }
320 }
321 }
322  
323 2 my $baseline = ord 'A';
324  
325 0 ▶sub _visualize {
Subroutine _visualize: not called CC=5 SCAR=34.0
326 0 local ($_) = @_;
327 0 ▶ return _isprint($_) ? $_
Branch: 4 branches
BranchTrueFalseTotal
_isprint($_) ? : 0 0 0
$_ eq "\n" ? : 0 0 0
ord $_ == 0 ? : 0 0 0
ord $_ < $baseline ? : 0 0 0
328 : $_ eq "\n" ? $_
329 : ord($_) == 0 ? ''
330 : ord($_) < $baseline ? '^' . chr($baseline + ord($_) - 1)
331 : '?'
332 }
333  
334 0 ▶sub hand_print {
Subroutine hand_print: not called CC=10 SCAR=47.0
Pod documented
335 0 my $OUT = \*STDOUT;
336 0 my $echo = undef;
337 0 my $speed = 0.05;
338 0 local $| = 1;
339 0 for (@_) {
340 0 ▶ if (ref eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_ eq 'HASH') { } 0 0 0
elsif (not $speed) { } 0 0 0
341 0 ▶ $speed = $_->{-speed} if exists $_->{-speed};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_->{"-speed"} 0 0 0
342 0 ▶ $OUT = $_->{-to} if exists $_->{-to};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_->{"-to"} 0 0 0
343 0 ▶ $echo = $_->{-echo} if exists $_->{-echo};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_->{"-echo"} 0 0 0
344 }
345 elsif (!$speed) {
346 0 print {$OUT} $_;
347 }
348 else {
349 0 print {$OUT} $_ and select undef, undef, undef, rand $speed
350 0 ▶ for map { defined $echo ? $echo : _visualize($_) } split "";
Branch: 2 branches
BranchTrueFalseTotal
defined $echo ? : 0 0 0
print {$OUT;} $_ and select undef, undef, undef, rand $speed 0 0 0
351 }
352 }
353 0 return scalar @_;
354 }
355  
356 0 ▶sub _fake_from_DATA {
Subroutine _fake_from_DATA: not called CC=11 SCAR=48.8
357 0 my ($caller, $IN, $OUT, $flags, @prompt) = @_;
358 0 ▶ local $SIG{INT} = sub { ReadMode 'restore', $IN; exit };
Subroutine __ANON__: not called CC=1 SCAR=6.9
359 0 ReadMode 'noecho', $IN;
360 0 ReadMode 'raw', $IN;
361 0 print {$OUT} @prompt;
362 0 my $input = getc $IN;
363 0 ▶ if ($input =~ /\cD|\cZ/) { print {$OUT} _visualize($input),"\n"; return; }
Branch: 1 branch
BranchTrueFalseTotal
if ($input =~ /\cD|\cZ/) 0 0 0
364 0 ▶ if ($input eq "\e") {
Branch: 1 branch
BranchTrueFalseTotal
if ($input eq "\e") 0 0 0
365 0 ReadMode 'restore', $IN;
366 0 return get_input($IN, $OUT, $flags, @prompt);
367 }
368 0 $input{$caller} =~ m/\G (?!\cD|\cZ) (.*) (\n?)/xgc;
369 0 my ($line, $nlstr) = ($1, $2);
370 0 ▶ unless (defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 0 0 0
371 0 ▶ while ($input ne "\n") { $input = getc $IN }
Branch: 1 branch
BranchTrueFalseTotal
if ($input ne "\n") 0 0 0
372 0 print {$OUT} "\n";
373 0 return;
374 }
375 0 ▶ delete $input{$caller} if pos $input{$caller} == length $input{$caller};
Branch: 1 branch
BranchTrueFalseTotal
if pos $input{$caller} == length $input{$caller} 0 0 0
376 0 ▶ if ($input eq "\n") {
Branch: 1 branch
BranchTrueFalseTotal
if ($input eq "\n") { } 0 0 0
377 0 hand_print { -to => $OUT, %$flags }, $line;
378 0 ▶ unless (defined <$IN>) { print {$OUT} "\n"; return; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined readline $IN) 0 0 0
379 }
380 else {
381 0 my $i = 0;
382 0 while (1) {
383 0 my $done = $i >= length $line;
384 0 ▶ print {$OUT} substr($line, $i++, 1) unless $done;
Branch: 1 branch
BranchTrueFalseTotal
unless $done 0 0 0
385 0 ▶ if (getc $IN eq "\n") {
Branch: 1 branch
BranchTrueFalseTotal
if (getc $IN eq "\n") 0 0 0
386 0 ▶ last if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 0 0 0
387 0 hand_print { -to => $OUT, %$flags }, substr($line, $i);
388 0 $i = length $line;
389 }
390 }
391 }
392 0 ReadMode 'restore', $IN;
393 0 print {$OUT} "\n";
394 0 return $line . $nlstr;
395 }
396  
397 0 ▶sub get_input {
Subroutine get_input: not called CC=30 SCAR=68.4
Pod documented
398 0 my ($IN, $OUT, $flags, @prompt) = @_;
399 my ($onechar, $nlstr, $echo, $require) =
400 0 @{$flags}{ -onechar, -nlstr, -echo, -'require' };
401 0 ▶ $nlstr = "\n" unless defined $nlstr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $nlstr 0 0 0
402 0 ▶ if (!-t $IN) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-t $IN) 0 0 0
403 0 ▶ return scalar <$IN> unless $onechar;
Branch: 1 branch
BranchTrueFalseTotal
unless $onechar 0 0 0
404 0 return getc $IN;
405 }
406 0 $OUT->autoflush(1);
407 0 ▶ local $SIG{INT} = sub { ReadMode 'restore', $IN; exit };
Subroutine __ANON__: not called CC=1 SCAR=6.9
408 0 my ($input, $newlines);
409 0 my %cntl = GetControlChars $IN;
410 0 my $cntl = join '|', values %cntl;
411 0 ReadMode 'raw', $IN;
412  
413 0 INPUT: while (1) {
414 0 my $next = getc $IN;
415 0 ▶ if ($next eq $cntl{INTERRUPT}) {
Branch: 5 branches
BranchTrueFalseTotal
if ($next eq $cntl{'INTERRUPT'}) { } 0 0 0
elsif ($next eq $cntl{'ERASE'}) { } 0 0 0
elsif ($next eq $cntl{'EOF'}) { } 0 0 0
elsif ($flags->{'-escape'} and $next eq "\e") { } 0 0 0
elsif (not $next =~ /$cntl/ and defined $next) { } 0 0 0
416 0 ReadMode 'restore', $IN;
417 0 exit;
418 }
419 elsif ($next eq $cntl{ERASE}) {
420 0 ▶ if (defined $input && length $input) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $input and length $input) 0 0 0
Condition0%0/3
defined $input and length $input
!ll&&!rl&&r
000
MC/DC0%0/2
defined $input and length $input
defined $input length $input
Truth table0%0/3
A and B
A= defined $inputB= length $input
ABresult
0X0
100
111
421 0 substr($input, -1) = "";
422 0 print {$OUT} "\b \b";
423 }
424 0 next;
425 }
426 elsif ($next eq $cntl{EOF}) {
427 0 ReadMode 'restore', $IN;
428 0 close $IN;
429 0 return $input;
430 }
431 ▶ elsif ($flags->{-escape} && $next eq "\e") {
Condition0%0/3
$flags->{"-escape"} and $next eq "\e"
!ll&&!rl&&r
000
MC/DC0%0/2
$flags->{"-escape"} and $next eq "\e"
$flags->{"-escape"} $next eq "\e"
Truth table0%0/3
A and B
A= $flags->{"-escape"}B= $next eq "\e"
ABresult
0X0
100
111
432 0 ReadMode 'restore', $IN;
433 0 print {$OUT} "<esc>";
434 0 return "\e";
435 }
436 ▶ elsif ($next !~ /$cntl/ && defined $next) {
Condition0%0/3
not $next =~ /$cntl/ and defined $next
!ll&&!rl&&r
000
MC/DC0%0/2
not $next =~ /$cntl/ and defined $next
not $next =~ /$cntl/ defined $next
Truth table0%0/3
not(A) and B
A= not $next =~ /$cntl/B= defined $next
ABresult
0X0
100
111
437 0 $input .= $next;
438 0 ▶ if ($next eq "\n") {
Branch: 1 branch
BranchTrueFalseTotal
if ($next eq "\n") { } 0 0 0
439 0 ▶ if ($input eq "\n" && exists $flags->{-default}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input eq "\n" and exists $flags->{"-default"}) 0 0 0
Condition0%0/3
$input eq "\n" and exists $flags->{"-default"}
!ll&&!rl&&r
000
MC/DC0%0/2
$input eq "\n" and exists $flags->{"-default"}
$input eq "\n" exists $flags->{"-default"}
Truth table0%0/3
A and B
A= $input eq "\n"B= exists $flags->{"-default"}
ABresult
0X0
100
111
440 0 ▶ print {$OUT} (
Branch: 2 branches
BranchTrueFalseTotal
defined $echo && $flags->{'-menu'} ? : 0 0 0
defined $echo ? : 0 0 0
Condition0%0/3
defined $echo and $flags->{"-menu"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $echo and $flags->{"-menu"}
defined $echo $flags->{"-menu"}
Truth table0%0/3
A and B
A= defined $echoB= $flags->{"-menu"}
ABresult
0X0
100
111
441 defined $echo
442 && $flags->{-menu} ? $echo
443 : defined $echo ? $echo x length($flags->{-default})
444 0 : '['.$flags->{-default}.']'
445 );
446 0 print {$OUT} $nlstr;
447 0 ReadMode 'restore', $IN;
448 return $onechar ? substr($_, 0, 1) : $_
449 0 ▶ for $flags->{-default};
Branch: 1 branch
BranchTrueFalseTotal
$onechar ? : 0 0 0
450 }
451 0 $newlines .= $nlstr;
452 }
453 else {
454 0 ▶ print {$OUT}(defined $echo ? $echo : $next);
Branch: 1 branch
BranchTrueFalseTotal
defined $echo ? : 0 0 0
455 }
456 }
457 else {
458 0 $input .= $next;
459 }
460 0 ▶ if ($onechar or !defined $next or $input =~ m{\Q$/\E$}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($onechar or not defined $next or $input =~ m[\Q$/\E$]) 0 0 0
Condition0%0/6
$onechar or not defined $next or $input =~ m[\Q$/\E$]
l!l&&r!l&&!r
000
$onechar or not defined $next
l!l&&r!l&&!r
000
MC/DC0%0/3
$onechar or not defined $next or $input =~ m[\Q$/\E$]
$onechar not defined $next $input =~ m[\Q$/\E$]
Truth table0%0/4
A or not(B) or C
A= $onecharB= not defined $nextC= $input =~ m[\Q$/\E$]
ABCresult
1XX1
01X1
0011
0000
461 0 ▶ chomp $input unless $flags->{-line};
Branch: 1 branch
BranchTrueFalseTotal
unless $flags->{"-line"} 0 0 0
462 0 ▶ if ($require and my $mesg = $require->($input)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($require and my $mesg = &$require($input)) { } 0 0 0
Condition0%0/3
$require and my $mesg = &$require($input)
!ll&&!rl&&r
000
MC/DC0%0/2
$require and my $mesg = &$require($input)
$require my $mesg = &$require($input)
Truth table0%0/3
A and B
A= $requireB= my $mesg = &$require($input)
ABresult
0X0
100
111
463 0 print {$OUT} "\r", " " x 79, "\r", sprintf($mesg, @prompt);
464 0 undef $input;
465 0 undef $newlines;
466 }
467 else {
468 0 ReadMode 'restore', $IN;
469 0 ▶ print {$OUT} $newlines if defined $newlines;
Branch: 1 branch
BranchTrueFalseTotal
if defined $newlines 0 0 0
470 0 ▶ return $onechar ? substr($input, 0, 1) : $input;
Branch: 1 branch
BranchTrueFalseTotal
$onechar ? : 0 0 0
471 }
472 }
473 }
474 }
475  
476 0 ▶sub _yesno {
Subroutine _yesno: not called CC=15 SCAR=54.8
477 0 my ($IN, $OUT, $flags, @prompt) = @_;
478 my ($yes, $no, $yesprompt, $noprompt) =
479 0 @{ $flags->{ -yesno } }{qw(yes no yesprompt noprompt)};
480 0 ▶ $yes = qr/^([^Nn])/ unless defined $yes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $yes 0 0 0
481 0 ▶ $no = qr/^([^Yy])/ unless defined $no;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $no 0 0 0
482 0 ▶ my $prompt2 =
Branch: 2 branches
BranchTrueFalseTotal
$yesprompt && $noprompt ? : 0 0 0
$yesprompt ? : 0 0 0
Condition0%0/3
$yesprompt and $noprompt
!ll&&!rl&&r
000
MC/DC0%0/2
$yesprompt and $noprompt
$yesprompt $noprompt
Truth table0%0/3
A and B
A= $yespromptB= $noprompt
ABresult
0X0
100
111
483 $yesprompt && $noprompt ? "'$yesprompt' or '$noprompt'"
484 : $yesprompt ? "'$yesprompt' for yes"
485 : "'$noprompt' for no";
486 0 my $raw = $flags->{-raw_input};
487 0 ▶ print {$OUT} @prompt if -t $IN;
Branch: 1 branch
BranchTrueFalseTotal
if -t $IN 0 0 0
488 0 while (1) {
489 0 my $response =
490 get_input($IN, $OUT, { %$flags, -nlstr => "" }, @prompt);
491 0 ▶ chomp $response unless $flags->{-line};
Branch: 1 branch
BranchTrueFalseTotal
unless $flags->{"-line"} 0 0 0
492 0 ▶ print {$OUT} "\n" and return _success($response, 'no_set', $raw)
Branch: 1 branch
BranchTrueFalseTotal
if defined $response and $response =~ /$yes/ 0 0 0
Condition0%0/8
defined $response && $response =~ /$yes/ && (print({$OUT;} "\n") && (return _success($response, 'no_set', $raw)))
!ll&&!rl&&r
000
defined $response and $response =~ /$yes/
!ll&&!rl&&r
000
print {$OUT;} "\n" and return _success($response, "no_set", $raw)
!ll
00
MC/DC0%0/3
defined $response && $response =~ /$yes/ && (print({$OUT;} "\n") && (return _success($response, 'no_set', $raw)))
defined $response $response =~ /$yes/ print {$OUT;} "\n"
Truth table0%0/4
A and B && C
A= defined $responseB= $response =~ /$yes/C= print {$OUT;} "\n"
ABCresult
0XX0
10X0
1100
1111
493 if defined $response and $response =~ /$yes/;
494 0 ▶ print {$OUT} "\n" and return _failure($response, $raw)
Branch: 1 branch
BranchTrueFalseTotal
if not defined $response or $response =~ /$no/ 0 0 0
Condition0%0/8
(!defined($response) || $response =~ /$no/) && (print({$OUT;} "\n") && (return _failure($response, $raw)))
!ll&&!rl&&r
000
not defined $response or $response =~ /$no/
l!l&&r!l&&!r
000
print {$OUT;} "\n" and return _failure($response, $raw)
!ll
00
MC/DC0%0/3
(!defined($response) || $response =~ /$no/) && (print({$OUT;} "\n") && (return _failure($response, $raw)))
not defined $response $response =~ /$no/ print {$OUT;} "\n"
Truth table0%0/5
not(A) or B && C
A= not defined $responseB= $response =~ /$no/C= print {$OUT;} "\n"
ABCresult
00X0
1X00
0100
1X11
0111
495 if !defined $response or $response =~ /$no/;
496 0 ▶ print {$OUT} "\r", " " x 79, "\r", @prompt,
Branch: 1 branch
BranchTrueFalseTotal
if -t $IN 0 0 0
497 "(Please answer $prompt2) "
498 if -t $IN;
499 }
500 }
501  
502 0 ▶sub _number {
Subroutine _number: not called CC=10 SCAR=47.0
503 0 my ($IN, $OUT, $flags, @prompt) = @_;
504 0 my $numtype = $flags->{ -number };
505 0 my $prompt_num = "(Please enter a valid $numtype) ";
506 0 my $match = $num_pat{$numtype};
507 0 my $require = $flags->{ -require };
508 0 ▶ print {$OUT} @prompt if -t $IN;
Branch: 1 branch
BranchTrueFalseTotal
if -t $IN 0 0 0
509 0 while (1) {
510 0 my $response =
511 get_input($IN, $OUT, { %$flags, -nlstr => "", -require => undef },
512 @prompt);
513 0 ▶ chomp $response if defined $response && !$flags->{-line};
Branch: 1 branch
BranchTrueFalseTotal
if defined $response and not $flags->{"-line"} 0 0 0
Condition0%0/3
defined $response and not $flags->{"-line"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $response and not $flags->{"-line"}
defined $response not $flags->{"-line"}
Truth table0%0/3
A and not(B)
A= defined $responseB= not $flags->{"-line"}
ABresult
0X0
100
111
514 0 ▶ if (-t $IN and defined $response) {
Branch: 1 branch
BranchTrueFalseTotal
if (-t $IN and defined $response) 0 0 0
Condition0%0/3
-t $IN and defined $response
!ll&&!rl&&r
000
MC/DC0%0/2
-t $IN and defined $response
-t $IN defined $response
Truth table0%0/3
A and B
A= -t $INB= defined $response
ABresult
0X0
100
111
515 0 ▶ if ($response !~ /\A \s* $match \s* \Z/x) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $response =~ /\A \s* $match \s* \Z/x) { } 0 0 0
elsif ($require and my $mesg = &$require($response)) { } 0 0 0
516 0 print {$OUT} "\r", " " x 79, "\r", @prompt, $prompt_num;
517 0 next;
518 }
519 ▶ elsif ($require and my $mesg = $require->($response)) {
Condition0%0/3
$require and my $mesg = &$require($response)
!ll&&!rl&&r
000
MC/DC0%0/2
$require and my $mesg = &$require($response)
$require my $mesg = &$require($response)
Truth table0%0/3
A and B
A= $requireB= my $mesg = &$require($response)
ABresult
0X0
100
111
520 0 print {$OUT} "\r", " " x 79, "\r", sprintf($mesg, @prompt);
521 0 next;
522 }
523 }
524 0 ▶ print {$OUT} "\n" and return _tidy($response);
Branch: 1 branch
BranchTrueFalseTotal
print {$OUT;} "\n" and return _tidy($response) 0 0 0
525 }
526 }
527  
528 0 ▶sub _self { $_[0] }
Subroutine _self: not called CC=1 SCAR=6.9
529  
530 0 ▶sub _menu {
Subroutine _menu: not called CC=27 SCAR=66.3
531 0 my ($IN, $OUT, $flags, @prompt) = @_;
532 0 my $datatype = ref $flags->{ -menu };
533 0 ▶ my @data = $datatype eq 'ARRAY' ? @{ $flags->{ -menu } }
Branch: 1 branch
BranchTrueFalseTotal
$datatype eq 'HASH' ? : 0 0 0
534 0 ▶ : $datatype eq 'HASH' ? sort keys %{ $flags->{ -menu } }
Branch: 1 branch
BranchTrueFalseTotal
$datatype eq 'ARRAY' ? : 0 0 0
535 : croak "Argument to -menu must be hash or array reference";
536  
537 my $val_for = $datatype eq 'ARRAY'
538 ? \&_self
539 0 ▶ : sub { $flags->{ -menu }{$_[0]} };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$datatype eq 'ARRAY' ? : 0 0 0
540  
541 0 my $count = @data;
542  
543 0 ▶ croak "Too many -menu items" if $count > 26;
Branch: 1 branch
BranchTrueFalseTotal
if $count > 26 0 0 0
544 0 ▶ croak "Too few -menu items" if $count < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $count < 1 0 0 0
545  
546 0 my $max_char = chr(ord('a') + $count - 1);
547 0 my $menu = q{};
548  
549 0 my $default_key;
550 0 my $next = 'a';
551 0 for (@data) {
552 0 my $item = $_;
553 0 ▶ if (defined $flags->{ -default } && !defined $default_key && $item eq $flags->{ -default }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $flags->{"-default"} and not defined $default_key and $item eq $flags->{"-default"}) 0 0 0
Condition0%0/6
defined $flags->{"-default"} and not defined $default_key and $item eq $flags->{"-default"}
!ll&&!rl&&r
000
defined $flags->{"-default"} and not defined $default_key
!ll&&!rl&&r
000
MC/DC0%0/3
defined $flags->{"-default"} and not defined $default_key and $item eq $flags->{"-default"}
defined $flags->{"-default"} not defined $default_key $item eq $flags->{"-default"}
Truth table0%0/4
A and not(B) and C
A= defined $flags->{"-default"}B= not defined $default_keyC= $item eq $flags->{"-default"}
ABCresult
0XX0
10X0
1100
1111
554 0 $default_key = $next;
555 }
556 0 $item =~ s/\A/qq{ }.$next++.q{. }/xmse;
557 0 $item =~ s/\n?\z/\n/xms;
558 0 $item =~ s/(?!\Z)\n/\n /gxms;
559 0 $menu .= $item;
560 }
561  
562 0 push @prompt, "\n$menu\n> ";
563  
564 0 my $prompt_range = "(Please enter a-$max_char) > ";
565 0 my $require = $flags->{ -require };
566 0 ▶ print {$OUT} @prompt if -t $IN;
Branch: 1 branch
BranchTrueFalseTotal
if -t $IN 0 0 0
567 0 while (1) {
568 0 my $response =
569 get_input($IN, $OUT, { %$flags, -escape => 1, -nlstr => "", -require => undef },
570 @prompt);
571 0 chomp $response;
572 0 ▶ if (-t $IN and defined $response) {
Branch: 1 branch
BranchTrueFalseTotal
if (-t $IN and defined $response) 0 0 0
Condition0%0/3
-t $IN and defined $response
!ll&&!rl&&r
000
MC/DC0%0/2
-t $IN and defined $response
-t $IN defined $response
Truth table0%0/3
A and B
A= -t $INB= defined $response
ABresult
0X0
100
111
573 0 ▶ if (length $response == 1 && $response eq "\e") {
Branch: 3 branches
BranchTrueFalseTotal
if (length $response == 1 and $response eq "\e") { } 0 0 0
elsif (length $response > 1 or ($response lt 'a' or $response gt $max_char)) { } 0 0 0
elsif ($require and my $mesg = &$require($data[ord($response) - 97])) { } 0 0 0
Condition0%0/3
length $response == 1 and $response eq "\e"
!ll&&!rl&&r
000
MC/DC0%0/2
length $response == 1 and $response eq "\e"
length $response == 1 $response eq "\e"
Truth table0%0/3
A and B
A= length $response == 1B= $response eq "\e"
ABresult
0X0
100
111
574 0 return $response;
575 }
576 ▶ elsif (length $response > 1 || ($response lt 'a' || $response gt $max_char) ) {
Condition0%0/6
length $response > 1 or ($response lt "a" or $response gt $max_char)
l!l&&r!l&&!r
000
$response lt "a" or $response gt $max_char
l!l&&r!l&&!r
000
MC/DC0%0/3
length $response > 1 or ($response lt "a" or $response gt $max_char)
length $response > 1 $response lt "a" $response gt $max_char
Truth table0%0/4
A or B or C
A= length $response > 1B= $response lt "a"C= $response gt $max_char
ABCresult
1XX1
01X1
0011
0000
577 0 ▶ if ($response ne $flags->{-default}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response ne $flags->{"-default"}) 0 0 0
578 0 print {$OUT} "\r", " " x 79, "\r", $prompt_range;
579 0 next;
580 }
581 0 $response = $default_key;
582 }
583 ▶ elsif ($require and my $mesg = $require->($data[ord($response)-ord('a')])) {
Condition0%0/3
$require and my $mesg = &$require($data[ord($response) - 97])
!ll&&!rl&&r
000
MC/DC0%0/2
$require and my $mesg = &$require($data[ord($response) - 97])
$require my $mesg = &$require($data[ord($response) - 97])
Truth table0%0/3
A and B
A= $requireB= my $mesg = &$require($data[ord($response) - 97])
ABresult
0X0
100
111
584 0 print {$OUT} "\r", " " x 79, "\r", sprintf($mesg, @prompt);
585 0 next;
586 }
587 }
588 0 print {$OUT} "\n";
589 0 my $selection = $data[ord($response)-ord('a')];
590 0 ▶ $response = defined $response ? $val_for->($selection) : $response;
Branch: 1 branch
BranchTrueFalseTotal
defined $response ? : 0 0 0
591 0 ▶ if (defined $response && ref($response) =~ m/\A(?:HASH|ARRAY)\z/xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $response and ref($response) =~ /\A(?:HASH|ARRAY)\z/msx) 0 0 0
Condition0%0/3
defined $response and ref($response) =~ /\A(?:HASH|ARRAY)\z/msx
!ll&&!rl&&r
000
MC/DC0%0/2
defined $response and ref($response) =~ /\A(?:HASH|ARRAY)\z/msx
defined $response ref($response) =~ /\A(?:HASH|ARRAY)\z/msx
Truth table0%0/3
A and B
A= defined $responseB= ref($response) =~ /\A(?:HASH|ARRAY)\z/msx
ABresult
0X0
100
111
592 0 $response = _menu($IN, $OUT, {%{$flags}, -menu=>$response}, "$selection: ");
593 0 ▶ if (defined $response && $response eq "\e") {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $response and $response eq "\e") 0 0 0
Condition0%0/3
defined $response and $response eq "\e"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $response and $response eq "\e"
defined $response $response eq "\e"
Truth table0%0/3
A and B
A= defined $responseB= $response eq "\e"
ABresult
0X0
100
111
594 0 ▶ print {$OUT} "\n", @prompt if -t $IN;
Branch: 1 branch
BranchTrueFalseTotal
if -t $IN 0 0 0
595 0 next;
596 }
597 }
598 0 return _tidy($response);
599 }
600 }
601  
602 0 ▶sub _smartmatch {
Subroutine _smartmatch: not called CC=5 SCAR=34.0
603 0 my ($str, $matcher) = @_;
604 0 my $type = ref $matcher;
605 my $res = $type eq 'CODE'
606 0 ▶ ? do { local $_ = $str; $matcher->() }
Branch: 2 branches
BranchTrueFalseTotal
$type eq 'Regexp' ? : 0 0 0
$type eq 'ARRAY' ? : 0 0 0
607 : $type eq 'Regexp' ? ($str =~ $matcher)
608 0 ▶ : $type eq 'ARRAY' ? scalar grep({ _smartmatch($str, $_) } @$matcher)
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'HASH' ? : 0 0 0
609 0 ▶ : $type eq 'HASH' ? $matcher->{$str}
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'CODE' ? : 0 0 0
610 : $str eq $matcher;
611 0 return $res;
612 }
613  
614 package IO::Prompt::ReturnVal;
615  
616 use overload
617 2 ▶ q{bool} => sub {
Subroutine BEGIN: called CC=1 SCAR=3.5
Subroutine __ANON__: not called CC=2 SCAR=17.9
618 0 ▶ $_ = $_[0]{value} if $_[0]{set_val};
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"set_val"} 0 0 0
619 0 $_[0]{handled} = 1;
620 0 $_[0]{success};
621 },
622 0 ▶ q{""} => sub { $_[0]{handled} = 1; "$_[0]{value}"; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
623 0 ▶ q{0+} => sub { $_[0]{handled} = 1; 0 + $_[0]{value}; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
624 2 fallback => 1,
625 2 ;
626  
627 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=2 SCAR=17.9
628 0 ▶ $_ = $_[0]{value} unless $_[0]{handled};
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]{"handled"} 0 0 0
629 }
630  
631 2 1; # Magic true value required at end of module
632 __END__
↑ Summary