blib/lib/Shell/POSIX/Select.pm

filter: Statementstmt 69.8% 379 / 543 Branchbran 38.2% 157 / 410 Conditioncond 28.5% 56 / 196 MC/DCmcdc 7.0% 8 / 113 Subroutinesub 73.3% 22 / 30 Podpod 0.0% 0 / 16 totaltotal 47.5% 622 / 1308 CC 263 SCAR 55.1
CC
263
Cov
52%
import
768.5
log_files
272.0
filter
214.5
CRAP 247.6
↑ Summary Test_Progs/sub2 »
1 package Shell::POSIX::Select;
2  
3 26 our $VERSION = '0.09';
4  
5 # TODO: Portable-ize tput stuff
6  
7 # TODO: Dump user's code-block with same line numbers shown in error
8 # messages for debugging ease
9  
10 # TODO: Add option to embolden menu numbers, to distinguish them from
11 # choices that are also numbers
12  
13 # See documentation and copyright notice below =pod section below
14  
15 # Not using Exporter.pm; doing typeglob-based exporting,
16 # using adapted code from Damian's Switch.pm
17 26 our ( @EXPORT_OK );
18 26 our ($Reply, $Heading, $Prompt);
19 26 @EXPORT_OK = qw( $Heading $Prompt $Reply $Eof );
20  
21 26 our ( $U_WARN, $REPORT, $DEBUG, $DEBUG_default, $_DEBUG, );
22 26 our ( $U_WARN_default, $_import_called, $U_DEBUG, $DEBUG_FILT );
23 26 our ( $sdump, $cdump, $script );
24 #
25 26 our ( @ISA, @EXPORT, $PRODUCTION, $LOGGING, $PKG, $INSTALL_TESTING,$ON,$OFF, $BOLD, $SGR0, $COLS );
26  
27 # What is the maximum number of columns that the user wants to see
28 # on-screen? By default, no maximum -- the number of columns will be
29 # determined by the width of the terminal and the length of the meny
30 # item strings.
31 26 our $MaxColumns = 99;
32 26 push @EXPORT_OK, '$MaxColumns';
33  
34 26 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=13.6
35 26 $PKG = __PACKAGE__ ;
36 26 $LOGGING = 0;
37  
38 0 ▶ $SIG{TERM}=$SIG{QUIT}=$SIG{INT}= sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
39 0 ▶ $DEBUG and warn caller(1), "\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG and warn caller 1, "\n" 0 0 0
40 # must disable reverse-video, if it was turned on
41 0 ▶ defined $ON and $ON ne "" and do {
Branch: 1 branch
BranchTrueFalseTotal
defined $ON and $ON ne "" and do { my $reset = $SGR0 || $OFF; defined $reset and warn "$reset\n" } 0 0 0
Condition0%0/3
defined $ON and $ON ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ON and $ON ne ""
defined $ON $ON ne ""
Truth table0%0/3
A and B
A= defined $ONB= $ON ne ""
ABresult
0X0
100
111
42 0 ▶ my $reset=($SGR0 || $OFF); defined $reset and warn "$reset\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $reset and warn "$reset\n" 0 0 0
Condition0%0/3
$SGR0 || $OFF
l!l&&r!l&&!r
000
MC/DC0%0/2
$SGR0 || $OFF
$SGR0 $OFF
Truth table0%0/3
A || B
A= $SGR0B= $OFF
ABresult
1X1
011
000
43 };
44 0 ▶ $DEBUG and warn "$0: killed by signal\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG and warn "$0: killed by signal\n" 0 0 0
45 0 exit 111; # means, killed by signal
46 26 };
47 26 ▶ ! defined $_import_called and $_import_called = 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $_import_called or $_import_called = 0 0 26 26
48 26 ( $script = $0 ) =~ s|^.*/||;
49  
50 }
51  
52 sub import ; # advance declaration
53  
54 26 ▶use File::Spec::Functions (':ALL');
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 26 ▶use File::Spec::Functions 0.7;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 26 ▶use Filter::Simple 0.84;
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 # Damian's been fixing bugs as I report them, so best to have recent version
60 # This is the oldest version that I know works pretty well
61 26 ▶use Text::Balanced 1.97 qw(extract_variable extract_bracketed);
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 26 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 26 $U_DEBUG=1;
66 26 $U_DEBUG=0;
67  
68 26 $DEBUG_FILT=4;
69 # $DEBUG_FILT=0;
70  
71 # $DEBUG=1; # force verbosity level for debugging messages
72 26 $DEBUG=0; # force verbosity level for debugging messages
73  
74 26 $REPORT=1; # report subroutines when entered
75 # $REPORT=0; # report subroutines when entered
76  
77 26 ▶$DEBUG > 0 and warn "Logging is $LOGGING\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 0 and warn "Logging is $LOGGING\n" 0 26 26
78  
79 # controls messages and carp vs. warn (but that doesn't do much)
80 26 $PRODUCTION=1;
81 26 ▶$PRODUCTION and $REPORT=$DEBUG_FILT=$DEBUG=0;
Branch: 1 branch
BranchTrueFalseTotal
$PRODUCTION and $REPORT = $DEBUG_FILT = $DEBUG = 0 26 0 26
82  
83 26 ▶$DEBUG and disable_buffering();
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG and disable_buffering() 0 26 26
84  
85 sub _WARN; sub _DIE;
86  
87 26 local $_; # avoid clobbering user's by accident
88  
89  
90 26 $Shell::POSIX::Select::_default_style='K'; # default loop-style is Kornish
91 26 $Shell::POSIX::Select::_default_prompt= "\nEnter number of choice:";
92 # I detest the shell's default prompt!
93 26 $Shell::POSIX::Select::_bash_prompt ='#?';
94 26 $Shell::POSIX::Select::_korn_prompt='#?';
95 26 $Shell::POSIX::Select::_generic ='#?';
96 26 $Shell::POSIX::Select::_arrows_prompt='>>';
97  
98 26 $U_WARN_default = 1; # for enabling user-warnings for bad interactive input
99  
100 # $_import_called > 0 or import(); # ensure initialization of defaults
101  
102 26 my $subname=__PACKAGE__ ; # for identifying messages from outside sub's
103  
104 26 my $select2foreach;
105 26 $select2foreach=1; # just translate select into foreach, for debugging
106 26 $select2foreach=0;
107  
108 # warn "Setting up video modes\n";
109 # I know about Term::Cap, but this seems more direct and sufficient
110  
111 26 $Shell::POSIX::Select::_FILTER_CALLS= $Shell::POSIX::Select::_ENLOOP_CALL_COUNT= $Shell::POSIX::Select::_LOOP_COUNT=0;
112 # Number of select loops detected
113 26 ▶$DEBUG > 3 and $LOGGING and warn "About to call log_files\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 3 and $LOGGING and warn "About to call log_files\n" 0 26 26
Condition33%1/3
$DEBUG > 3 and $LOGGING
!ll&&!rl&&r
2600
MC/DC0%0/2
$DEBUG > 3 and $LOGGING
$DEBUG > 3 $LOGGING
Truth table33%1/3
A and B
A= $DEBUG > 3B= $LOGGING
ABresult
0X0
100
111
114  
115 26 ▶$LOGGING and log_files(); # open logfiles, depending on DEBUG setting
Branch: 1 branch
BranchTrueFalseTotal
$LOGGING and log_files() 0 26 26
116  
117 26 ▶$DEBUG >2 and warn "Import_called initially set to: $_import_called\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 2 and warn "Import_called initially set to: $_import_called\n" 0 26 26
118  
119 FILTER_ONLY
120 code_no_comments => \&filter,
121 28 ▶ all => sub { $LOGGING and print SOURCE };
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$LOGGING and print SOURCE $_ 0 28 28
122  
123 26 ▶$DEBUG >2 and warn "Import_called set to: $_import_called\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 2 and warn "Import_called set to: $_import_called\n" 0 26 26
124 26 ▶$DEBUG >2 and $Shell::POSIX::Select::_testmode and warn "testmode is $Shell::POSIX::Select::_testmode";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 2 and $_testmode and warn "testmode is $_testmode" 0 26 26
Condition33%1/3
$DEBUG > 2 and $_testmode
!ll&&!rl&&r
2600
MC/DC0%0/2
$DEBUG > 2 and $_testmode
$DEBUG > 2 $_testmode
Truth table33%1/3
A and B
A= $DEBUG > 2B= $_testmode
ABresult
0X0
100
111
125  
126 26 ▶use re 'eval';
Subroutine BEGIN: called CC=1 SCAR=0.0
127  
128 # Scope for declaration of pre-compiled REs:
129 {
130 26 my $RE_kw1 = qr^
131 (\bselect\b)
132 ^x; # extended-syntax, allowing comments, etc.
133  
134 26 my $RE_kw2 = qr^
135 \G(\bselect\b)
136 ^x; # extended-syntax, allowing comments, etc.
137  
138 26 my $RE_decl = qr^
139 (\s*
140 # grab declarator if there
141 (?: \b my \b| \b local \b| \b our \b )
142 \s*)
143 ^x; # extended-syntax, allowing comments, etc.
144  
145 26 my $RE_kw_and_decl = qr^
146 \bselect\b
147 \s*
148 ( # Next, grab optional declarator and varname if there
149 (?: \b my \b| \b local \b| \b our \b )?
150 \s*
151 )?
152 ^x; # extended-syntax, allowing comments, etc.
153  
154  
155 26 my $RE_list = qr^
156 \s*
157 (
158 # $RE{balanced}{-parens=>'()'}
159 )
160 ^x; # extended-syntax, allowing comments, etc.
161  
162 26 my $RE_block = qr^
163 \s*
164 # Is following really beneficial/necessary? I think I needed it in one case - tfm
165 (?= { ) # ensure opposite of } comes next
166 (
167 # now find the code-block
168 # $RE{balanced}{-parens=>'{}'}
169 )
170 ^x; # extended-syntax, allowing comments, etc.
171  
172 sub matches2fields;
173 sub enloop_codeblock;
174  
175 28 ▶ sub filter {
Subroutine filter: called CC=61 SCAR=53.7
Pod undocumented
176 28 my $subname = sub_name();
177 28 my $last_call = 0;
178 28 my $orig_string=$_;
179 28 my $detect_msg='<unset>';
180  
181 28 ++$::_FILTER_CALLS;
182  
183 28 ▶ $orig_string ne $_ and die "$_ got trashed";
Branch: 1 branch
BranchTrueFalseTotal
$orig_string ne $_ and die "$_ got trashed" 0 28 28
184  
185 #/(..)/ and warn "Matched chars: '$1'\n"; # prime the pos marker
186  
187 28 my $loopnum;
188 # Probably looping out of control if we get this many:
189 28 my $maxloops = 25;
190  
191 28 my $first_celador;
192 28 ▶ if ( $last_call = ($_ eq "") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_call = $_ eq '') { } 0 28 28
193 0 return undef ;
194 }
195 else {
196 # TIMJI: Revisit; why is following the default?
197 28 $detect_msg="SELECT LOOP DETECTED";
198 28 ▶ $orig_string ne $_ and die "$_ got trashed";
Branch: 1 branch
BranchTrueFalseTotal
$orig_string ne $_ and die "$_ got trashed" 0 28 28
199  
200  
201 28 ▶ $DEBUG > 1 and show_subs("****** Pre-Pre-WHILE ****** \n","");
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 1 and show_subs("****** Pre-Pre-WHILE ****** \n", "") 0 28 28
202 28 ▶ $DEBUG > 1 and $LOGGING and print LOG "\$_ is '$_'\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 1 and $LOGGING and print LOG "\$_ is '${_}'\n" 0 28 28
Condition33%1/3
$DEBUG > 1 and $LOGGING
!ll&&!rl&&r
2800
MC/DC0%0/2
$DEBUG > 1 and $LOGGING
$DEBUG > 1 $LOGGING
Truth table33%1/3
A and B
A= $DEBUG > 1B= $LOGGING
ABresult
0X0
100
111
203  
204 28 $loopnum=0;
205 28 ▶ $DEBUG > 1 and show_subs("****** Pre-WHILE ****** \n","");
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 1 and show_subs("****** Pre-WHILE ****** \n", "") 0 28 28
206  
207 28 ▶ while (++$loopnum <= $maxloops) { # keep looping until we can't find any more select loops
Branch: 1 branch
BranchTrueFalseTotal
if (++$loopnum <= $maxloops) 65 0 65
208  
209 65 ▶ $loopnum == 2 and $first_celador=$_;
Branch: 1 branch
BranchTrueFalseTotal
$loopnum == 2 and $first_celador = $_ 22 43 65
210  
211 65 ▶ $DEBUG > 1 and show_subs("****** LOOKING FOR LOOP ****** #$loopnum\n","");
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 1 and show_subs("****** LOOKING FOR LOOP ****** #$loopnum\n", "") 0 65 65
212 65 ▶ $loopnum > 25 and warn "$subname: Might be stuck in loop\n";
Branch: 1 branch
BranchTrueFalseTotal
$loopnum > 25 and warn "${subname}: Might be stuck in loop\n" 0 65 65
213 65 ▶ $loopnum > 100 and die "$subname: Probably was stuck in loop\n";
Branch: 1 branch
BranchTrueFalseTotal
$loopnum > 100 and die "${subname}: Probably was stuck in loop\n" 0 65 65
214 65 ▶ $DEBUG > 3 and pos() and warn "pos is currently: ", pos(), "\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 3 and pos $_ and warn "pos is currently: ", pos $_, "\n" 0 65 65
Condition33%1/3
$DEBUG > 3 and pos $_
!ll&&!rl&&r
6500
MC/DC0%0/2
$DEBUG > 3 and pos $_
$DEBUG > 3 pos $_
Truth table33%1/3
A and B
A= $DEBUG > 3B= pos $_
ABresult
0X0
100
111
215 65 pos()=0;
216 65 ▶ /\S/ or $LOGGING and
Branch: 1 branch
BranchTrueFalseTotal
/\S/ or $LOGGING and print LOG "\$_ is all white space or else empty\n" 65 0 65
Condition20%1/5
/\S/ || $LOGGING && print(LOG "\$_ is all white space or else empty\n")
l!l
650
$LOGGING and print LOG "\$_ is all white space or else empty\n"
!ll&&!rl&&r
000
MC/DC0%0/3
/\S/ || $LOGGING && print(LOG "\$_ is all white space or else empty\n")
/\S/ $LOGGING print LOG "\$_ is all white space or else empty\n"
Truth table0%0/4
A || B and C
A= /\S/B= $LOGGINGC= print LOG "\$_ is all white space or else empty\n"
ABCresult
1XX1
0111
00X0
0100
217 print LOG "\$_ is all white space or else empty\n";
218  
219 # /(..)/ and warn "Matched chars: '$1'\n"; # prime the pos marker
220  
221 65 my ($matched, $can_rewrite) = 0;
222 65 ▶ if ($select2foreach) {
Branch: 1 branch
BranchTrueFalseTotal
if ($select2foreach) { } 0 65 65
223 # simple conversion, for debugging basic ops
224 # change one word, and select loops with all pieces
225 # present are magically rendered syntactically acceptable
226 # NOTE: will break select() usage!
227 0 ▶ s/\bselect\b/foreach /g and $matched = -1;
Branch: 1 branch
BranchTrueFalseTotal
s/\bselect\b/foreach /g and $matched = -1 0 0 0
228 # All these can be handled in one pass, so exit loop
229 0 goto FILTER_EXIT;
230 }
231 else {
232 65 my $pos;
233 65 my ($match, $start_match);
234 65 my ($got_kw,$got_decl, $got_loop_var, $got_list, $got_codeblock);
235 65 my $iteration=0;
236 68 FIND_LOOP:
237 my ($loop_var, $loop_decl, $loop_list, $loop_block)= ("" x 3);
238  
239 68 ▶ $DEBUG_FILT > 0 and warn "Pos initially at ", pos($_), "\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG_FILT > 0 and warn "Pos initially at ", pos $_, "\n" 0 68 68
240  
241 68 ▶ !defined pos() and warn "AT FIND_LOOP, POS IS UNDEF\n";
Branch: 1 branch
BranchTrueFalseTotal
defined pos $_ or warn "AT FIND_LOOP, POS IS UNDEF\n" 68 0 68
242  
243 68 $match=$got_kw=$got_decl=$got_loop_var=$got_list=$got_codeblock="";
244 68 my $matched=0; # means, currently no detected loops that still need replacement
245  
246 # my $RE = ( $loopnum == 1 ? $RE_kw1 : $RE_kw2 ) ; # second version uses \G
247 68 my $RE = $RE_kw1 ; # always restart from the beginning, of incrementally modified program
248 # Same pattern good now, since pos() will have been reset by mod
249 # my $RE = ( $loopnum == 1 ? $RE_kw1 : $RE_kw1 ) ; # second version uses \G
250 68 ▶ if ( m/$RE/g ) { # try to match keyword, "select"
Branch: 1 branch
BranchTrueFalseTotal
if (/$RE/g) { } 40 28 68
251 40 ++$matched ;
252 40 $match=$1;
253 40 $start_match=pos() - length $1;
254 40 $got_kw=1;
255 40 ▶ $DEBUG_FILT > 1 and show_progress($match, pos(), $_);
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG_FILT > 1 and show_progress($match, pos $_, $_) 0 40 40
256 }
257 else {
258 # no more select keywords to process! # LOOP EXIT #1
259 28 goto FILTER_EXIT;
260 }
261  
262 40 $pos=pos(); # remember position
263  
264 40 ▶ if (/\G$RE_decl/g) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G$RE_decl/g) { } 11 29 40
265 11 ++$matched ;
266 11 $loop_decl=$1;
267 11 $match.=" $1";
268 11 $got_decl=1;
269 }
270 else {
271 29 pos()=$pos; # reset to where we left off
272 }
273 40 ▶ $DEBUG_FILT > 1 and show_progress($match, pos(), $_);
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG_FILT > 1 and show_progress($match, pos $_, $_) 0 40 40
274  
275 40 my @rest;
276 40 ▶ $DEBUG_FILT > 0 and warn "POS before ext-var is now ", pos(), "\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG_FILT > 0 and warn "POS before ext-var is now ", pos $_, "\n" 0 40 40
277  
278 40 ( $loop_var, @rest ) = extract_variable( $_ );
279 40 ▶ $DEBUG_FILT > 0 and show_subs( "POST- ext-var string is: ", $_, pos(),19);
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG_FILT > 0 and show_subs("POST- ext-var string is: ", $_, pos $_, 19) 0 40 40
280  
281 40 ▶ $DEBUG_FILT > 0 and warn "POS after ext-var is now ", pos(), "\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG_FILT > 0 and warn "POS after ext-var is now ", pos $_, "\n" 0 40 40
282  
283 40 ▶ if (defined $loop_var and $loop_var ne "" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $loop_var and $loop_var ne '') { } 26 14 40
Condition66%2/3
defined $loop_var and $loop_var ne ""
!ll&&!rl&&r
14026
MC/DC50%1/2
defined $loop_var and $loop_var ne ""
defined $loop_var $loop_var ne ""
Truth table66%2/3
A and B
A= defined $loop_varB= $loop_var ne ""
ABresult
0X0
100
111
284 26 $got_loop_var=1;
285 26 ▶ $DEBUG_FILT > 0 and warn "Got_Loop_Var matched '$loop_var'\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG_FILT > 0 and warn "Got_Loop_Var matched '${loop_var}'\n" 0 26 26
286 26 $match.=" $loop_var";
287 }
288 else {
289 14 pos()=$pos; # reset to where we left off
290 14 ▶ $DEBUG_FILT > 0 and warn "extract_variable failed to match\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG_FILT > 0 and warn "extract_variable failed to match\n" 0 14 14
291 }
292 40 ▶ $DEBUG_FILT > 1 and show_progress($match, pos(), $_);
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG_FILT > 1 and show_progress($match, pos $_, $_) 0 40 40
293  
294 40 gobble_spaces();
295  
296 # $DEBUG_FILT > 0 and warn "Pre-extract_bracketed ()\n";
297 40 ( $loop_list, @rest ) = extract_bracketed($_, '()');
298 40 ▶ if (defined $loop_list and $loop_list ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $loop_list and $loop_list ne '') { } 40 0 40
Condition33%1/3
defined $loop_list and $loop_list ne ""
!ll&&!rl&&r
0040
MC/DC0%0/2
defined $loop_list and $loop_list ne ""
defined $loop_list $loop_list ne ""
Truth table33%1/3
A and B
A= defined $loop_listB= $loop_list ne ""
ABresult
0X0
100
111
299 40 ++$matched;
300 40 $got_list=1;
301 40 $match.=" $loop_list";
302 40 ▶ $DEBUG_FILT > 1 and show_progress($match, pos(), $_);
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG_FILT > 1 and show_progress($match, pos $_, $_) 0 40 40
303 }
304 else { # no loop list; not our kind of select
305 # warn "extract_bracketed failed to match\n";
306 # If we didn't find loop var, they're probably using
307 # select() function or syscall, not select loop
308 0 ▶ if ($got_loop_var) {
Branch: 1 branch
BranchTrueFalseTotal
if ($got_loop_var) { } 0 0 0
309 0 ▶ $DEBUG_FILT > 3 and
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG_FILT > 3 and warn "${PKG}: Found keyword and loop variable, but no ( LIST )!\n" 0 0 0
310 warn "$PKG: Found keyword and loop variable, but no ( LIST )!\n",
311 ;
312 # "If { } really there, try placing 'no $PKG;' after loop to fix.\n";
313 }
314 else {
315 0 ▶ $DEBUG_FILT > 3 and warn "$PKG: Found keyword, but no ( LIST )\n",
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG_FILT > 3 and warn "${PKG}: Found keyword, but no ( LIST )\n", "Must be some other use of the word\n" 0 0 0
316 "Must be some other use of the word\n";
317 }
318 0 ▶ $DEBUG_FILT > 0 and warn "giving up on this match; scanning for next keyword (1)";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG_FILT > 0 and warn "giving up on this match; scanning for next keyword (1)" 0 0 0
319 0 ▶ if (++$iteration < $maxloops) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$iteration < $maxloops) { } 0 0 0
320 0 goto FIND_LOOP;
321 }
322 else {
323 0 _DIE "$PKG: Maximum iterations reached while looking for select loop #$loopnum";
324 }
325 } # else
326  
327 40 gobble_spaces();
328  
329 # $DEBUG > 1 and warn " DDD sending to extract_bracketed() ===$_===\n";
330 40 ( $loop_block, @rest ) = extract_bracketed($_, '{}');
331 # $DEBUG > 1 and warn " DDD extract_bracketed returned ===$loop_block===\n";
332 40 ▶ if (defined $loop_block and $loop_block ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $loop_block and $loop_block ne '') { } 37 3 40
Condition66%2/3
defined $loop_block and $loop_block ne ""
!ll&&!rl&&r
3037
MC/DC50%1/2
defined $loop_block and $loop_block ne ""
defined $loop_block $loop_block ne ""
Truth table66%2/3
A and B
A= defined $loop_blockB= $loop_block ne ""
ABresult
0X0
100
111
333 37 ++$matched;
334 37 $got_codeblock=1;
335 37 $match.=" $loop_block";
336 37 ▶ $DEBUG_FILT > 1 and show_progress($match, pos(), $_);
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG_FILT > 1 and show_progress($match, pos $_, $_) 0 37 37
337 }
338 else {
339 # if $var there, can't possibly be select syscall or function use,
340 # so 100% sure there's a problem
341  
342 3 ▶ if ($got_loop_var) {
Branch: 1 branch
BranchTrueFalseTotal
if ($got_loop_var) { } 0 3 3
343 0 warn "$PKG: Found loop variable and list, but no code-block!\n",
344 ;
345 # "If { } really there, try placing 'no $PKG;' after loop to fix.\n";
346 }
347 else {
348 3 ▶ $DEBUG_FILT > 3 and warn "$PKG: Found keyword and list,",
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG_FILT > 3 and warn "${PKG}: Found keyword and list,", " but no code-block\n", "Must be some other use of the word\n" 0 3 3
349 " but no code-block\n",
350 "Must be some other use of the word\n";
351 }
352 3 ▶ $DEBUG_FILT > 0 and warn "giving up on this match; scanning for next keyword (2)";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG_FILT > 0 and warn "giving up on this match; scanning for next keyword (2)" 0 3 3
353 3 goto FIND_LOOP;
354 }
355  
356 # and print "list_and_block matched '$&'\n";
357 # defined $& and $match.=$&;
358 # defined $& and $match.="$1 $2";
359 #defined $& and ($loop_list, $loop_block) = ($1, $2);
360  
361 37 my $end_match;
362 37 ▶ if ( $matched == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matched == 0) { } 0 37 37
363 0 die" Can it ever get here?";
364 0 goto FILTER_EXIT;
365 }
366 else {
367 37 $end_match=pos();
368 37 $detect_msg='<unset>';
369 37 ▶ if ( $matched == 1 ) { # means "select" keyword only
Branch: 1 branch
BranchTrueFalseTotal
if ($matched == 1) 0 37 37
370 ;
371 }
372 37 ▶ if ( $matched == 2 ) { # means "select" plus decl, var, list, or block
Branch: 2 branches
BranchTrueFalseTotal
if ($matched == 2) { } 0 37 37
elsif ($matched >= 3) { } 37 0 37
373 0 $detect_msg="select loop incomplete; ";
374 0 ▶ $got_list or $detect_msg.= "no (LIST) detected\n";
Branch: 1 branch
BranchTrueFalseTotal
$got_list or $detect_msg .= "no (LIST) detected\n" 0 0 0
375 0 ▶ $got_codeblock or $detect_msg.= "no {CODE} detected\n";
Branch: 1 branch
BranchTrueFalseTotal
$got_codeblock or $detect_msg .= "no {CODE} detected\n" 0 0 0
376 }
377 elsif ( $matched >= 3 ) {
378 }
379 }
380  
381 # print "Entire match: $match\n";
382 # print "Matched Text: ",
383 # substr $_, $start_match,
384 # $end_match-$start_match;
385  
386 37 ▶ if ( $matched > 1 ) { # 1 just means select->foreach conversion
Branch: 1 branch
BranchTrueFalseTotal
if ($matched > 1) 37 0 37
387 37 $::_LOOP_COUNT++; # counts # detected select-loops
388 37 ▶ $DEBUG > 0 and
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 0 and warn "${PKG}: Set debug to: $DEBUG\n" 0 37 37
389 warn "$PKG: Set debug to: $Shell::POSIX::Select::DEBUG\n";
390 }
391  
392 # $can_rewrite indicates whether we matched the crucial
393 # parts that allow replacement of the input -- the list and codeblock
394 # If we got both, the $can_rewrite var shows true now
395 37 ▶ $can_rewrite = $matched >= 2 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$matched >= 2 ? : 37 0 37
396  
397 # $DEBUG > 1 and warn "Calling MATCHES2FIELDS with \$loop_list of $loop_list\n";
398 # $DEBUG > 1 and warn "Calling MATCHES2FIELDS with \$loop_block of ===$loop_block===\n";
399 37 ▶ if ($can_rewrite) {
Branch: 1 branch
BranchTrueFalseTotal
if ($can_rewrite) 37 0 37
400 37 my $replacer = enloop_codeblock
401 matches2fields ( $loop_decl,
402 $loop_var,
403 $loop_list,
404 $loop_block ),
405 $::_LOOP_COUNT;
406
407 37 substr($_, $start_match, ($end_match-$start_match), $replacer );
408 # print "\n\nModified \$_ is: \n$_\n";
409 }
410 }
411 } # end while
412 continue {
413 37 ▶ $DEBUG_FILT > 2 and warn "CONTINUING FIND_LOOP\n" ;
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG_FILT > 2 and warn "CONTINUING FIND_LOOP\n" 0 37 37
414 }
415 #warn "Leaving $subname 1 \n";
416 } # else
417 FILTER_EXIT:
418 # $Shell::POSIX::Select::filter_output="PRE-LOADING DUMP VAR, loopnum was $loopnum";
419 28 if (
420 0 # and $DEBUG or $Shell::POSIX::Select::dump_data
421 ) {
422 # print TTY "$detect_msg\nCode 222\n" ;
423 # print TTY "Code 222\n" ;
424 if ($loopnum == 1 and
425 $detect_msg !~ /SELECT LOOP DETECTED/ ) {
426 # $DEBUG and print STDERR "copacetic\n";
427 # exit 222;
428 # We still need to run the program!
429 }
430 else {
431 $DEBUG >2 and print TTY "LOOP DETECTED: $detect_msg\n"; exit 222;
432 }
433 } # if 0
434  
435 28 ▶ $loopnum > 0 and $Shell::POSIX::Select::filter_output=$_;
Branch: 1 branch
BranchTrueFalseTotal
$loopnum > 0 and $filter_output = $_ 28 0 28
436 # Restore original string-like parts of the code:
437 28 $Shell::POSIX::Select::filter_output =~ s/$Filter::Simple::placeholder/${$Filter::Simple::components[unpack('N',$1)]}/ge;
438 28 ▶ $LOGGING and print USERPROG $_; # $_ unset 2nd call; label starts below
Branch: 1 branch
BranchTrueFalseTotal
$LOGGING and print USERPROG $_ 0 28 28
439 28 ▶ $DEBUG_FILT > 2 and _WARN "Leaving $subname on call #$::_FILTER_CALLS\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG_FILT > 2 and _WARN("Leaving $subname on call #$main::_FILTER_CALLS\n") 0 28 28
440 } # end sub filter
441 } # Scope for declaration of filters' REs
442  
443  
444 26 ▶sub show_progress {
Subroutine show_progress: not called CC=4 SCAR=28.4
Pod undocumented
445 0 my $subname = sub_name();
446  
447  
448 0 my ($match, $pos, $string) = @_;
449  
450 0 ▶ ! defined $match or $match eq "" and warn "$subname: \$match is empty\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $match and ($match eq "" and warn "${subname}: \$match is empty\n") 0 0 0
Condition0%0/5
defined $match && ($match eq '' && warn("${subname}: \$match is empty\n"))
!ll&&!rl&&r
000
$match eq "" and warn "${subname}: \$match is empty\n"
!ll
00
MC/DC0%0/2
defined $match && ($match eq '' && warn("${subname}: \$match is empty\n"))
defined $match $match eq ""
Truth table0%0/3
A && B
A= defined $matchB= $match eq ""
ABresult
0X0
100
111
451 0 show_subs( "Match so far: ", $match, 0, 99);
452 0 ▶ defined $pos and warn "POS is now $pos\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $pos and warn "POS is now $pos\n" 0 0 0
453 0 show_subs( "Remaining string: ", $string, $pos, 19);
454 } # show_progress
455  
456 0 ▶sub show_context {
Subroutine show_context: not called CC=2 SCAR=17.9
Pod undocumented
457 0 my $subname = sub_name();
458  
459  
460 0 my ($left, $match, $right) = @_;
461  
462 0 ▶ $DEBUG > 0 and warn "left/match/right: $left/$match/$right";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 0 and warn "left/match/right: $left/$match/$right" 0 0 0
463  
464 0 show_subs( "Left is", $left, -10);
465 0 show_subs( "Right is", $right, 0, 10);
466 } # show_context
467  
468 # Following sub converts matched elements of users source into the
469 # fields we need: declaration (optional), loop_varname (optional), codeblock
470  
471 37 ▶sub matches2fields {
Subroutine matches2fields: called CC=31 SCAR=46.8
Pod undocumented
472 37 my $subname = sub_name();
473 37 my $default_loopvar = 0;
474  
475  
476 37 my ( $debugging_code, $codeblock2, );
477 37 my ( $decl, $loop_var, $values, $codeblock, $fullmatch ) = @_;
478  
479 37 $debugging_code = "";
480 37 ▶ if ($U_DEBUG > 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($U_DEBUG > 3) 0 37 37
481 0 $debugging_code = "\n# USER-MODE DEBUGGING CODE STARTS HERE\n";
482 0 $debugging_code .=
483 '; $,="/"; warn "Caller is now: ", (caller 0), "\n";';
484 0 $debugging_code .=
485 'warn "Caller 3 is now: ", ((caller 0)[3]), "\n";';
486 0 $debugging_code .= 'warn "\@_ is: @_\n";';
487 0 $debugging_code .= 'warn "\@ARGV is: @ARGV\n";';
488  
489 # $debugging_code .=
490 # 'warn "\@looplist is : @Shell::POSIX::Select::looplist\n"';
491 0 $debugging_code .= "# USER-MODE DEBUGGING CODE ENDS HERE\n\n";
492  
493 0 $debugging_code .= "";
494 }
495  
496 37 ▶ if ( !defined $values or $values =~ /^\s*\(\s*\)\s*$/ ) { # ( ) is legit syntax
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $values or $values =~ /^\s*\(\s*\)\s*$/) 2 35 37
Condition66%2/3
not defined $values or $values =~ /^\s*\(\s*\)\s*$/
l!l&&r!l&&!r
0235
MC/DC50%1/2
not defined $values or $values =~ /^\s*\(\s*\)\s*$/
not defined $values $values =~ /^\s*\(\s*\)\s*$/
Truth table66%2/3
not(A) or B
A= not defined $valuesB= $values =~ /^\s*\(\s*\)\s*$/
ABresult
1X1
011
000
497 # warn "values is undef or vacant";
498 # Code to let user prog figure out if select loop is in sub,
499 # and if so, selects @_ for default LIST
500 2 $values = # supply appropriate default list, depending on programmer's context
501 'defined ((( caller 0 )[3]) and ' .
502 ' (( caller 0 )[3]) ne "") ? @_ : @ARGV '
503 ;
504 }
505  
506  
507 37 ▶ if ( defined $decl and $decl ne "" and
Branch: 3 branches
BranchTrueFalseTotal
if (defined $decl and $decl ne '' and defined $loop_var and $loop_var ne '') { } 11 26 37
elsif (defined $decl and $decl ne '' and !defined($loop_var) || $loop_var eq '') { } 0 26 26
elsif (defined $loop_var and $loop_var ne '' and !defined($decl) || $decl eq '') { } 15 11 26
Condition66%6/9
defined $decl and $decl ne "" and defined $loop_var and $loop_var ne ""
!ll&&!rl&&r
26011
defined $decl and $decl ne "" and defined $loop_var
!ll&&!rl&&r
26011
defined $decl and $decl ne ""
!ll&&!rl&&r
26011
MC/DC25%1/4
defined $decl and $decl ne "" and defined $loop_var and $loop_var ne ""
defined $decl $decl ne "" defined $loop_var $loop_var ne ""
Truth table40%2/5
A and B and C and D
A= defined $declB= $decl ne ""C= defined $loop_varD= $loop_var ne ""
ABCDresult
0XXX0
10XX0
110X0
11100
11111
508 defined $loop_var and $loop_var ne "" ) {
509 11 ▶ $LOGGING and print LOG
Branch: 1 branch
BranchTrueFalseTotal
$LOGGING and print LOG "LOOP: Two-part declaration,", " scoper is: $decl, varname is $loop_var\n" 0 11 11
510 "LOOP: Two-part declaration,",
511 " scoper is: $decl, varname is $loop_var\n";
512 }
513 ▶ elsif ( defined $decl and $decl ne "" and
Condition22%2/9
defined $decl and $decl ne "" and !defined($loop_var) || $loop_var eq ""
!ll&&!rl&&r
2600
defined $decl and $decl ne ""
!ll&&!rl&&r
2600
not defined $loop_var or $loop_var eq ""
l!l&&r!l&&!r
000
MC/DC0%0/4
defined $decl and $decl ne "" and !defined($loop_var) || $loop_var eq ""
defined $decl $decl ne "" not defined $loop_var $loop_var eq ""
Truth table20%1/5
A and B and not(C) or D
A= defined $declB= $decl ne ""C= not defined $loop_varD= $loop_var eq ""
ABCDresult
0XXX0
10XX0
11000
111X1
11011
514 (! defined $loop_var or $loop_var eq "") ) {
515 0 ▶ $LOGGING and print LOG
Branch: 1 branch
BranchTrueFalseTotal
$LOGGING and print LOG "LOOP: Declaration without variable name: $decl" 0 0 0
516 "LOOP: Declaration without variable name: $decl" ;
517 0 warn "$PKG: variable declarator ($decl) provided without variable name\n";
518 0 warn "giving up on this match; scanning for next keyword (3)";
519 0 goto FIND_LOOP;
520 }
521 ▶ elsif ( defined $loop_var and $loop_var ne "" and
Condition55%5/9
defined $loop_var and $loop_var ne "" and !defined($decl) || $decl eq ""
!ll&&!rl&&r
11015
defined $loop_var and $loop_var ne ""
!ll&&!rl&&r
11015
not defined $decl or $decl eq ""
l!l&&r!l&&!r
1500
MC/DC25%1/4
defined $loop_var and $loop_var ne "" and !defined($decl) || $decl eq ""
defined $loop_var $loop_var ne "" not defined $decl $decl eq ""
Truth table40%2/5
A and B and not(C) or D
A= defined $loop_varB= $loop_var ne ""C= not defined $declD= $decl eq ""
ABCDresult
0XXX0
10XX0
11000
111X1
11011
522 (! defined $decl or $decl eq "") ) {
523 15 ▶ $LOGGING and print LOG
Branch: 1 branch
BranchTrueFalseTotal
$LOGGING and print LOG "LOOP: Variable without declaration (okay): $loop_var" 0 15 15
524 "LOOP: Variable without declaration (okay): $loop_var"
525 }
526 else {
527 11 ▶ $LOGGING and print LOG "LOOP: zero-word declaration\n";
Branch: 1 branch
BranchTrueFalseTotal
$LOGGING and print LOG "LOOP: zero-word declaration\n" 0 11 11
528  
529 11 my $default_loopvar = 1;
530 11 ($decl, $loop_var) = qw (local $_); # default loop var; package scope
531 }
532  
533 37 ▶ if ( !defined $codeblock or $codeblock =~ /^\s*{\s*}\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $codeblock or $codeblock =~ /^\s*{\s*}\s*$/) 2 35 37
Condition66%2/3
not defined $codeblock or $codeblock =~ /^\s*{\s*}\s*$/
l!l&&r!l&&!r
0235
MC/DC50%1/2
not defined $codeblock or $codeblock =~ /^\s*{\s*}\s*$/
not defined $codeblock $codeblock =~ /^\s*{\s*}\s*$/
Truth table66%2/3
not(A) or B
A= not defined $codeblockB= $codeblock =~ /^\s*{\s*}\s*$/
ABresult
1X1
011
000
534 # default codeblock prints the selection; good for grep()-like filtering
535 # NOTE: Following string must start/end with {}
536 2 $codeblock = "{
537 print \"$loop_var\\n\" ; # ** USING DEFAULT CODEBLOCK **
538 }";
539 }
540  
541 # I've already extracted what could be a valid variable name,
542 # but the regex was kinda sleazy, so it's time to validate
543 # it using TEXT::BALANCED::extract_variable()
544 # But I found a bug, it rejects $::var*, so exempt that form from check
545  
546 37 ▶ unless ($default_loopvar or $loop_var =~ /^\$::\w+/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($default_loopvar or $loop_var =~ /^\$::\w+/) { } 0 37 37
Condition33%1/3
$default_loopvar or $loop_var =~ /^\$::\w+/
l!l&&r!l&&!r
0037
MC/DC0%0/2
$default_loopvar or $loop_var =~ /^\$::\w+/
$default_loopvar $loop_var =~ /^\$::\w+/
Truth table33%1/3
A or B
A= $default_loopvarB= $loop_var =~ /^\$::\w+/
ABresult
1X1
011
000
547 # don't check if I inserted it myself, or is in form $::stuff,
548 # which extract_variable() doesn't properly extract
549 37 ▶ $DEBUG > 1 and warn "Pre-extract_variable 3\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 1 and warn "Pre-extract_variable 3\n" 0 37 37
550 # Now let's see if Damian likes it:
551 37 my ( $loop_var2, @rest ) = extract_variable($loop_var);
552 37 ▶ if ( $loop_var2 ne $loop_var ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($loop_var2 ne $loop_var) 0 37 37
553 0 ▶ $DEBUG > 1 and
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 1 and warn "${PKG}: extracted var diff from parsed var: ", $DEBUG > 0 and warn "${PKG}: varname for select loop failed validation", " #$main::_LOOP_COUNT: $loop_var\n" 0 0 0
Condition0%0/2
$DEBUG > 1 and warn "${PKG}: extracted var diff from parsed var: ", $DEBUG > 0
!ll
00
MC/DC0%0/1
$DEBUG > 1 and warn "${PKG}: extracted var diff from parsed var: ", $DEBUG > 0
$DEBUG > 1
Truth table0%0/2
A
A= $DEBUG > 1
Aresult
00
11
554 warn "$PKG: extracted var diff from parsed var: ",
555 $DEBUG > 0 and warn
556 "$PKG: varname for select loop failed validation",
557 " #$::_LOOP_COUNT: $loop_var\n";
558 }
559 }
560 else {
561 ;
562 }
563  
564 37 ▶ !defined $decl and $decl = "";
Branch: 1 branch
BranchTrueFalseTotal
defined $decl or $decl = "" 22 15 37
565 # okay for this to be empty string; means user wants it global, or
566 # declared it before loop
567  
568 # make version of \$codeblock without curlies at either end
569 37 ( $codeblock2 = $codeblock ) =~ s/\A\s*\{\s*|\s*\}\s*\z//g;
570  
571 37 ▶ defined $decl and $decl eq 'unset' and undef $decl; # pass as undef
Branch: 1 branch
BranchTrueFalseTotal
defined $decl and $decl eq "unset" and undef $decl 0 37 37
Condition33%1/3
defined $decl and $decl eq "unset"
!ll&&!rl&&r
0370
MC/DC0%0/2
defined $decl and $decl eq "unset"
defined $decl $decl eq "unset"
Truth table33%1/3
A and B
A= defined $declB= $decl eq "unset"
ABresult
0X0
100
111
572 # $DEBUG > 1 and warn " DDD matches2fields() is returning codeblock2 ===$codeblock2===\n";
573 37 return ( $decl, $loop_var, $values, $codeblock2, $debugging_code );
574 } # matches2fields
575  
576 37 ▶sub enloop_codeblock {
Subroutine enloop_codeblock: called CC=18 SCAR=42.2
Pod undocumented
577 # Wraps code implementing select-loop around user-supplied codeblock
578 37 my $subname = sub_name();
579  
580 37 $Shell::POSIX::Select::_ENLOOP_CALL_COUNT++;
581  
582 37 my ( $decl, $loop_var, $values, $codestring, $dcode, $loopnum ) = @_;
583  
584 37 ▶ (defined $values and $values ne "") or do {
Branch: 1 branch
BranchTrueFalseTotal
defined $values and $values ne "" or do { $DEBUG > 1 and _WARN("NO VALUES! Using dummy ones"); $values = "( dummy1, dummy2 )" } 37 0 37
Condition33%1/3
defined $values and $values ne ""
!ll&&!rl&&r
0037
MC/DC0%0/2
defined $values and $values ne ""
defined $values $values ne ""
Truth table33%1/3
A and B
A= defined $valuesB= $values ne ""
ABresult
0X0
100
111
585 0 ▶ $DEBUG > 1 and _WARN "NO VALUES! Using dummy ones";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 1 and _WARN("NO VALUES! Using dummy ones") 0 0 0
586 0 $values = '( dummy1, dummy2 )';
587 };
588  
589 37 ▶ my $declaration =
Branch: 1 branch
BranchTrueFalseTotal
defined $decl && $decl ne '' ? : 22 15 37
Condition66%2/3
defined $decl and $decl ne ""
!ll&&!rl&&r
01522
MC/DC50%1/2
defined $decl and $decl ne ""
defined $decl $decl ne ""
Truth table66%2/3
A and B
A= defined $declB= $decl ne ""
ABresult
0X0
100
111
590 ( defined $decl and $decl ne "" ) ? "$decl $loop_var; " .
591 ' # LOOP-VAR DECLARATION REQUESTED (perhaps by default)' :
592 " ; # NO DECLARATION OF LOOP-VAR REQUESTED";
593  
594 37 my $arrayname = $PKG . '::looplist';
595 37 my $NL = '\n';
596  
597 # Now build the code for the user-prog to run
598 37 my @parts;
599 # Start new scope first, so if user has LOOP: label before select,
600 # it applies to the whole encapsulated loop
601 # wrapper scope needed so user can LABEL: select(), and not *my* label
602 37 push @parts, qq(
603 # Code generated by $PKG v$VERSION, by tim(AT)TeachMePerl.com
604 # NOTA BENE: Line 1 of this segment must start with {, so user can LABEL it
605 { # **** NEW WRAPPER SCOPE FOR SELECTLOOP #$loopnum ****
606 \$${PKG}::DEBUG > 1 and $loopnum == 1 and
607 warn "LINE NUMBER FOR START OF USER CODE_BLOCK IS: ", __LINE__, "\\n";
608 _SEL_LOOP$loopnum: { # **** NEW SCOPE FOR SELECTLOOP #$loopnum ****
609 );
610 # warn " DDD LOGGING is now $LOGGING\n";
611 37 ▶ $LOGGING and (print PART1 $parts[0] or _DIE "failed to write to PART1\n");
Branch: 1 branch
BranchTrueFalseTotal
$LOGGING and print(PART1 $parts[0]) || _DIE("failed to write to PART1\n") 0 37 37
Condition16%1/6
$LOGGING && (print(PART1 $parts[0]) || _DIE("failed to write to PART1\n"))
!ll
3700
print PART1 $parts[0] or _DIE("failed to write to PART1\n")
l!l&&r!l&&!r
000
MC/DC0%0/3
$LOGGING && (print(PART1 $parts[0]) || _DIE("failed to write to PART1\n"))
$LOGGING print PART1 $parts[0] _DIE("failed to write to PART1\n")
Truth table0%0/4
A && B or C
A= $LOGGINGB= print PART1 $parts[0]C= _DIE("failed to write to PART1\n")
ABCresult
0XX0
1000
11X1
1011
612 37 ▶ $DEBUG > 4 and warn "SETTING $arrayname to $values\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 4 and warn "SETTING $arrayname to $values\n" 0 37 37
613  
614 37 push @parts, qq(
615 # critical for values's contents to be resolved in user's scope
616 local \@$arrayname=$values;
617 local \$${PKG}::num_values=\@$arrayname;
618  
619 \$${PKG}::DEBUG > 4 and do {
620 warn "ARRAY VALUES ARE: \@$arrayname\\n";
621 warn "NUM VALUES is \$${PKG}::num_values\\n";
622 warn "user-program debug level is \$${PKG}::U_WARN\\n";
623 };
624 $declaration # loop-var declaration appears here
625 );
626  
627 37 ▶ $LOGGING and (print PART2 $parts[1] or _DIE "failed to write to PART1\n");
Branch: 1 branch
BranchTrueFalseTotal
$LOGGING and print(PART2 $parts[1]) || _DIE("failed to write to PART1\n") 0 37 37
Condition16%1/6
$LOGGING && (print(PART2 $parts[1]) || _DIE("failed to write to PART1\n"))
!ll
3700
print PART2 $parts[1] or _DIE("failed to write to PART1\n")
l!l&&r!l&&!r
000
MC/DC0%0/3
$LOGGING && (print(PART2 $parts[1]) || _DIE("failed to write to PART1\n"))
$LOGGING print PART2 $parts[1] _DIE("failed to write to PART1\n")
Truth table0%0/4
A && B or C
A= $LOGGINGB= print PART2 $parts[1]C= _DIE("failed to write to PART1\n")
ABCresult
0XX0
1000
11X1
1011
628  
629 37 ▶ $DEBUG > 4 and do {
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 4 and do { warn "\$codestring is: $codestring\n"; warn "\$dcode is: '${dcode}'\n"; warn "\$arrayname is: $arrayname\n"; defined $_autoprompt or warn "autoprompt is unset"; defined $codestring or warn "codestring is unset" } 0 37 37
630 0 warn "\$codestring is: $codestring\n";
631 0 warn "\$dcode is: '$dcode'\n";
632 0 warn "\$arrayname is: $arrayname\n";
633 0 ▶ !defined $Shell::POSIX::Select::_autoprompt and warn "autoprompt is unset";
Branch: 1 branch
BranchTrueFalseTotal
defined $_autoprompt or warn "autoprompt is unset" 0 0 0
634 0 ▶ !defined $codestring and warn "codestring is unset";
Branch: 1 branch
BranchTrueFalseTotal
defined $codestring or warn "codestring is unset" 0 0 0
635 };
636  
637 { # local scope for $^W mod
638 # getting one pesky "uninit var" warnings I can't resolve
639 37 local $^W=0;
640 37 push @parts, qq(
641 $dcode;
642 local (
643 \$${PKG}::Prompt[$loopnum],
644 \$${PKG}::menu
645 ) =
646 ${PKG}::make_menu(
647 \$${PKG}::Heading || "",
648 \$${PKG}::Prompt || "" , # Might be overridden in make_menu
649 \@$arrayname
650 );
651  
652 # no point in prompting a pipe!
653 local \$${PKG}::do_prompt[$loopnum] = (-t) ? 1 : 0 ;
654 $DEBUG > 2 and warn "do_prompt is \$${PKG}::do_prompt[$loopnum]\\n";
655 if ( defined \$${PKG}::menu ) { # No list, no iterations!
656 while (1) { # for repeating prompt for selections
657 # localize, so I don't have to reset $Reply for
658 # outer loop on exit from inner
659 local (\$Reply);
660 while (1) { # for validating user's input
661 local \$${PKG}::bad = 0;
662  
663 # local decl suppresses newline on prompt when -l switch turned on
664 {
665 local \$\\;
666 if (\$${PKG}::do_prompt[$loopnum]) {
667  
668 # When transferring from INNER to OUTER loop,
669 # extra NL before prompt is visually desirable
670  
671 if ( \$${PKG}::_extra_nl) {
672 print STDERR "\\n\\n";
673 \$${PKG}::_extra_nl=0;
674 }
675 print STDERR
676 "\$${PKG}::menu$NL$ON\$${PKG}::Prompt[$loopnum]$OFF$BOLD ";
677 }
678 }
679  
680 # \$${PKG}::do_prompt=$Shell::POSIX::Select::_autoprompt;
681 # constant prompting depends on style
682 \$${PKG}::do_prompt[$loopnum]= 0;
683  
684 if ( \$${PKG}::dump_data ) {
685 \$Reply = undef;
686 # dump filtered source for comparison against expected
687 print STDERR "copacetic\n"; # ensure some output, and flush pending
688 exit 222; # code for graceful, expected, early exit
689 }
690 else {
691 # \$^W=0;
692 # warn "Waiting for input";
693 \$Eof=0;
694 \$Reply = <STDIN>;
695 # warn "Got input";
696 # \$^W=1;
697  
698 if ( !defined( \$Reply ) ) {
699 defined "$BOLD" and "$BOLD" ne "" and print STDERR "$SGR0";
700  
701 # need to undef loop var; user may check it!
702 undef $loop_var;
703  
704 # last ${PKG}::_SEL_LOOP$loopnum; # Syntax error!
705 # If returning to outer loop, show the prompt for it
706 # warn "User hit ^D";
707 if ( $loopnum > 1 and -t ) { # reset prompting for outer loop
708 \$${PKG}::do_prompt[$loopnum-1] = 1; \$${PKG}::_extra_nl=1;
709 }
710 $DEBUG > 2 and warn "Lasting out of _SEL_LOOP$loopnum\\n";
711 \$Eof=1;
712 last _SEL_LOOP$loopnum;
713 }
714 !defined \$Reply and die "REPLY accessed, while undefined";
715 chomp \$Reply;
716  
717 # undo emboldening of user input
718 defined "$BOLD" and "$BOLD" ne "" and print STDERR "$SGR0";
719  
720 #print STDERR "\$${PKG}::menu$NL$ON\$${PKG}::Prompt$OFF$BOLD ";
721 if ( \$Reply eq "" ) { # interpreted as re-print menu request
722 # Empty input is legit, means redisplay menu
723 \$${PKG}::U_WARN > 1 and warn "\\tINPUT IS: empty\\n";
724 \$${PKG}::bad = \$${PKG}::do_prompt[$loopnum] = 1;
725 }
726 elsif ( \$Reply =~ /\\D/ ) { # shouldn't be any non-digit!
727 \$${PKG}::U_WARN > 0
728 and warn "\\tINPUT CONTAINS NON-DIGIT: '\$Reply'\\n";
729 \$${PKG}::bad = 1; # Korn and Bash shell just ignore this case
730 }
731 elsif ( \$Reply < 1 or \$Reply > \$${PKG}::num_values ) {
732 \$${PKG}::U_WARN > 0
733 and warn
734 "\\t'\$Reply' IS NOT IN RANGE: 1 - \$${PKG}::num_values\\n";
735 \$${PKG}::bad = 1; # Korn and Bash shell just ignore this case
736 }
737  
738 # warn "BAD is now: \$${PKG}::bad";
739 \$${PKG}::bad or
740 $DEBUG > 2 and warn "About to last out of Reply Validator Loop\n";
741 \$${PKG}::bad or last; # REPLY VALIDATOR EXITED HERE
742 } # if for validating user input
743 } # infinite while for validating user input
744  
745 $loop_var = \$$arrayname\[\$Reply - 1]; # set users' variable
746  
747 # USER'S LOOP-BLOCK BELOW
748 $codestring;
749  
750 # USER'S LOOP-BLOCK ABOVE
751 # Making sure there's colon (maybe
752 # even two) after codestring above,
753 # in case user omitted after last
754 # statement in block. I might add
755 # another statement below it someday!
756 $DEBUG > 2 and warn "At end of prompt-repeating loop \n";
757 } # infinite while for repeating collection of selections
758 $DEBUG and warn "BEYOND end of prompt-repeating loop \n";
759 } # endif (defined \$${PKG}::menu)
760 else {
761 \$${PKG}::DEBUG > 0 and warn "$PKG: Select Loop #$loopnum has no list, so no iterations\\n";
762  
763 if ( \$${PKG}::dump_data ) {
764 \$Reply = undef;
765 # dump filtered source for comparison against expected
766 print STDERR "copacetic\n"; # ensure some output, and flush pending
767 exit 222; # code for graceful, expected, early exit
768 }
769 }
770 # return omitted above, to get last expression's value
771 # returned automatically, just like shell's version
772  
773 ); # push onto parts ender
774 } # local scope for $^W mod
775  
776 37 ▶ $LOGGING and (print PART3 $parts[2] or _DIE "failed to write to PART3\n");
Branch: 1 branch
BranchTrueFalseTotal
$LOGGING and print(PART3 $parts[2]) || _DIE("failed to write to PART3\n") 0 37 37
Condition16%1/6
$LOGGING && (print(PART3 $parts[2]) || _DIE("failed to write to PART3\n"))
!ll
3700
print PART3 $parts[2] or _DIE("failed to write to PART3\n")
l!l&&r!l&&!r
000
MC/DC0%0/3
$LOGGING && (print(PART3 $parts[2]) || _DIE("failed to write to PART3\n"))
$LOGGING print PART3 $parts[2] _DIE("failed to write to PART3\n")
Truth table0%0/4
A && B or C
A= $LOGGINGB= print PART3 $parts[2]C= _DIE("failed to write to PART3\n")
ABCresult
0XX0
1000
11X1
1011
777  
778 37 push @parts, qq(
779 } # **** END NEW SCOPE FOR SELECTLOOP #$loopnum ****
780 } # **** END WRAPPER SCOPE FOR SELECTLOOP #$loopnum ****
781 # vi:ts=2 sw=2:
782 );
783  
784 37 ▶ $LOGGING and (print PART4 $parts[3] or _DIE "failed to write to PART4\n");
Branch: 1 branch
BranchTrueFalseTotal
$LOGGING and print(PART4 $parts[3]) || _DIE("failed to write to PART4\n") 0 37 37
Condition16%1/6
$LOGGING && (print(PART4 $parts[3]) || _DIE("failed to write to PART4\n"))
!ll
3700
print PART4 $parts[3] or _DIE("failed to write to PART4\n")
l!l&&r!l&&!r
000
MC/DC0%0/3
$LOGGING && (print(PART4 $parts[3]) || _DIE("failed to write to PART4\n"))
$LOGGING print PART4 $parts[3] _DIE("failed to write to PART4\n")
Truth table0%0/4
A && B or C
A= $LOGGINGB= print PART4 $parts[3]C= _DIE("failed to write to PART4\n")
ABCresult
0XX0
1000
11X1
1011
785 # Following is portable PART-divider, used to isolate chunk
786 # with unitialized value causing warning
787 # ); push @parts, qq(
788  
789 37 return ( join "", @parts ); # return assembled code, for user to run
790 } # enloop_codeblock
791  
792 22 ▶sub make_menu {
Subroutine make_menu: called CC=27 SCAR=37.8
Pod undocumented
793 22 my $subname = sub_name();
794  
795  
796 # Replacement of empty list by @_ or @ARGV happens in matches2fields
797 # Here we check to see if we got arguments from somewhere
798 # Note that it's not necesssarily an error if there are no values,
799 # that just means we won't do any iterations
800  
801 22 my ($heading) = shift;
802 22 my ($prompt) = shift;
803 22 my (@values) = @_;
804 22 ▶ unless (@values) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@values) 4 18 22
805 4 return ( undef, undef ); # can't make menu out of nothing!
806 }
807 18 my ( $l, $l_length ) = 0;
808 18 my $count = 5;
809 18 my ( $sep, $padding ) = "" x 2;
810 18 my $choice = "";
811  
812  
813 # Find longest string value in selection list
814 18 my $v_length = 0;
815  
816 18 ▶ for ( my $i = 0 ; $i < @values ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @values) 35 18 53
817 35 ▶ ( $l = length $values[$i] ) > $v_length and $v_length = $l;
Branch: 1 branch
BranchTrueFalseTotal
($l = length $values[$i]) > $v_length and $v_length = $l 18 17 35
818 }
819 18 ▶ $DEBUG > 3 and $LOGGING and print LOG "Longest value is $v_length chars\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 3 and $LOGGING and print LOG "Longest value is $v_length chars\n" 0 18 18
Condition33%1/3
$DEBUG > 3 and $LOGGING
!ll&&!rl&&r
1800
MC/DC0%0/2
$DEBUG > 3 and $LOGGING
$DEBUG > 3 $LOGGING
Truth table33%1/3
A and B
A= $DEBUG > 3B= $LOGGING
ABresult
0X0
100
111
820  
821 # Figure out lengths of labels (numbers on menu selections)
822 18 ▶ $DEBUG > 3 and $LOGGING and print LOG "Number of values is ", scalar @values, "\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 3 and $LOGGING and print LOG "Number of values is ", scalar @values, "\n" 0 18 18
Condition33%1/3
$DEBUG > 3 and $LOGGING
!ll&&!rl&&r
1800
MC/DC0%0/2
$DEBUG > 3 and $LOGGING
$DEBUG > 3 $LOGGING
Truth table33%1/3
A and B
A= $DEBUG > 3B= $LOGGING
ABresult
0X0
100
111
823  
824  
825 18 ▶ @values >= 10_000 ? $l_length = 5 :
Branch: 5 branches
BranchTrueFalseTotal
@values >= 10000 ? : 0 18 18
@values >= 1000 ? : 0 18 18
@values >= 100 ? : 0 18 18
@values >= 10 ? : 0 18 18
@values > 0 ? : 18 0 18
826 @values >= 1_000 ? $l_length = 4 :
827 @values >= 100 ? $l_length = 3 :
828 @values >= 10 ? $l_length = 2 :
829 @values > 0 ? $l_length = 1 :
830 undef $l_length;
831  
832 18 ▶ $DEBUG > 3 and $LOGGING and print LOG "Label length is $l_length\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 3 and $LOGGING and print LOG "Label length is $l_length\n" 0 18 18
Condition33%1/3
$DEBUG > 3 and $LOGGING
!ll&&!rl&&r
1800
MC/DC0%0/2
$DEBUG > 3 and $LOGGING
$DEBUG > 3 $LOGGING
Truth table33%1/3
A and B
A= $DEBUG > 3B= $LOGGING
ABresult
0X0
100
111
833  
834 0 ▶ if ( !defined $l_length ) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $l_length) 0 18 18
835  
836 18 $sep = "\040\040";
837 18 my $l_sep = length $sep; # separator 'tween pieces
838  
839 # Figure out how many columns per line we can print
840 # 2 is for :<SP> after label
841  
842 # TIMJI: Convert to using YUMPY's Term::Size::Heuristic here, later on
843  
844 18 my $one_label = ( $l_length + 2 ) + $v_length + $l_sep;
845 18 my $columns = int( $COLS / $one_label );
846 18 ▶ $columns < 1 and $columns = 1;
Branch: 1 branch
BranchTrueFalseTotal
$columns < 1 and $columns = 1 0 18 18
847 # Do not let the number of columns grow beyond the maximum:
848 18 ▶ if ($MaxColumns < $columns)
Branch: 1 branch
BranchTrueFalseTotal
if ($MaxColumns < $columns) 0 18 18
849 {
850 0 $columns = $MaxColumns;
851 } # if
852  
853 # $DEBUG > 3 and
854 #HERE
855 18 ▶$LOGGING and print LOG "T-Cols, Columns, label: $COLS, $columns, $one_label\n";
Branch: 1 branch
BranchTrueFalseTotal
$LOGGING and print LOG "T-Cols, Columns, label: $COLS, $columns, $one_label\n" 0 18 18
856  
857 # Prompt may have been set in import() according to a submitted option;
858 # if so, keep it. If not, use shell's default
859 $prompt =
860 (defined $Shell::POSIX::Select::Prompt and
861 $Shell::POSIX::Select::Prompt ne "") ?
862 $Shell::POSIX::Select::Prompt :
863 defined $ENV{Select_POSIX_Shell_Prompt} ? $ENV{Select_POSIX_Shell_Prompt} :
864 18 ▶ $Shell::POSIX::Select::_default_prompt;
Branch: 2 branches
BranchTrueFalseTotal
defined $Prompt && $Prompt ne '' ? : 4 14 18
defined $ENV{'Select_POSIX_Shell_Prompt'} ? : 0 14 14
Condition66%2/3
defined $Prompt and $Prompt ne ""
!ll&&!rl&&r
1404
MC/DC50%1/2
defined $Prompt and $Prompt ne ""
defined $Prompt $Prompt ne ""
Truth table66%2/3
A and B
A= defined $PromptB= $Prompt ne ""
ABresult
0X0
100
111
865 ;
866  
867 18 ▶ $DEBUG > 3 and $LOGGING and print LOG "Making menu\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 3 and $LOGGING and print LOG "Making menu\n" 0 18 18
Condition33%1/3
$DEBUG > 3 and $LOGGING
!ll&&!rl&&r
1800
MC/DC0%0/2
$DEBUG > 3 and $LOGGING
$DEBUG > 3 $LOGGING
Truth table33%1/3
A and B
A= $DEBUG > 3B= $LOGGING
ABresult
0X0
100
111
868  
869 {
870 18 local $, = "\n";
871 }
872  
873 18 my $menu;
874 18 ▶ $menu = defined $heading ? "${ON}$heading$OFF" : "" ;
Branch: 1 branch
BranchTrueFalseTotal
defined $heading ? : 18 0 18
875 18 $menu.="\n";
876 # $columns == 0 and die "Columns is zero!";
877 18 ▶ for ( my $i = 0, my $j = 1 ; $i < @values ; $i++, $j++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @values) 35 18 53
878 35 $menu .= sprintf "%${l_length}d) %-${v_length}s$sep", $j, $values[$i];
879 35 ▶ $j % $columns or $menu .= sprintf "\n"; # format $count items per line
Branch: 1 branch
BranchTrueFalseTotal
$j % $columns or $menu .= "\n" 35 0 35
880  
881 # For 385 line list:
882 # Illegal modulus zero at /pmods/yumpy/Select/Shell/POSIX/Select.pm line 764.
883  
884 }
885 18 return ( $prompt, $menu );
886 } # make_menu
887  
888 0 ▶sub log_files {
Subroutine log_files: not called CC=16 SCAR=56.1
Pod undocumented
889 0 my $subname = sub_name();
890 0 my ($dir, $sep);
891  
892  
893 0 ▶ if ( $LOGGING == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($LOGGING == 1) { } 0 0 0
elsif ($LOGGING > 1) { } 0 0 0
894 0 $dir = tmpdir();
895 #
896 # USERPROG shows my changes, with control-chars
897 # filling in as placeholders for some pieces. For
898 # debugging purposes, I find it helpful to print that
899 # out ASAP so I have something to look at if the
900 # program bombs out before SOURCE gets written out,
901 # which is the same apart from placeholders being
902 # converted to original data.
903 #
904 0 ▶ $DEBUG > 1 and $LOGGING > 0 and warn "Opening log files\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 1 and $LOGGING > 0 and warn "Opening log files\n" 0 0 0
Condition0%0/3
$DEBUG > 1 and $LOGGING > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$DEBUG > 1 and $LOGGING > 0
$DEBUG > 1 $LOGGING > 0
Truth table0%0/3
A and B
A= $DEBUG > 1B= $LOGGING > 0
ABresult
0X0
100
111
905 0 ▶ open LOG, '>', catfile($dir, 'SELECT_log') or _DIE "Open LOG failed, $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open LOG, ">", catfile($dir, "SELECT_log") or _DIE("Open LOG failed, $!\n") 0 0 0
906 0 ▶ open SOURCE, '>', catfile($dir, 'SELECT_source') or _DIE "Open SOURCE failed, $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open SOURCE, ">", catfile($dir, "SELECT_source") or _DIE("Open SOURCE failed, $!\n") 0 0 0
907 0 ▶ open USERPROG, '>', catfile($dir, 'SELECT_user_program') or _DIE "Open USERPROG failed, $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open USERPROG, ">", catfile($dir, "SELECT_user_program") or _DIE("Open USERPROG failed, $!\n") 0 0 0
908 0 ▶ open PART1, '>', catfile($dir, 'SELECT_part1') or _DIE "Open PART1 failed, $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open PART1, ">", catfile($dir, "SELECT_part1") or _DIE("Open PART1 failed, $!\n") 0 0 0
909 0 ▶ open PART2, '>', catfile($dir, 'SELECT_part2') or _DIE "Open PART2 failed, $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open PART2, ">", catfile($dir, "SELECT_part2") or _DIE("Open PART2 failed, $!\n") 0 0 0
910 0 ▶ open PART3, '>', catfile($dir, 'SELECT_part3') or _DIE "Open PART3 failed, $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open PART3, ">", catfile($dir, "SELECT_part3") or _DIE("Open PART3 failed, $!\n") 0 0 0
911 0 ▶ open PART4, '>', catfile($dir, 'SELECT_part4') or _DIE "Open PART4 failed, $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open PART4, ">", catfile($dir, "SELECT_part4") or _DIE("Open PART4 failed, $!\n") 0 0 0
912 0 $LOGGING++; # to avoid 2nd invocation
913 0 ▶ $DEBUG > 1 and $LOGGING > 0 and warn "Finished with log files\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 1 and $LOGGING > 0 and warn "Finished with log files\n" 0 0 0
Condition0%0/3
$DEBUG > 1 and $LOGGING > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$DEBUG > 1 and $LOGGING > 0
$DEBUG > 1 $LOGGING > 0
Truth table0%0/3
A and B
A= $DEBUG > 1B= $LOGGING > 0
ABresult
0X0
100
111
914 }
915 elsif ($LOGGING > 1) {
916 0 ▶ $DEBUG > 0 and warn "$subname: Logfiles opened previously\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 0 and warn "${subname}: Logfiles opened previously\n" 0 0 0
917 }
918 else {
919 0 ▶ $DEBUG > 0 and warn "$subname: Logfiles not opened\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 0 and warn "${subname}: Logfiles not opened\n" 0 0 0
920 }
921 } # log_files
922  
923 320 ▶sub sub_name {
Subroutine sub_name: called CC=2 SCAR=7.2
Pod undocumented
924 320 my $callers_name = (caller 1)[3] ;
925 320 ▶ if ( ! defined $callers_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $callers_name) { } 0 320 320
926 0 $callers_name='Main_program'; # must be call from main
927 }
928 else {
929 320 $callers_name =~ s/^.*:://; # strip package name
930 320 $callers_name .= '()'; # sub_name -> sub_name()
931 }
932 320 return $callers_name;
933 } # sub_name
934  
935 27 ▶sub _WARN {
Subroutine _WARN: called CC=2 SCAR=7.2
936 27 my $subname = sub_name();
937 27 ▶ $PRODUCTION ? carp(@_) : warn (@_);
Branch: 1 branch
BranchTrueFalseTotal
$PRODUCTION ? : 27 0 27
938 }
939  
940 0 ▶sub _DIE {
Subroutine _DIE: not called CC=3 SCAR=24.8
941 0 my $subname = sub_name();
942 0 ▶ $DEBUG and warn "$0: In _DIE, with PRODUCTION of $PRODUCTION, arg of @_\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG and warn "$0: In _DIE, with PRODUCTION of $PRODUCTION, arg of @_\n" 0 0 0
943 0 ▶ $PRODUCTION ? croak(@_) : die (@_);
Branch: 1 branch
BranchTrueFalseTotal
$PRODUCTION ? : 0 0 0
944 }
945  
946  
947 0 ▶sub ignoring_case { lc $a cmp lc $b }
Subroutine ignoring_case: not called CC=1 SCAR=6.9
Pod undocumented
948  
949 27 ▶sub import {
Subroutine import: called CC=60 SCAR=66.4
950 27 local $_;
951 27 my $subname = sub_name();
952 27 my %import;
953 27 $_import_called++;
954  
955 27 shift; # discard package name
956  
957 27 $Shell::POSIX::Select::U_WARN = $Shell::POSIX::Select::U_WARN_default;
958 27 $Shell::POSIX::Select::_style = $Shell::POSIX::Select::_default_style;
959 # $Shell::POSIX::Select::_prompt =
960 # Prompt is now established in make_menu, during run-time
961 27 $Shell::POSIX::Select::_autoprompt=0;
962  
963 # First, peel off symbols to import, if any
964 # warn "Caller of $subname is ", scalar caller, "\n";
965 27 my $user_pkg=caller;
966 # $DEBUG > 2 and
967 27 ▶for (my $i=0; $i<@_; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 12 26 38
968 12 my $found=0;
969 12 foreach (@EXPORT_OK) { # Handle $Headings, etc.
970 22 ▶ if ($_[$i] eq $_) { $import{$_} = $i; $found++; last; }
Branch: 1 branch
BranchTrueFalseTotal
if ($_[$i] eq $_) 11 11 22
971 }
972 # stop as soon as first non-symbol encountered, so as not to
973 # accidentally mess with following hash-style options
974 12 ▶ $found==0 and last;
Branch: 1 branch
BranchTrueFalseTotal
$found == 0 and last 1 11 12
975 }
976 27 ▶%import and export($user_pkg, keys %import); # create aliases for user
Branch: 1 branch
BranchTrueFalseTotal
%import and export($user_pkg, keys %import) 8 19 27
977  
978 # following gets "attempt to delete unreferenced scalar"!
979 # %import and delete @_[values %import];
980 # Delete from @_ each
981 27 map { delete $_[$_] } values %import; # but this works
982 # warn "Numvals in array is ", scalar @_, "\n";
983  
984 27 @_= grep defined, @_; # reset, to eliminate extracted imports
985 # warn "Numvals in array is now ", scalar @_, "\n";
986 # warnings sets user-program debugging level
987 # debug sets module's debuging level
988 27 my @legal_options = qw( style prompt testmode warnings debug logging );
989 27 my %options =
990 hash_options(\@legal_options, @_ ); # style => Korn, etc.
991  
992  
993 27 my @styles=qw( bash korn );
994 27 my @prompts=qw( generic korn bash arrows );
995 27 my @testmodes=qw( make foreach );
996  
997 27 my $bad;
998  
999 # timji: Loopify this section later, once it gets stable
1000  
1001 # "logging" enables/disables logging of filter output to file
1002 27 ▶ $_ = $ENV{Shell_POSIX_Select_logging} || $options{logging};
Condition33%1/3
$ENV{'Shell_POSIX_Select_logging'} || $options{'logging'}
l!l&&r!l&&!r
0027
MC/DC0%0/2
$ENV{'Shell_POSIX_Select_logging'} || $options{'logging'}
$ENV{'Shell_POSIX_Select_logging'} $options{'logging'}
Truth table33%1/3
A || B
A= $ENV{'Shell_POSIX_Select_logging'}B= $options{'logging'}
ABresult
1X1
011
000
1003 27 ▶ if (defined) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_) 0 27 27
1004 # unless ( is_unix() ) {
1005 # warn "$PKG\::$subname: logging is only for UNIX-like OSs\n";
1006 # }
1007 0 ▶ if (/^(\d)$/ and 0 <= $1 and $1 <=1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^(\d)$/ and 0 <= $1 and $1 <= 1) { } 0 0 0
Condition0%0/6
/^(\d)$/ and 0 <= $1 and $1 <= 1
!ll&&!rl&&r
000
/^(\d)$/ and 0 <= $1
!ll&&!rl&&r
000
MC/DC0%0/3
/^(\d)$/ and 0 <= $1 and $1 <= 1
/^(\d)$/ 0 <= $1 $1 <= 1
Truth table0%0/4
A and B and C
A= /^(\d)$/B= 0 <= $1C= $1 <= 1
ABCresult
0XX0
10X0
1100
1111
1008 0 $LOGGING = $_;
1009 0 ▶ $DEBUG > 0 and warn "$PKG: Set logging to: $LOGGING\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 0 and warn "${PKG}: Set logging to: $LOGGING\n" 0 0 0
1010 }
1011 else {
1012 0 _WARN "$PKG\::$subname: Invalid logging level '$_'\n";
1013 0 ▶ $DEBUG > 1 and _DIE;
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 1 and _DIE() 0 0 0
1014 }
1015 }
1016  
1017 # "debug" enables/disables informational messages while running user program
1018 27 ▶ $_ = $ENV{Shell_POSIX_Select_warnings} || $options{warnings};
Condition33%1/3
$ENV{'Shell_POSIX_Select_warnings'} || $options{'warnings'}
l!l&&r!l&&!r
0027
MC/DC0%0/2
$ENV{'Shell_POSIX_Select_warnings'} || $options{'warnings'}
$ENV{'Shell_POSIX_Select_warnings'} $options{'warnings'}
Truth table33%1/3
A || B
A= $ENV{'Shell_POSIX_Select_warnings'}B= $options{'warnings'}
ABresult
1X1
011
000
1019 27 $select2foreach=0;
1020 27 ▶ if (defined) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_) 0 27 27
1021 0 ▶ if (/^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\d+$/) { } 0 0 0
1022 0 $Shell::POSIX::Select::U_WARN = $_;
1023 0 warn "$PKG: Set warnings to: $Shell::POSIX::Select::U_WARN\n";
1024 }
1025 else {
1026 0 _WARN "$PKG\::$subname: Invalid warnings level '$_'\n";
1027 0 ▶ $DEBUG > 1 and _DIE;
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 1 and _DIE() 0 0 0
1028 }
1029 }
1030  
1031 # "debug" enables/disables informational messages while running user program
1032 27 ▶ $_ = $ENV{Shell_POSIX_Select_debug} || $options{debug};
Condition33%1/3
$ENV{'Shell_POSIX_Select_debug'} || $options{'debug'}
l!l&&r!l&&!r
0027
MC/DC0%0/2
$ENV{'Shell_POSIX_Select_debug'} || $options{'debug'}
$ENV{'Shell_POSIX_Select_debug'} $options{'debug'}
Truth table33%1/3
A || B
A= $ENV{'Shell_POSIX_Select_debug'}B= $options{'debug'}
ABresult
1X1
011
000
1033 27 ▶ if (defined) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_) 0 27 27
1034 0 ▶ if (/^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\d+$/) { } 0 0 0
1035 0 $Shell::POSIX::Select::DEBUG = $_;
1036 }
1037 else {
1038 0 _WARN "$PKG\::$subname: Invalid debug option '$_'\n";
1039 0 ▶ $DEBUG > 1 and _DIE;
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 1 and _DIE() 0 0 0
1040 }
1041 }
1042  
1043 27 ▶ $_=$ENV{Shell_POSIX_Select_style} || $options{style};
Condition33%1/3
$ENV{'Shell_POSIX_Select_style'} || $options{'style'}
l!l&&r!l&&!r
0027
MC/DC0%0/2
$ENV{'Shell_POSIX_Select_style'} || $options{'style'}
$ENV{'Shell_POSIX_Select_style'} $options{'style'}
Truth table33%1/3
A || B
A= $ENV{'Shell_POSIX_Select_style'}B= $options{'style'}
ABresult
1X1
011
000
1044 27 ▶ if (defined) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_) 0 27 27
1045 0 my $found=0;
1046 0 foreach my $style (@styles) {
1047 0 ▶ if ($_ =~ /^$style$/i ) { # korn, bash,etc.
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /^$style$/i) 0 0 0
1048 # code as K, B, etc.
1049 0 $Shell::POSIX::Select::_style = uc substr($_,0,1);
1050 0 $found++; # last one wins
1051 }
1052 }
1053 0 ▶ if (! $found) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 0 0 0
1054 0 _WARN "$PKG\::$subname: Invalid style option '$_'\n";
1055 0 ▶ $DEBUG > 1 and _DIE;
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 1 and _DIE() 0 0 0
1056 }
1057 }
1058  
1059 # Bash automatically shows prompt every time,
1060 # Ksh only does if user enters input of <CR> only
1061 27 my $autoprompt=0;
1062 27 ▶ if ( $Shell::POSIX::Select::_style eq 'K' ) { $autoprompt=0; }
Branch: 2 branches
BranchTrueFalseTotal
if ($_style eq 'K') { } 27 0 27
elsif ($_style eq 'B') { } 0 0 0
1063 0 elsif ( $Shell::POSIX::Select::_style eq 'B' ) { $autoprompt=1; }
1064  
1065 27 $Shell::POSIX::Select::_autoprompt = $autoprompt;
1066 27 ▶ $_ = $ENV{Shell_POSIX_Select_prompt} || $options{prompt} ;
Condition33%1/3
$ENV{'Shell_POSIX_Select_prompt'} || $options{'prompt'}
l!l&&r!l&&!r
0027
MC/DC0%0/2
$ENV{'Shell_POSIX_Select_prompt'} || $options{'prompt'}
$ENV{'Shell_POSIX_Select_prompt'} $options{'prompt'}
Truth table33%1/3
A || B
A= $ENV{'Shell_POSIX_Select_prompt'}B= $options{'prompt'}
ABresult
1X1
011
000
1067 27 ▶ if (defined) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_) 0 27 27
1068 0 $_=lc $_;
1069 0 my $found=0;
1070 0 foreach my $prompt (sort @prompts) { # sorting, so "generic" choice beats shell-specific ones
1071 0 ▶ if ($_ =~ /^$prompt$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /^$prompt$/i) 0 0 0
1072 0 ▶ $_ eq 'generic' and do {
Branch: 1 branch
BranchTrueFalseTotal
$_ eq "generic" and do { $DEBUG > 0 and warn "Set generic prompt"; $_prompt = $_generic; ++$found and last; die 33 } 0 0 0
1073 0 ▶ $DEBUG > 0 and warn "Set generic prompt";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 0 and warn "Set generic prompt" 0 0 0
1074 0 $Shell::POSIX::Select::_prompt =
1075 $Shell::POSIX::Select::_generic;
1076 0 ▶ ++$found and last;
Branch: 1 branch
BranchTrueFalseTotal
++$found and last 0 0 0
1077 0 die 33;
1078 };
1079 0 ▶ $_ eq "korn" and do {
Branch: 1 branch
BranchTrueFalseTotal
$_ eq "korn" and do { $_prompt = $_korn_prompt; ++$found; last } 0 0 0
1080 0 $Shell::POSIX::Select::_prompt =
1081 $Shell::POSIX::Select::_korn_prompt;
1082 0 $found++;
1083 0 last;
1084 };
1085 0 ▶ $_ eq "bash" and do {
Branch: 1 branch
BranchTrueFalseTotal
$_ eq "bash" and do { $_prompt = $_bash_prompt; ++$found; last } 0 0 0
1086 0 $Shell::POSIX::Select::_prompt =
1087 $Shell::POSIX::Select::_bash_prompt;
1088 0 $found++;
1089 0 last;
1090 };
1091 0 ▶ $_ eq "arrows" and do {
Branch: 1 branch
BranchTrueFalseTotal
$_ eq "arrows" and do { $_prompt = $_arrows_prompt; ++$found; last } 0 0 0
1092 0 $Shell::POSIX::Select::_prompt =
1093 $Shell::POSIX::Select::_arrows_prompt;
1094 0 $found++;
1095 0 last;
1096 };
1097 }
1098 # If not a prompt keyword, must be literal prompt
1099 0 do {
1100 0 $Shell::POSIX::Select::_prompt = $_;
1101 0 $found++;
1102 0 last;
1103 };
1104 }
1105 0 ▶ if (! $found) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 0 0 0
1106 0 _WARN "$PKG\::$subname: Invalid prompt option '$_'\n";
1107 0 ▶ $DEBUG > 1 and _DIE;
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 1 and _DIE() 0 0 0
1108 }
1109 }
1110  
1111 27 $Shell::POSIX::Select::dump_data=0;
1112 27 ▶ $_= $ENV{Shell_POSIX_Select_testmode} || $options{testmode} ;
Condition33%1/3
$ENV{'Shell_POSIX_Select_testmode'} || $options{'testmode'}
l!l&&r!l&&!r
2700
MC/DC0%0/2
$ENV{'Shell_POSIX_Select_testmode'} || $options{'testmode'}
$ENV{'Shell_POSIX_Select_testmode'} $options{'testmode'}
Truth table33%1/3
A || B
A= $ENV{'Shell_POSIX_Select_testmode'}B= $options{'testmode'}
ABresult
1X1
011
000
1113 27 ▶ if (defined) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_) 27 0 27
1114 27 my $found=0;
1115 #foreach my $mode ( @testmodes ) {
1116 27 ▶ if ($_ =~ /^make$/i ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_ =~ /^make$/i) { } 27 0 27
elsif ($_ =~ /^foreach$/i) { } 0 0 0
1117 27 $Shell::POSIX::Select::_testmode= 'make';
1118 27 $Shell::POSIX::Select::dump_data=1;
1119 27 $found++;
1120 }
1121 elsif ($_ =~ /^foreach$/i ) {
1122 0 $Shell::POSIX::Select::_testmode= 'foreach';
1123 0 $select2foreach=1;
1124 0 $found++;
1125 }
1126 else {
1127 0 $Shell::POSIX::Select::_testmode= '';
1128 0 ▶ $DEBUG > 2 and _WARN "Unrecognized testmode: $_\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 2 and _WARN("Unrecognized testmode: $_\n") 0 0 0
1129 }
1130 #}
1131 27 ▶ if (! $found) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 0 27 27
1132 0 _WARN "$PKG\::$subname: Invalid testmode option '$_'\n";
1133 0 ▶ $DEBUG > 1 and _DIE;
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 1 and _DIE() 0 0 0
1134 }
1135 }
1136 # ENV variable overrides program spec
1137  
1138 27 ▶ ( ! defined $Shell::POSIX::Select::_testmode or
Branch: 1 branch
BranchTrueFalseTotal
!defined($_testmode) || $_testmode eq "" and $_testmode = "" 0 27 27
Condition33%1/3
not defined $_testmode or $_testmode eq ""
l!l&&r!l&&!r
0027
MC/DC0%0/2
not defined $_testmode or $_testmode eq ""
not defined $_testmode $_testmode eq ""
Truth table33%1/3
not(A) or B
A= not defined $_testmodeB= $_testmode eq ""
ABresult
1X1
011
000
1139 $Shell::POSIX::Select::_testmode eq "" ) and
1140 $Shell::POSIX::Select::_testmode = "";
1141  
1142 27 ▶$DEBUG > 2 and warn "37 Testmode set to $Shell::POSIX::Select::_testmode\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 2 and warn "37 Testmode set to $_testmode\n" 0 27 27
1143  
1144 27 ▶ $LOGGING and log_files();
Branch: 1 branch
BranchTrueFalseTotal
$LOGGING and log_files() 0 27 27
1145  
1146 $ENV{Shell_POSIX_Select_reference} and
1147 27 ▶ $Shell::POSIX::Select::dump_data = 'Ref_Data';
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"Shell_POSIX_Select_reference"} and $dump_data = "Ref_Data" 0 27 27
1148  
1149 # Don't assume /dev/tty will work on user's platform!
1150 27 ▶ if ( $Shell::POSIX::Select::dump_data ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dump_data) 27 0 27
1151  
1152 # must ensure all output gets flushed to dumpfile before exiting
1153 27 disable_buffering();
1154  
1155 #if ( ! $PRODUCTION ) {
1156 27 $Shell::POSIX::Select::_TTY=0;
1157 # What's the OS-portable equivalent of "/dev/tty" in the above?
1158 27 ▶ if ( -c '/dev/tty' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-c "/dev/tty") 27 0 27
1159 27 ▶ if ( open TTY, '> /dev/tty' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open TTY, '> /dev/tty') { } 0 27 27
1160 0 $Shell::POSIX::Select::_TTY=1;
1161 }
1162 else {
1163 27 _WARN "Open of /dev/tty failed, $!\n";
1164 }
1165 }
1166 #}
1167  
1168 27 $sdump = qq/$script.sdump/;
1169 27 ▶ if ($Shell::POSIX::Select::dump_data =~ /[a-z]/i)
Branch: 1 branch
BranchTrueFalseTotal
if ($dump_data =~ /[a-z]/i) { } 0 27 27
1170 {
1171 0 $sdump = catfile($Shell::POSIX::Select::dump_data, $sdump .'_ref');
1172 }
1173 else
1174 {
1175 # TODO: probably should put it in the same
1176 # folder as the original program being
1177 # analyzed, rather than '.':
1178 27 $sdump = catfile('.', $sdump);
1179 }
1180 27 ($cdump = $sdump) =~ s/$script\.sdump/$script.cdump/; # make code-dump name too
1181  
1182 # HERE next two lines squelch
1183  
1184  
1185 # Make reference copies of dumps for distribution, or test copies,
1186 # depending on ENV{reference} set or testmode=make
1187 27 ▶ close STDERR or
Branch: 1 branch
BranchTrueFalseTotal
close STDERR or die "$PKG-END(): Failed to close 'STDERR', $!\n" 27 0 27
1188 die "$PKG-END(): Failed to close 'STDERR', $!\n";
1189 27 ▶ open STDERR, "> $sdump" or
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, "> $sdump" or die "$PKG-END(): Failed to open '${sdump}' for writing, $!\n" 27 0 27
1190 die "$PKG-END(): Failed to open '$sdump' for writing, $!\n";
1191  
1192 27 ▶ open STDOUT, ">&STDERR" or
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&STDERR" or die "$PKG-END(): Failed to dup STDOUT to STDERR, $!\n" 27 0 27
1193 die "$PKG-END(): Failed to dup STDOUT to STDERR, $!\n";
1194 }
1195  
1196 27 ( $ON , $OFF , $BOLD , $SGR0 , $COLS ) =
1197 display_control ($Shell::POSIX::Select::dump_data);
1198 27 1;
1199 } # import
1200  
1201 8 ▶sub export { # appropriated from Switch.pm
Subroutine export: called CC=2 SCAR=6.9
Pod undocumented
1202 8 my $subname = sub_name();
1203  
1204 # $offset = (caller)[2]+1;
1205 8 my $pkg = shift;
1206 26 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1207 # All exports are scalard vars, so strip sigils and poke in package name
1208 8 foreach ( map { s/^\$//; $_ } @_ ) { # must change $Reply to Reply, etc.
1209 11 *{"${pkg}::$_"} =
1210 11 \${ "Shell::POSIX::Select::$_" };
1211 # "Shell::POSIX::Select::$_";
1212 }
1213 # *{"${pkg}::__"} = \&__ if grep /__/, @_;
1214 8 1;
1215 }
1216  
1217 27 ▶sub hash_options {
Subroutine hash_options: called CC=3 SCAR=11.2
Pod undocumented
1218 27 my $ref_legal_keys = shift;
1219 27 my %options = @_ ;
1220 27 my $num_options=keys %options;
1221 27 my %options2 ;
1222  
1223 27 my $subname = sub_name();
1224  
1225  
1226  
1227 27 ▶ if ($num_options) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_options) 1 26 27
1228 my @legit_options =
1229 1 grep { "@$ref_legal_keys" =~ /\b $_ \b/x }
1230 sort ignoring_case keys %options;
1231  
1232 my @illegit_options =
1233 1 grep { "@$ref_legal_keys" !~ /\b $_ \b/x }
1234 sort ignoring_case keys %options;
1235  
1236 @options2{sort ignoring_case @legit_options} =
1237 1 @options{sort ignoring_case @legit_options } ;
1238 { # scope for local change to $,
1239 1 local $,=' ';
1240 1 ▶ if ($num_options > keys %options2) { # options filtered out?
Branch: 1 branch
BranchTrueFalseTotal
if ($num_options > keys %options2) 0 1 1
1241 0 my $msg= "$PKG\::$subname:\n Invalid options: " ;
1242 0 $msg .= "@illegit_options\n";
1243 0 _DIE; # Can't be conditional on DEBUG setting,
1244 # because that comes after this sub returns!
1245 }
1246 }
1247  
1248 }
1249  
1250 27 return %options2;
1251 }
1252  
1253 0 ▶sub show_subs {
Subroutine show_subs: not called CC=6 SCAR=37.4
Pod undocumented
1254 # show sub-string in reverse video, primarily for debugging
1255 0 my $subname = sub_name();
1256  
1257 0 ▶ @_ >= 1 or die "${PKG}\::subname: no arguments\n" ;
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 1 or die "${PKG}::subname: no arguments\n" 0 0 0
1258 0 ▶ my $msg=shift || '<no msg>';
Condition0%0/2
shift() || '<no msg>'
l!l
00
MC/DC0%0/1
shift() || '<no msg>'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1259 0 ▶ my $string=(shift || '');
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1260 0 ▶ my $start=(shift || 0);
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1261 0 ▶ my $length=(shift || 9999);
Condition0%0/2
shift() || 9999
l!l
00
MC/DC0%0/1
shift() || 9999
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1262  
1263 0 $string =~ s/[^[[:alpha:]\d\s]]/-/g; # control-chars screw up printing
1264 # warn "Calling substr for parms $string/$start/$length\n";
1265 0 warn "$msg", $ON, substr ($string, $start, $length), $OFF, "\n";
1266 }
1267  
1268 80 ▶sub gobble_spaces {
Subroutine gobble_spaces: called CC=4 SCAR=14.0
Pod undocumented
1269 80 my $subname = sub_name();
1270  
1271 80 my $pos=pos(); # remember current position
1272 80 ▶ if (/\G\s+/g) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G\s+/g) { } 74 6 80
1273 74 ▶ $DEBUG_FILT > 1 and
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG_FILT > 1 and warn "${subname}: space gobbler matched '$&' of length ", length $&, "\n" 0 74 74
1274 warn "$subname: space gobbler matched '$&' of length ", length $&, "\n" ;
1275 }
1276 else {
1277 6 ▶ $DEBUG_FILT > 1 and warn "$subname: space gobbler matched nothing\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG_FILT > 1 and warn "${subname}: space gobbler matched nothing\n" 0 6 6
1278 6 pos()=$pos; # reset to prior position
1279 }
1280 80 $pos=pos(); # identify current position
1281 }
1282  
1283 27 ▶sub display_control {
Subroutine display_control: called CC=14 SCAR=45.5
Pod undocumented
1284 27 my $subname = sub_name();
1285  
1286 27 my $flag=shift;
1287 27 my ( $on , $off , $bold , $sgr0 , $cols ) ;
1288  
1289 # in "make" or "reference" testmodes, mustn't clutter output with coloration
1290 # Disable screen manips for reference source-code dumps
1291 27 ▶ unless ( $flag ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($flag) 0 27 27
1292 0 ▶ if ( is_unix() and
Branch: 1 branch
BranchTrueFalseTotal
if (is_unix() and defined $ENV{'TERM'} and not system('tput -V >/dev/null 2>&1')) { } 0 0 0
Condition0%0/6
is_unix() and defined $ENV{"TERM"} and not system("tput -V >/dev/null 2>&1")
!ll&&!rl&&r
000
is_unix() and defined $ENV{"TERM"}
!ll&&!rl&&r
000
MC/DC0%0/3
is_unix() and defined $ENV{"TERM"} and not system("tput -V >/dev/null 2>&1")
is_unix() defined $ENV{"TERM"} not system("tput -V >/dev/null 2>&1")
Truth table0%0/4
A and B and not(C)
A= is_unix()B= defined $ENV{"TERM"}C= not system("tput -V >/dev/null 2>&1")
ABCresult
0XX0
10X0
1100
1111
1293 defined $ENV{TERM} and
1294 ! system 'tput -V >/dev/null 2>&1' ) {
1295 # Always need column count
1296 # for menu sizing
1297 0 ▶ $cols=`tput cols`; defined $COLS and chomp ($COLS) ;
Branch: 1 branch
BranchTrueFalseTotal
defined $COLS and chomp $COLS 0 0 0
1298 0 ▶ if ($flag ne 'make') {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag ne "make") 0 0 0
1299 0 $on=`tput smso`;
1300 0 ▶ $off=`tput rmso` || `tput sgr0`;
Condition0%0/3
`tput rmso` || `tput sgr0`
l!l&&r!l&&!r
000
MC/DC0%0/2
`tput rmso` || `tput sgr0`
`tput rmso` `tput sgr0`
Truth table0%0/3
A || B
A= `tput rmso`B= `tput sgr0`
ABresult
1X1
011
000
1301 0 $bold=`tput bold`; # for prettifying screen captures
1302 0 $sgr0=`tput sgr0`; # for prettifying screen captures
1303 }
1304 }
1305 else {
1306 }
1307 0 ▶ $DEBUG > 2 and warn "Returning $on , $off , $bold , sgr0 , $cols \n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 2 and warn "Returning $on , $off , $bold , sgr0 , $cols \n" 0 0 0
1308 }
1309 27 ▶ return ($on || "", $off || "", $bold || "", $sgr0 || "", $cols || 80);
Condition50%5/10
$on || ''
l!l
027
$off || ''
l!l
027
$bold || ''
l!l
027
$sgr0 || ''
l!l
027
$cols || 80
l!l
027
MC/DC0%0/1
$on || ''
$on
MC/DC0%0/1
$off || ''
$off
MC/DC0%0/1
$bold || ''
$bold
MC/DC0%0/1
$sgr0 || ''
$sgr0
MC/DC0%0/1
$cols || 80
$cols
Truth table50%1/2
A
A= $on
Aresult
11
00
Truth table50%1/2
A
A= $off
Aresult
11
00
Truth table50%1/2
A
A= $bold
Aresult
11
00
Truth table50%1/2
A
A= $sgr0
Aresult
11
00
Truth table50%1/2
A
A= $cols
Aresult
11
00
1310 }
1311  
1312 26 ▶END { # END block
Subroutine END: called CC=12 SCAR=40.6
1313 # sdump means screen-dump, cdump means code-dump
1314 26 ▶ if ( $Shell::POSIX::Select::dump_data ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dump_data) { } 26 0 26
1315 26 ▶ if ( $ENV{Shell_POSIX_Select_reference} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{'Shell_POSIX_Select_reference'}) { } 0 26 26
1316 }
1317 else {
1318 }
1319 26 my $pwd=curdir();
1320 # $Shell::POSIX::Select::_TTY and
1321 # dump filtered source, for reference or analysis
1322 26 ▶ unless (open SOURCE, "> $cdump") {
Branch: 1 branch
BranchTrueFalseTotal
unless (open SOURCE, "> $cdump") 0 26 26
1323 0 ▶ $Shell::POSIX::Select::_TTY and
Branch: 1 branch
BranchTrueFalseTotal
$_TTY and print TTY "$PKG-END(): Failed to open '${cdump}' for writing, $!\n" and warn "$PKG-END(): Failed to open '${cdump}' for writing, $!\n" 0 0 0
Condition0%0/3
$_TTY and print TTY "$PKG-END(): Failed to open '${cdump}' for writing, $!\n"
!ll&&!rl&&r
000
MC/DC0%0/2
$_TTY and print TTY "$PKG-END(): Failed to open '${cdump}' for writing, $!\n"
$_TTY print TTY "$PKG-END(): Failed to open '${cdump}' for writing, $!\n"
Truth table0%0/3
A and B
A= $_TTYB= print TTY "$PKG-END(): Failed to open '${cdump}' for writing, $!\n"
ABresult
0X0
100
111
1324 print TTY "$PKG-END(): Failed to open '$cdump' for writing, $!\n" and
1325 warn "$PKG-END(): Failed to open '$cdump' for writing, $!\n" ;
1326 0 die;
1327 }
1328 26 ▶ defined $Shell::POSIX::Select::filter_output and
Branch: 1 branch
BranchTrueFalseTotal
defined $filter_output and print(SOURCE $filter_output) || die("$PKG-END(): Failed to write to '${cdump}', $!\n") 26 0 26
Condition50%2/4
defined $filter_output && (print(SOURCE $filter_output) || die("$PKG-END(): Failed to write to '${cdump}', $!\n"))
!ll
026
print SOURCE $filter_output or die "$PKG-END(): Failed to write to '${cdump}', $!\n"
l!l
260
MC/DC0%0/2
defined $filter_output && (print(SOURCE $filter_output) || die("$PKG-END(): Failed to write to '${cdump}', $!\n"))
defined $filter_output print SOURCE $filter_output
Truth table0%0/3
A && B
A= defined $filter_outputB= print SOURCE $filter_output
ABresult
0X0
100
111
1329 (print SOURCE $Shell::POSIX::Select::filter_output or
1330 die "$PKG-END(): Failed to write to '$cdump', $!\n");
1331 # system "ls -li $cdump $sdump";
1332 }
1333  
1334 # Screen dumping now arranged in sub import()
1335 # open SCREEN, "> $script.sdump" or
1336 # die "$PKG-END(): Failed to open '$script.sdump' for writing, $!\n";
1337  
1338 else {
1339 0 ▶ defined $SGR0 and $SGR0 ne "" and print STDERR "$SGR0"; # ensure turned off
Branch: 1 branch
BranchTrueFalseTotal
defined $SGR0 and $SGR0 ne "" and print STDERR "$SGR0" 0 0 0
Condition0%0/3
defined $SGR0 and $SGR0 ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $SGR0 and $SGR0 ne ""
defined $SGR0 $SGR0 ne ""
Truth table0%0/3
A and B
A= defined $SGR0B= $SGR0 ne ""
ABresult
0X0
100
111
1340 0 ▶ $DEBUG > 1 and $LOGGING and print LOG "\n$PKG finished\n";
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 1 and $LOGGING and print LOG "\n$PKG finished\n" 0 0 0
Condition0%0/3
$DEBUG > 1 and $LOGGING
!ll&&!rl&&r
000
MC/DC0%0/2
$DEBUG > 1 and $LOGGING
$DEBUG > 1 $LOGGING
Truth table0%0/3
A and B
A= $DEBUG > 1B= $LOGGING
ABresult
0X0
100
111
1341 0 print STDERR "\n"; # ensure shell prompt starts on fresh line
1342 }
1343 26 exit 0;
1344 }
1345  
1346 0 ▶sub is_unix {
Subroutine is_unix: not called CC=5 SCAR=34.0
Pod undocumented
1347 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /^(MacOS|MSWin32|os2|VMS|epoc|NetWare|dos|cygwin)$/xi) { } 0 0 0
1348 # I'm using the $^O from File::Spec, which oughta know
1349 # and guessing at others; help!
1350 $^O =~ /^(MacOS|MSWin32|os2|VMS|epoc|NetWare|dos|cygwin)$/ix
1351 ) {
1352 0 ▶ $DEBUG > 2
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 2 and warn "Operating System not UNIX;", $^O, "\n" 0 0 0
1353 and warn "Operating System not UNIX;", $^O, "\n";
1354 }
1355 else {
1356 0 ▶ $DEBUG > 2
Branch: 1 branch
BranchTrueFalseTotal
$DEBUG > 2 and warn "Operating System reported as ", $^O, "\n" 0 0 0
1357 and warn "Operating System reported as ", $^O, "\n";
1358 }
1359 0 ▶ return defined $1 ? 0 : 1 ;
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 0 0 0
1360 }
1361  
1362 27 ▶sub disable_buffering {
Subroutine disable_buffering: called CC=1 SCAR=0.0
Pod undocumented
1363  
1364 27 my $old_fh = select (STDERR);
1365 27 $|=1;
1366 27 select ($old_fh);
1367 27 return 0;
1368 }
1369  
1370 =pod
1371  
1372 =head1 NAME
1373  
1374 Shell::POSIX::Select - The POSIX Shell's "select" loop for Perl
1375  
1376 =head1 PURPOSE
1377  
1378 This module implements the C<select> loop of the "POSIX" shells (Bash, Korn, and derivatives)
1379 for Perl.
1380 That loop is unique in two ways: it's by far the friendliest feature of any UNIX shell,
1381 and it's the I<only> UNIX shell loop that's missing from the Perl language. Until now!
1382  
1383 What's so great about this loop? It automates the generation of a numbered menu
1384 of choices, prompts for a choice, proofreads that choice and complains if it's invalid
1385 (at least in this enhanced implementation), and executes a code-block with a variable
1386 set to the chosen value. That saves a lot of coding for interactive programs --
1387 especially if the menu consists of many values!
1388  
1389 The benefit of bringing this loop to Perl is that it obviates the
1390 need for future programmers
1391 to reinvent the I<Choose-From-A-Menu> wheel.
1392  
1393 =head1 SYNOPSIS
1394  
1395 =for comment
1396 Resist temptation to add more spaces in line below; they cause bad wrapping for text-only document version
1397
1398 =for comment
1399 The damn CPAN html renderer, which doesn't respond to
1400 =for html or =for HTML directives (!), can't get the following
1401 right! It's showing the B<> codes! Postscript and text work fine.
1402 B<select>
1403 [ [ my | local | our ] scalar_var ]
1404 B<(> [LIST] B<)>
1405 B<{> [CODE] B<}>
1406  
1407 select [ [my|local|our] scalar_var ] ( [LIST] ) { [CODE] }
1408  
1409 In the above, the enclosing square brackets I<(not typed)> identify optional elements, and vertical bars separate mutually-exclusive choices:
1410  
1411 The required elements are the keyword C<select>,
1412 the I<parentheses>, and the I<curly braces>.
1413 See L<"SYNTAX"> for details.
1414  
1415 =head1 ELEMENTARY EXAMPLES
1416  
1417 NOTE: All non-trivial programming examples shown in this document are
1418 distributed with this module, in the B<Scripts> directory.
1419 L<"ADDITIONAL EXAMPLES">, covering more features, are shown below.
1420  
1421 =head2 ship2me.plx
1422  
1423 use Shell::POSIX::Select;
1424  
1425 select $shipper ( 'UPS', 'FedEx' ) {
1426 print "\nYou chose: $shipper\n";
1427 last;
1428 }
1429 ship ($shipper, $ARGV[0]); # prints confirmation message
1430  
1431 B<Screen>
1432  
1433 ship2me.plx '42 hemp toothbrushes' # program invocation
1434
1435 1) UPS 2) FedEx
1436
1437 Enter number of choice: 2
1438
1439 You chose: FedEx
1440 Your order has been processed. Thanks for your business!
1441  
1442  
1443 =head2 ship2me2.plx
1444  
1445 This variation on the preceding example shows how to use a custom menu-heading and interactive prompt.
1446 It also presents all menus in one column.
1447  
1448 use Shell::POSIX::Select qw($Heading $Prompt $MaxColumns);
1449  
1450 $Heading = 'Select a Shipper' ;
1451 $Prompt = 'Enter Vendor Number: ' ;
1452 $MaxColumns = 1;
1453 select $shipper ( 'UPS', 'FedEx' ) {
1454 print "\nYou chose: $shipper\n";
1455 last;
1456 }
1457 ship ($shipper, $ARGV[0]); # prints confirmation message
1458  
1459 B<Screen>
1460  
1461 ship2me2.plx '42 hemp toothbrushes'
1462  
1463 Select a Shipper
1464  
1465 1) UPS 2) FedEx
1466  
1467 Enter Vendor Number: 2
1468  
1469 You chose: FedEx
1470 Your order has been processed. Thanks for your business!
1471  
1472  
1473 =head1 SYNTAX
1474  
1475 =head2 Loop Structure
1476  
1477 Supported invocation formats include the following:
1478  
1479 use Shell::POSIX::Select ;
1480  
1481 select () { } # Form 0
1482 select () { CODE } # Form 1
1483 select (LIST) { CODE } # Form 2
1484 select $var (LIST) { CODE } # Form 3
1485 select my $var (LIST) { CODE } # Form 4
1486 select our $var (LIST) { CODE } # Form 5
1487 select local $var (LIST) { CODE } # Form 6
1488  
1489  
1490 If the loop variable is omitted (as in I<Forms> I<0>, I<1> and I<2> above),
1491 it defaults to C<$_>, C<local>ized to the loop's scope.
1492 If the LIST is omitted (as in I<Forms> I<0> and I<1>),
1493 C<@ARGV> is used by default, unless the loop occurs within a subroutine, in which case
1494 C<@_> is used instead.
1495 If CODE is omitted (as in I<Form> I<0>,
1496 it defaults to a statement that B<prints> the loop variable.
1497  
1498 The cases shown above are merely examples; all reasonable permutations are permitted, including:
1499  
1500 select $var ( ) { CODE }
1501 select local $var (LIST) { }
1502  
1503 The only form that's I<not> allowed is one that specifies the loop-variable's declarator without naming the loop variable, as in:
1504  
1505 select our () { } # WRONG! Must name variable with declarator!
1506  
1507 =head2 The Loop variable
1508  
1509 See L<"SCOPING ISSUES"> for full details about the implications
1510 of different types of declarations for the loop variable.
1511  
1512 =head2 The $Reply Variable
1513  
1514 When the interactive user responds to the C<select> loop's prompt
1515 with a valid input (i.e., a number in the correct range),
1516 the variable C<$Reply> is set within the loop to that number.
1517 Of course, the actual item selected is usually of great interest than
1518 its number in the menu, but there are cases in which access to this
1519 number is useful (see L<"menu_ls.plx"> for an example).
1520  
1521 =head1 OVERVIEW
1522  
1523 This loop is syntactically similar to Perl's
1524 C<foreach> loop, and functionally related, so we'll describe it in those terms.
1525  
1526 foreach $var ( LIST ) { CODE }
1527  
1528 The job of C<foreach> is to run one iteration of CODE for each LIST-item,
1529 with the current item's value placed in C<local>ized C<$var>
1530 (or if the variable is missing, C<local>ized C<$_>).
1531  
1532 select $var ( LIST ) { CODE }
1533  
1534 In contrast, the C<select> loop displays a numbered menu of
1535 LIST-items on the screen, prompts for (numerical) input, and then runs an iteration
1536 with C<$var> being set that number's LIST-item.
1537  
1538 In other words, C<select> is like an interactive, multiple-choice version of a
1539 C<foreach> loop.
1540 And that's cool! What's I<not> so cool is that
1541 C<select> is also the I<only> UNIX shell loop that's been left out of
1542 the Perl language. I<Until now!>
1543  
1544 This module implements the C<select> loop of the Korn and Bash
1545 ("POSIX") shells for Perl.
1546 It accomplishes this through Filter::Simple's I<Source Code Filtering> service,
1547 allowing the programmer to blithely proceed as if this control feature existed natively in Perl.
1548  
1549 The Bash and Korn shells differ slightly in their handling
1550 of C<select> loops, primarily with respect to the layout of the on-screen menu.
1551 This implementation currently follows the Korn shell version most closely
1552 (but see L<"TODO-LIST"> for notes on planned enhancements).
1553  
1554 =head1 ENHANCEMENTS
1555  
1556 Although the shell doesn't allow the loop variable to be omitted,
1557 for compliance with Perlish expectations,
1558 the C<select> loop uses C<local>ized C<$_> by default
1559 (as does the native C<foreach> loop). See L<"SYNTAX"> for details.
1560  
1561 The interface and behavior of the Shell versions has been retained
1562 where deemed desirable,
1563 and sensibly modified along Perlish lines elsewhere.
1564 Accordingly, the (primary) default LIST is B<@ARGV> (paralleling the Shell's B<"$@">),
1565 menu prompts can be customized by having the script import and set B<$Prompt>
1566 (paralleling the Shell's B<$PS3>),
1567 and the user's response to the prompt appears in the
1568 variable B<$Reply> (paralleling the Shell's B<$REPLY>),
1569 C<local>ized to the loop.
1570  
1571 A deficiency of the shell implementation is the
1572 inability of the user to provide a I<heading> for each C<select> menu.
1573 Sure, the
1574 shell programmer can B<echo> a heading before the loop is entered and the
1575 menu is displayed, but that approach doesn't help when an I<Outer loop> is
1576 reentered on departure from an I<Inner loop>,
1577 because the B<echo> preceding the I<Outer loop> won't be re-executed.
1578  
1579 A similar deficiency surrounds the handling of a custom prompt string, and
1580 the need to automatically display it on moving from an inner loop
1581 to an outer one.
1582  
1583 To address these deficiencies, this implementation provides the option of having a heading and prompt bound
1584 to each C<select> loop. See L<"IMPORTS AND OPTIONS"> for details.
1585  
1586 Headings and prompts are displayed in reverse video on the terminal,
1587 if possible, to make them more visually distinct.
1588  
1589 Some shell versions simply ignore bad input,
1590 such as the entry of a number outside the menu's valid range,
1591 or alphabetic input. I can't imagine any argument
1592 in favor of this behavior being desirable when input is coming from a terminal,
1593 so this implementation gives clear warning messages for such cases by default
1594 (see L<"Warnings"> for details).
1595  
1596 After a menu's initial prompt is issued, some shell versions don't
1597 show it again unless the user enters an empty line.
1598 This is desirable in cases where the menu is sufficiently large as to
1599 cause preceding output to scroll off the screen, and undesirable otherwise.
1600 Accordingly, an option is provided to enable or disable automatic prompting
1601 (see L<"Prompts">).
1602  
1603 This implementation always issues a fresh prompt
1604 when a terminal user submits EOF as input to a nested C<select> loop.
1605 In such cases, experience shows it's critical to reissue the
1606 menu of the outer loop before accepting any more input.
1607  
1608 =head1 SCOPING ISSUES
1609  
1610 If the loop variable is named and provided with a I<declarator> (C<my>, C<our>, or C<local>),
1611 the variable is scoped within the loop using that type of declaration.
1612 But if the variable is named but lacks a declarator,
1613 no declaration is applied to the variable.
1614  
1615 This allows, for example,
1616 a variable declared as private I<above the loop> to be accessible
1617 from within the loop, and beyond it,
1618 and one declared as private I<for the loop> to be confined to it:
1619  
1620 select my $loopvar ( ) { }
1621 print "$loopvar DOES NOT RETAIN last value from loop here\n";
1622 -------------------------------------------------------------
1623 my $loopvar;
1624 select $loopvar ( ) { }
1625 print "$loopvar RETAINS last value from loop here\n";
1626  
1627 With this design,
1628 C<select> behaves differently than the
1629 native C<foreach> loop, which nowadays employs automatic
1630 localization.
1631  
1632 foreach $othervar ( ) { } # variable localized automatically
1633 print "$othervar DOES NOT RETAIN last value from loop here\n";
1634  
1635 select $othervar ( ) { } # variable in scope, or global
1636 print "$othervar RETAINS last value from loop here\n";
1637  
1638 This difference in the treatment of variables is intentional, and appropriate.
1639 That's because the whole point of C<select>
1640 is to let the user choose a value from a list, so it's often
1641 critically important to be able to see, even outside the loop,
1642 the value assigned to the loop variable.
1643  
1644 In contrast, it's usually considered undesirable and unnecessary
1645 for the value of the
1646 C<foreach> loop's variable to be visible outside the loop, because
1647 in most cases it will simply be that of the last element in the list.
1648  
1649 Of course, in situations where the
1650 C<foreach>-like behavior of implicit C<local>ization is desired,
1651 the programmer has the option of declaring the C<select> loop's
1652 variable as C<local>.
1653  
1654 Another deficiency of the Shell versions is that it's difficult for the
1655 programmer to differentiate between a
1656 C<select> loop being exited via C<last>,
1657 versus the loop detecting EOF on input.
1658 To correct this situation,
1659 the variable C<$Eof> can be imported and checked for a I<TRUE> value
1660 upon exit from a C<select> loop (see L<"Eof Detection">).
1661  
1662 =head1 IMPORTS AND OPTIONS
1663  
1664 =head2 Syntax
1665  
1666 use Shell::POSIX::Select (
1667 '$Prompt', # to customize per-menu prompt
1668 '$Heading', # to customize per-menu heading
1669 '$MaxColumns', # to limit visual number of columns of choices
1670 '$Eof', # T/F for Eof detection
1671 # Variables must come first, then key/value options
1672 prompt => 'Enter number of choice:', # or 'whatever:'
1673 style => 'Bash', # or 'Korn'
1674 warnings => 1, # or 0
1675 debug => 0, # or 1-5
1676 logging => 0, # or 1
1677 testmode => <unset>, # or 'make', or 'foreach'
1678 );
1679  
1680 I<NOTE:> The values shown for options are the defaults, except for C<testmode>, which doesn't have one.
1681  
1682 =head2 Prompts
1683  
1684 There are two ways to customize the prompt used to solicit choices from
1685 C<select> menus; through use of the prompt I<option>, which applies to
1686 all loops, or the C<$Prompt> variable, which can be set independently for
1687 each loop.
1688  
1689 =head3 The prompt option
1690  
1691 The C<prompt> option is intended for use in
1692 programs that either contain a single C<select> loop, or are
1693 content to use the same prompt for every loop.
1694 It allows a custom interactive prompt to be set in the B<use> statement.
1695  
1696 The prompt string should not end in a whitespace character, because
1697 that doesn't look nice when the prompt is highlighted for display
1698 (usually in I<reverse video>).
1699 To offset the cursor from the prompt's end,
1700 I<one space> is inserted automatically
1701 after display highlighting has been turned off.
1702  
1703 If the environment variable C<$ENV{Shell_POSIX_Select_prompt}>
1704 is present,
1705 its value overrides the one in the B<use> statement.
1706  
1707 The default prompt is "Enter number of choice:".
1708 To get the same prompt as provided by the Korn or Bash shell,
1709 use C<< prompt =>> Korn >> or C<< prompt => Bash >>.
1710  
1711 =head3 The $Prompt variable
1712  
1713 The programmer may also modify the prompt during execution,
1714 which may be desirable with nested loops that require different user instructions.
1715 This is accomplished by
1716 importing the $Prompt variable, and setting it to the desired prompt string
1717 before entering the loop. Note that imported variables have to be listed
1718 as the initial arguments to the C<use> directive, and properly quoted.
1719 See L<"order.plx"> for an example.
1720  
1721 NOTE: If the program's input channel is not connected to a terminal,
1722 prompting is automatically disabled
1723 (since there's no point in soliciting input from a I<pipe>!).
1724  
1725 =head2 $Heading
1726  
1727 The programmer has the option of binding a heading to each loop's menu,
1728 by importing C<$Heading> and setting it just before entering the associated loop.
1729 See L<"order.plx"> for an example.
1730  
1731 =head2 $Eof
1732  
1733 A common concern with the Shell's C<select> loop is distinguishing between
1734 cases where a loop ends due to EOF detection, versus the execution of C<break>
1735 (like Perl's C<last>).
1736 Although the Shell programmer can check the C<$REPLY> variable to make
1737 this distinction, this implementation localizes its version of that variable
1738 (C<$Reply>) to the loop,
1739 obviating that possibility.
1740  
1741 Therefore, to make EOF detection as convenient and easy as possible,
1742 the programmer may import C<$Eof> and check it for a
1743 I<TRUE> value after a C<select> loop.
1744 See L<"lc_filename.plx"> for a programming example.
1745  
1746 =head2 Number of Columns
1747  
1748 By default, the visual length of each option is examined,
1749 and the list is spread across as many columns as will reasonably fit in the terminal.
1750 You can override this behavior by importing and setting C<$MaxColumns>
1751 to the maximum number of columns you wish to display.
1752 See Scripts/max_columns_1.plx in the distribution as an example.
1753  
1754 =head2 Styles
1755  
1756 The C<style> options I<Korn> and I<Bash> can be used to request a more Kornish or Bashlike style of behavior.
1757 Currently, the only difference is that the former disables, and the latter enables,
1758 prompting for every input. A value can be
1759 provided for the C<style> option
1760 using an argument of the form C<< style => 'Korn' >> to the C<use> directive.
1761 The default setting is C<Bash>.
1762 If the environment variable C<$ENV{Shell_POSIX_Select_style}> is
1763 set to C<Korn> or C<Bash>,
1764 its value overrides the one provided with the B<use> statement.
1765  
1766 =head2 Warnings
1767  
1768 The C<warnings> option,
1769 whose values range from C<0> to C<1>, enables informational messages meant to help
1770 the interactive user provide correct inputs.
1771 The default setting is C<1>, which provides warnings about incorrect
1772 responses to menu prompts
1773 (I<non-numeric>, I<out of range>, etc.).
1774 Level C<0> turns these off.
1775  
1776 If the environment variable C<$ENV{Shell_POSIX_Select_warnings}> is
1777 present, its value takes precedence.
1778  
1779 =head2 Logging
1780  
1781 The C<logging> option, whose value ranges from C<0> to C<1>,
1782 causes informational messages and source code to be saved in temporary files
1783 (primarily for debugging purposes).
1784  
1785 The default setting is C<0>, which disables logging.
1786  
1787 If the environment variable C<$ENV{Shell_POSIX_Select_logging}> is
1788 present, its value takes precedence.
1789  
1790 =head2 Debug
1791  
1792 The C<debug> option,
1793 whose values range from C<0> to C<9>, enables informational messages
1794 to aid in identifying bugs.
1795 If the environment variable C<$ENV{Shell_POSIX_Select_debug}> is
1796 present, and set to one of the acceptable values, it takes precedence.
1797  
1798 This option is primarly intended for the author's use, but
1799 users who find bugs may want to enable it and email the output to
1800 L<"AUTHOR">. But before concluding that the problem is truly a bug
1801 in this module, please confirm that the program runs correctly with the option
1802 C<< testmode => foreach >> enabled (see L<"Testmode">).
1803  
1804 =head2 Testmode
1805  
1806 The C<testmode> option, whose values are 'make' and 'foreach',
1807 changes the way the program is executed. The 'make' option is used
1808 during the module's installation, and causes the program to dump
1809 the modified source code and screen display to files,
1810 and then stop (rather than interacting with the user).
1811  
1812 If the environment variable C<$ENV{Shell_POSIX_Select_testmode}> is
1813 present, and set to one of the acceptable values, it takes precedence.
1814  
1815 With the C<foreach> option enabled, the program simply translates occurrences
1816 of C<select> into C<foreach>, which provides a useful method for
1817 checking that the program is syntactically correct before any serious
1818 filtering has been applied (which can introduce syntax errors).
1819 This works because the two loops, in their I<full forms>, have identical syntax.
1820  
1821 Note that before you use C<< testmode => foreach >>, you I<must> fill in any
1822 missing parts that are required by C<foreach>.
1823  
1824 For instance,
1825  
1826 C< select () {}>
1827  
1828 must be rewritten as follows, to explicitly show "@ARGV" (assuming it's not in a subroutine) and "print":
1829  
1830 C< foreach (@ARGV) { print; }>
1831  
1832 =head1 ADDITIONAL EXAMPLES
1833  
1834 NOTE: All non-trivial programming examples shown in this document are
1835 distributed with this module, in the B<Scripts> directory.
1836 See L<"ELEMENTARY EXAMPLES">
1837 for simpler uses of C<select>.
1838  
1839 =head2 pick_file.plx
1840  
1841 This program lets the user choose filenames to be sent to the output.
1842 It's sort of like an
1843 interactive Perl C<grep> function, with a live user providing the
1844 filtering service.
1845 As illustrated below,
1846 it could be used with Shell command substitution to provide selected arguments to a command.
1847  
1848 use Shell::POSIX::Select (
1849 prompt => 'Pick File(s):' ,
1850 style => 'Korn' # for automatic prompting
1851 );
1852 select ( <*> ) { }
1853  
1854 B<Screen>
1855  
1856 lp `pick_file`> # Using UNIX-like OS
1857  
1858 1) memo1.txt 2) memo2.txt 3) memo3.txt
1859 4) junk1.txt 5) junk2.txt 6) junk3.txt
1860  
1861 Pick File(s): 4
1862 Pick File(s): 2
1863 Pick File(s): ^D
1864  
1865 request id is yumpy@guru+587
1866  
1867 =head2 browse_images.plx
1868  
1869 Here's a simple yet highly useful script. It displays a menu of all
1870 the image files in the current directory, and then displays the chosen
1871 ones on-screen using a backgrounded image viewer.
1872 It uses Perl's C<grep> to filter-out filenames that don't
1873 end in the desired extensions.
1874  
1875 use Shell::POSIX::Select ;
1876  
1877 $viewer='xv'; # Popular image viewer
1878  
1879 select ( grep /\.(jpg|gif|tif|png)$/i, <*> ) {
1880 system "$viewer $_ &" ; # run viewer in background
1881 }
1882  
1883 =head2 perl_man.plx
1884  
1885 Back in the olden days, we only had one Perl man-page. It was
1886 voluminous, but at least you knew what argument to give the B<man>
1887 command to get the documentaton.
1888  
1889 Now we have over a hundred Perl man pages, with unpredictable names
1890 that are difficult to remember. Here's the program I use that
1891 allows me to select the man-page of interest from a menu.
1892  
1893 use Shell::POSIX::Select ;
1894  
1895 # Extract man-page names from the TOC portion of the output of "perldoc perl"
1896 select $manpage ( sort ( `perldoc perl` =~ /^\s+(perl\w+)\s/mg) ) {
1897 system "perldoc $manpage" ;
1898 }
1899  
1900 B<Screen>
1901  
1902 1) perl5004delta 2) perl5005delta 3) perl561delta
1903 4) perl56delta 5) perl570delta 6) perl571delta
1904 . . .
1905  
1906 I<(This large menu spans multiple screens, but all parts can be accessed
1907 using your normal terminal scrolling facility.)>
1908  
1909 Enter number of choice: 6
1910  
1911
1912 PERL571DELTA(1) Perl Programmers Reference Guide
1913  
1914 NAME
1915 perl571delta - what's new for perl v5.7.1
1916  
1917 DESCRIPTION
1918 This document describes differences between the 5.7.0
1919 release and the 5.7.1 release.
1920 . . .
1921  
1922 =head2 pick.plx
1923  
1924 This more general C<pick>-ing program lets the user make selections
1925 from I<arguments>, if they're present, or else I<input>, in the spirit of Perl's
1926 C<-n> invocation option and C<< <> >> input operator.
1927  
1928 use Shell::POSIX::Select ;
1929  
1930 BEGIN {
1931 if (@ARGV) {
1932 @choices=@ARGV ;
1933 }
1934 else { # if no args, get choices from input
1935 @choices=<STDIN> or die "$0: No data\n";
1936 chomp @choices ;
1937 # STDIN already returned EOF, so must reopen
1938 # for terminal before menu interaction
1939 open STDIN, "/dev/tty" or
1940 die "$0: Failed to open STDIN, $!" ; # UNIX example
1941 }
1942 }
1943 select ( @choices ) { } # prints selections to output
1944  
1945 B<Sample invocations (UNIX-like system)>
1946  
1947 lp `pick *.txt` # same output as shown for "pick_file"
1948  
1949 find . -name '*.plx' -print | pick | xargs lp # includes sub-dirs
1950  
1951 who |
1952 awk '{ print $1 }' | # isolate user names
1953 pick | # select user names
1954 Mail -s 'Promote these people!' boss
1955  
1956  
1957 =head2 delete_file.plx
1958  
1959 In this program, the user selects a filename
1960 to be deleted. The outer loop is used to refresh the list,
1961 so the file deleted on the previous iteration gets removed from the next menu.
1962 The outer loop is I<labeled> (as C<OUTER>), so that the inner loop can refer to it when
1963 necessary.
1964  
1965 use Shell::POSIX::Select (
1966 '$Eof', # for ^D detection
1967 prompt=>'Choose file for deletion:'
1968 ) ;
1969  
1970 OUTER:
1971 while ( @files=<*.py> ) { # collect serpentine files
1972 select ( @files ) { # prompt for deletions
1973 print STDERR "Really delete $_? [y/n]: " ;
1974 my $answer = <STDIN> ; # ^D sets $Eof below
1975 defined $answer or last OUTER ; # exit on ^D
1976 $answer eq "y\n" and unlink and last ;
1977 }
1978 $Eof and last;
1979 }
1980  
1981 =head2 lc_filename.plx
1982  
1983 This example shows the benefit of importing C<$Eof>,
1984 so the outer loop can be exited when the user supplies
1985 C<^D> to the inner one.
1986  
1987 Here's how it works.
1988 If the rename succeeds in the inner loop, execution
1989 of C<last> breaks out of the C<select> loop;
1990 $Eof will then be evaluated as I<FALSE>, and
1991 the C<while> loop will start a new C<select> loop,
1992 with a (depleted) filename menu. But if the user
1993 presses C<^D> to the menu prompt, C<$Eof> will test
1994 as I<TRUE>, triggering the exit from the C<while> loop.
1995  
1996 use Shell::POSIX::Select (
1997 '$Eof' ,
1998 prompt => 'Enter number (^D to exit):'
1999 style => 'Korn' # for automatic prompting
2000 );
2001  
2002 # Rename selected files from current dir to lowercase
2003 while ( @files=<*[A-Z]*> ) { # refreshes select's menu
2004 select ( @files ) { # skip fully lower-case names
2005 if (rename $_, "\L$_") {
2006 last ;
2007 }
2008 else {
2009 warn "$0: rename failed for $_: $!\n";
2010 }
2011 }
2012 $Eof and last ; # Handle ^D to menu prompt
2013 }
2014  
2015 B<Screen>
2016  
2017 lc_filename.plx
2018  
2019 1) Abe.memo 2) Zeke.memo
2020 Enter number (^D to exit): 1
2021  
2022 1) Zeke.memo
2023 Enter number (^D to exit): ^D
2024  
2025 =head2 order.plx
2026  
2027 This program sets a custom prompt and heading for each of
2028 its two loops, and shows the use of a label on the outer loop.
2029  
2030 use Shell::POSIX::Select qw($Prompt $Heading);
2031
2032 $Heading="\n\nQuantity Menu:";
2033 $Prompt="Choose Quantity:";
2034
2035 OUTER:
2036 select my $quantity (1..4) {
2037 $Heading="\nSize Menu:" ;
2038 $Prompt='Choose Size:' ;
2039
2040 select my $size ( qw (L XL) ) {
2041 print "You chose $quantity units of size $size\n" ;
2042 last OUTER ; # Order is complete
2043 }
2044 }
2045  
2046 B<Screen>
2047  
2048 order.plx
2049  
2050 Quantity Menu:
2051 1) 1 2) 2 3) 3 4) 4
2052 Choose Quantity: 4
2053  
2054 Size Menu:
2055 1) L 2) XL
2056 Choose Size: ^D (changed my mind about the quantity)
2057  
2058 Quantity Menu:
2059 1) 1 2) 2 3) 3 4) 4
2060 Choose Quantity: 2
2061  
2062 Size Menu:
2063 1) L 2) XL
2064 Choose Size: 2
2065 You chose 2 units of size XL
2066  
2067 =head2 browse_records.plx
2068  
2069 This program shows how you can implement a "record browser",
2070 that builds a menu from the designated field of each record, and then
2071 shows the record associated with the selected field.
2072  
2073 To use a familiar
2074 example, we'll browse the UNIX password file by user-name.
2075  
2076 use Shell::POSIX::Select ( style => 'Korn' );
2077
2078 if (@ARGV != 2 and @ARGV != 3) {
2079 die "Usage: $0 fieldnum filename [delimiter]" ;
2080 }
2081
2082 # Could also use Getopt:* module for option parsing
2083 ( $field, $file, $delim) = @ARGV ;
2084 if ( ! defined $delim ) {
2085 $delim='[\040\t]+' # SP/TAB sequences
2086 }
2087
2088 $field-- ; # 2->1, 1->0, etc., for 0-based indexing
2089
2090 foreach ( `cat "$file"` ) {
2091 # field is the key in the hash, value is entire record
2092 $f2r{ (split /$delim/, $_)[ $field ] } = $_ ;
2093 }
2094
2095 # Show specified fields in menu, and display associated records
2096 select $record ( sort keys %f2r ) {
2097 print "$f2r{$record}\n" ;
2098 }
2099  
2100 B<Screen>
2101  
2102 browsrec.plx '1' /etc/passwd ':'
2103  
2104 1) at 2) bin 3) contix 4) daemon 5) ftp 6) games
2105 7) lp 8) mail 9) man 10) named 11) news 12) nobody
2106 13) pop 14) postfix 15) root 16) spug 17) sshd 18) tim
2107  
2108 Enter number of choice: 18
2109  
2110 tim:x:213:100:Tim Maher:/home/tim:/bin/bash
2111  
2112 Enter number of choice: ^D
2113  
2114 =head2 menu_ls.plx
2115  
2116 This program shows a prototype for a menu-oriented front end
2117 to a UNIX command, that prompts the user for command-option choices,
2118 assembles the requested command, and then runs it.
2119  
2120 It employs the user's numeric choice,
2121 stored in the C<$Reply> variable, to extract from an array the command
2122 option associated with each option description.
2123  
2124 use Shell::POSIX::Select qw($Heading $Prompt $Eof) ;
2125  
2126 # following avoids used-only once warning
2127 my ($type, $format) ;
2128
2129 # Would be more Perlish to associate choices with options
2130 # via a Hash, but this approach demonstrates $Reply variable
2131
2132 @formats = ( 'regular', 'long' ) ;
2133 @fmt_opt = ( '', '-l' ) ;
2134
2135 @types = ( 'only non-hidden', 'all files' ) ;
2136 @typ_opt = ( '', '-a' , ) ;
2137
2138 print "** LS-Command Composer **\n\n" ;
2139
2140 $Heading="\n**** Style Menu ****" ;
2141 $Prompt= "Choose listing style:" ;
2142 OUTER:
2143 select $format ( @formats ) {
2144 $user_format=$fmt_opt[ $Reply - 1 ] ;
2145
2146 $Heading="\n**** File Menu ****" ;
2147 $Prompt="Choose files to list:" ;
2148 select $type ( @types ) { # ^D restarts OUTER
2149 $user_type=$typ_opt[ $Reply - 1 ] ;
2150 last OUTER ; # leave loops once final choice obtained
2151 }
2152 }
2153 $Eof and exit ; # handle ^D to OUTER
2154
2155 # Now construct user's command
2156 $command="ls $user_format $user_type" ;
2157
2158 # Show command, for educational value
2159 warn "\nPress <ENTER> to execute \"$command\"\n" ;
2160  
2161 # Now wait for input, then run command
2162 defined <> or print "\n" and exit ;
2163
2164 system $command ; # finally, run the command
2165
2166 B<Screen>
2167  
2168 menu_ls.plx
2169
2170 ** LS-Command Composer **
2171
2172 1) regular 2) long
2173 Choose listing format: 2
2174
2175 1) only non-hidden 2) all files
2176 Choose files to list: 2
2177
2178 Press <ENTER> to execute "ls -l -a" <ENTER>
2179  
2180 total 13439
2181 -rw-r--r-- 1 yumpy gurus 1083 Feb 4 15:41 README
2182 -rw-rw-r-- 6 yumpy gurus 277 Dec 17 14:36 .exrc.mmkeys
2183 -rw-rw-r-- 7 yumpy gurus 285 Jan 16 18:45 .exrc.podkeys
2184 $
2185  
2186 =head1 BUGS
2187  
2188 =head2 UNIX Orientation
2189  
2190 I've been a UNIX programmer since 1976, and a Linux proponent since
2191 1992, so it's most natural for me to program for those platforms.
2192 Accordingly, this early release has some minor features that are only
2193 allowed, or perhaps only entirely functional, on UNIX-like systems.
2194 I'm open to suggestions on how to implement some of these features in
2195 a more portable manner.
2196  
2197 Some of the programming examples are also
2198 UNIX oriented, but it should be easy enough for those specializing on
2199 other platforms to make the necessary adapations. 8-}
2200  
2201 =head2 Terminal Display Modes
2202  
2203 These have been tested under UNIX/Linux, and work as expected,
2204 using B<tput>. When time permits, I'll convert to a portable
2205 implementation that will support other OSs.
2206  
2207 =head2 Incorrect Line Numbers in Warnings
2208  
2209 Because this module inserts new source code into your program,
2210 Perl messages that reference line numbers will refer to a
2211 different source file than you wrote. For this reason,
2212 only messages referring to lines before the first C<select>
2213 loop in your program will be correct.
2214  
2215 If you're on a UNIX-like system, by enabling the C<debugging>
2216 and C<logging> options (see L<"Debug"> and L<"Logging">), you can
2217 get an on-screen report of the proper offset to apply to interpret
2218 the line numbers of the source code that gets dumped to the
2219 F</tmp/SELECT_source> file. Of course, if everything works correctly,
2220 you'll have little reason to look at the source. 8-}
2221  
2222 =head2 Please Report Bugs!
2223  
2224 This is a non-trivial program, that does some fairly complex parsing
2225 and data munging,
2226 so I'm sure there are some latent bugs awaiting your discovery.
2227 Please share them with me, by emailing the offending code,
2228 and/or the diagnostic messages enabled by the I<debug>
2229 option setting (see L<"IMPORTS AND OPTIONS">).
2230  
2231 =head1 TODO-LIST
2232  
2233 =head2 More Shell-like Menus
2234  
2235 In a future release, there could be options for
2236 more accurately emulating Bash and Korn-style behavior,
2237 if anybody cares (the main difference is in how the
2238 items are ordered in the menus).
2239  
2240 =head2 More Extensive Test Suite
2241  
2242 More tests are needed, especially for the complex and tricky cases.
2243  
2244 =head1 MODULE DEPENDENCIES
2245  
2246 File::Spec::Functions
2247 Text::Balanced
2248 Filter::Simple
2249  
2250 =head1 EXPORTS: Default
2251  
2252 $Reply
2253  
2254 This variable is C<local>ized to each C<select> loop,
2255 and provides the menu-number of the most recent valid selection.
2256 For an example of its use, see L<"menu_ls.plx">.
2257  
2258 =head1 EXPORTS: Optional
2259  
2260 $Heading
2261 $Prompt
2262 $MaxColumns
2263 $Eof
2264  
2265 See L<"IMPORTS AND OPTIONS"> for details.
2266  
2267 =head1 SCRIPTS
2268  
2269 browse_images
2270 browse_jpeg
2271 browse_records
2272 delete_file
2273 lc_filename
2274 long_listem
2275 menu_ls
2276 order
2277 perl_man
2278 pick
2279 pick_file
2280  
2281 =head1 AUTHOR
2282  
2283 Tim Maher
2284  
2285 =head1 MAINTAINER
2286  
2287 Martin Thurn
2288 mthurn@cpan.org
2289  
2290 =begin html
2291  
2292 -
2293  
2294 =end html
2295  
2296 =begin HTML
2297  
2298 +
2299  
2300 =end HTML
2301  
2302 =head1 ACKNOWLEDGEMENTS
2303  
2304 I probably never would have even attempted to write this module
2305 if it weren't for the provision of Filter::Simple by Damian Conway,
2306 which I ruthlessly exploited to make a hard job easy.
2307  
2308 I<The Damian> also gave useful tips
2309 during the module's development, for which I'm grateful.
2310  
2311 I I<definitely> wouldn't have ever written this module, if I hadn't
2312 found myself writing a chapter on I<Looping> for my upcoming
2313 B<Manning Publications> book,
2314 and once again lamenting the fact that the most friendly Shell loop
2315 was still missing from Perl.
2316 So in a fit of zeal, I vowed to rectify that oversight!
2317  
2318 I hope you find this module as useful as I do! 8-}
2319  
2320 For more examples of how this loop can be used in Perl programs,
2321 watch for my upcoming book, I<Minimal Perl: for Shell Users and Programmers>
2322 (see
2323 L<http://teachmeperl.com/mp4sh.html>) in early fall, 2003.
2324  
2325 =head1 SEE ALSO
2326  
2327 man ksh # on UNIX or UNIX-like systems
2328  
2329 man bash # on UNIX or UNIX-like systems
2330  
2331 =head1 DON'T SEE ALSO
2332  
2333 B<perldoc -f select>, which has nothing to do with this module
2334 (the names just happen to match up).
2335  
2336 =head1 VERSION
2337  
2338 This document describes version 0.08.
2339  
2340 =head1 LICENSE
2341  
2342 Copyright (C) 2002-2003, Timothy F. Maher. All rights reserved.
2343  
2344 This module is free software;
2345 you can redistribute it and/or modify it under the same terms as Perl itself.
2346  
2347 =cut
2348  
2349 # vi:ts=2 sw=2:
2350  
2351 26 1;
2352  
2353 # Tim Maher, tim@teachmeperl.com, yumpy@cpan.org
2354 # Fri May 2 10:29:25 PDT 2003
2355 # Mon May 5 10:51:49 PDT 2003
2356  
2357 __END__
↑ Summary Test_Progs/sub2 »