ShowStuff.pm

filter: Statementstmt 9.5% 93 / 973 Branchbran 0.0% 0 / 400 Conditioncond 0.0% 0 / 93 MC/DCmcdc 0.0% 0 / 54 Subroutinesub 22.3% 27 / 121 Podpod 58.3% 35 / 60 totaltotal 9.1% 155 / 1701 CC 257 SCAR 52.7
CC
257
Cov
6%
showref
1122.0
showhashplain
600.0
showsth_web
306.0
CRAP 193.9
↑ Summary ShowStuff/ShowVar.pm »
1 package Debug::ShowStuff;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Tie::IxHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use String::Util ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 require 5.005; # require at least Perl version 5.005
7  
8 # version
9 1 our $VERSION = '1.16';
10  
11  
12 # allow for forced context
13 # This is a change to how Debug::ShowStuff used to work. It used to be that
14 # the output of a function (e.g. println) was returned as a scalar if the
15 # function call was made in an array context. This got confusing when the
16 # call was the last call in a function. I'm changing it so that, by default,
17 # the function always outputs to the standard file handle (usually STDOUT).
18 1 our $always_void = 1;
19  
20 # allow for forced web/plain
21 1 ▶use vars qw[ $forceweb ];
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 # global output file handle
24 # default $out to STDOUT
25 1 our $out;
26 1 $out = *STDOUT;
27  
28 # default $out to STDOUT
29 1 ▶use vars qw[ $active ];
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 $active = 1;
31  
32 # INDENT: how far to indent a line
33 1 our $indent_level = 0;
34 1 my $indent_tab = ' ';
35  
36 # constants
37 1 ▶use constant STDTABLE => qq|<p>\n<table style="STYLE" border="4" rules="rows" cellspacing="0" cellpadding="3">\n|;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39  
40 #---------------------------------------------------------------------
41 # export
42 #
43 1 ▶use vars qw[@EXPORT_OK %EXPORT_TAGS @ISA];
Subroutine BEGIN: called CC=1 SCAR=0.0
44 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 1 push @ISA, 'Exporter';
46  
47 1 @EXPORT_OK = qw[
48 showhash showhashhtml showhashplain
49 showarray showarr
50 showarraydiv showarrdiv
51 showcgi
52 showscalar showsca
53 showref
54 showstderr
55 setoutput
56 pressenter
57 inweb
58 httpheader
59 httpheaders
60 println
61 printnorm
62 printhr
63 preln
64 dieln
65 devexit
66 diearr
67 fixundef
68 diearr
69 findininc
70 confirm
71 output_to_file
72 define_show
73 showtainted
74 showstuff
75 indent
76 timer
77 backtrace
78 dietrace
79 autoshow
80
81 forceenv
82 forcetext
83 forceweb
84 forcenone
85
86 showisa
87
88 showsth
89 showsql
90 explainsql
91 ];
92  
93 1 %EXPORT_TAGS = ('all' => [@EXPORT_OK]);
94 #
95 # export
96 #---------------------------------------------------------------------
97  
98  
99 #------------------------------------------------------------------------------
100 # opening POD
101 #
102  
103 =head1 NAME
104  
105 Debug::ShowStuff - A collection of handy debugging routines for displaying
106 the values of variables with a minimum of coding.
107  
108 =head1 SYNOPSIS
109  
110 Here's a sampling of a few of my favorite functions in this module.
111  
112 use Debug::ShowStuff ':all';
113
114 # display values of a hash or hash reference
115 showhash %hash;
116 showhash $hashref;
117
118 # display values of an array or array reference
119 showarr @arr;
120 showarr $arrref;
121
122 # show all the params received through CGI
123 showcgi();
124
125 # A particularly fancy utility: display STDERR at top of web page
126 my $warnings = showstderr;
127  
128  
129 =head1 INSTALLATION
130  
131 C<Debug::ShowStuff> can be installed with the usual routine:
132  
133 perl Makefile.PL
134 make
135 make test
136 make install
137  
138 =head1 DESCRIPTION
139  
140 C<Debug::ShowStuff> grew dynamically from my needs in debugging code. I found
141 myself doing the same tasks over and over... displaying the keys and
142 values in a hash, displaying the elements in an array, displaying the output
143 of STDERR in a web page, etc. C<Debug::ShowStuff> began as two or three of my
144 favorite routines and grew as I added to that collection. Finally I decided
145 to publish these tools in the hope that other Perl programmers will find
146 them useful.
147  
148 =head2 Not for production work
149  
150 C<Debug::ShowStuff> is for debugging, not for production work. It does not
151 always output the actual value of something, but rather information B<about>
152 the value. For example, the following code outputs the actual value in the
153 first line, but a note about the value in the second.
154  
155 println 'my value';
156 println undef;
157  
158 which outputs
159  
160 my value
161 [undef]
162  
163 I would discourage you from using C<Debug::ShowStuff> in production code.
164 C<Debug::ShowStuff> is only for quick-n-dirty displays of variable values in
165 order to debug your code.
166  
167 =head2 Text and web modes
168  
169 The functions in C<Debug::ShowStuff> are designed to output either in plain
170 text mode (like if you're running the script from a command prompt), or in web
171 mode (like from a CGI). If the script appears to be running in a CGI or other
172 web mode (see the C<inweb> function) then values are output using HTML, with
173 special HTML characters escaped for proper display. Othewise the values are
174 output as they are.
175  
176 Generally you won't need to bother telling C<Debug::ShowStuff> if you're in
177 text or web mode... it figures it out on its own.
178  
179 =head2 Dynamic output/return: different than previous versions
180  
181 NOTE: The dynamic behavior of "show" functions has changed since earlier
182 versions of Debug::ShowStuff. "show" functions now always outputs to STDOUT
183 or STDERR unless $Debug::ShowStuff::always_void is set to false. By default
184 $always_void is true.
185  
186 If $always_void is false, then the following applies:
187  
188 The functions that start with "show" dynamically either output to STDOUT or
189 STDERR or return a string, depending on the context in which the
190 functions are called. For example, if you call showhash in a void context:
191  
192 showhash %myhash;
193  
194 then the contents of %myhash are output to STDOUT. On the other hand, if the
195 function is called in scalar context:
196  
197 my $var = showhash(%myhash);
198  
199 then the same string that would have been output to STDOUT is instead
200 returned and stored in $var.
201  
202 By default, output is sent to STDOUT, not STDERR. You can change the
203 default output to STDERR using the C<setoutput> command. See the docs
204 for that command for more detail.
205  
206 =head2 Displaying "invisible" strings
207  
208 To facilitate telling the difference between C<[undef]> and an empty string,
209 functions output the strings "[undef]" and "[empty string]". So, for example,
210 this code:
211  
212 println undef;
213 println "";
214  
215 produces this:
216  
217 [undef]
218 [empty string]
219  
220 =head1 FUNCTION DESCRIPTIONS
221  
222 =cut
223  
224 #
225 # opening POD
226 #------------------------------------------------------------------------------
227  
228  
229 #------------------------------------------------------------------------------
230 # default
231 # private subroutine
232 #
233 0 ▶sub default {
Subroutine default: not called CC=3 SCAR=24.8
Pod undocumented
234 0 ▶ for (my $i=0; $i<=$#_; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#_) 0 0 0
235 0 ▶ defined($_[$i]) and return $_[$i];
Branch: 1 branch
BranchTrueFalseTotal
defined $_[$i] and return $_[$i] 0 0 0
236 }
237
238 0 return undef;
239 }
240 #
241 #------------------------------------------------------------------------------
242  
243  
244  
245 #------------------------------------------------------------------------------
246 # println
247 #
248  
249 =head2 println
250  
251 C<println> was the original Debug::ShowStuff function. It simply outputs the
252 given values.
253  
254 In text mode it adds a newline to the end.
255  
256 For example, this code:
257  
258 println "hello world";
259  
260 produces this output, including the newline:
261  
262 hello world
263  
264 In L<web mode|/"Text and web modes"> it puts the output inside a <p> element. The values are HTML
265 escaped so that HTML-significant characters like < and > are actually
266 displayed as < and >. The <p> element has CSS styles set so that the
267 characters are always black, the background is always white, text is
268 left-aligned, and the <p> element has a black border, regardless of the styles
269 of the surrounding elements. So, for example, in web mode, the following code:
270  
271 println 'whatever';
272  
273 outputs the following HTML:
274  
275 <p style="background-color:white;color:black;text-align:left">whatever</p>
276  
277 Like other "show" functions, undef is output as the string "[undef]" and
278 an empty string is output as the string "[empty string]".
279  
280 Values in the arguments array are output concatenated together with no
281 spaces in between. Each value is evaluated independently for if it
282 is undef, empty string, or a string with visible content. So, for example,
283 this code:
284  
285 println "whatever", "", undef, "dude";
286  
287 outputs this:
288  
289 whatever[empty string][undef]dude
290  
291 =cut
292  
293 # And after all that documentation, the sub is only one line long. println
294 # actually calls the private sub "printer" with a few extra arguments. println
295 # shares printer with printnorm.
296  
297 0 ▶sub println {
Subroutine println: not called CC=1 SCAR=6.9
Pod documented
298 0 return printer (wantarray(), 'p', "\n", @_);
299 }
300 #
301 # println
302 #------------------------------------------------------------------------------
303  
304  
305 #------------------------------------------------------------------------------
306 # indent
307 #
308  
309 =head2 indent()
310  
311 C<indent()> is for situations where you're outputting a lot of stuff and you
312 want to tidy up the putput with indentation.
313 In L<text mode|/"Text and web modes"> the output is indented with 3 spaces per
314 indentation level. In web mode the output is indented with 20px per
315 indentation level.
316  
317 C<indent()> must be assigned to a variable or it has no effect. C<indent()>
318 increases the indent level by one. When the variable goes out of scope, the
319 indent level is decreased by one.
320  
321 For example suppose you want to display the values of records from a database.
322 You might loop through the records, outputting them like this:
323  
324 while (my $record = $sth->fetchrow_hashref) {
325 println $record->{'name_nick'};
326 my $indent = indent();
327 showhash $record;
328 }
329  
330 That would produce output something like this:
331  
332 Ricky
333 ---------------------------------------
334 name_first = Rick
335 name_last = Adams
336 ---------------------------------------
337  
338 Dan
339 ---------------------------------------
340 name_first = Daniel
341 name_last = Bradley
342 ---------------------------------------
343  
344 By default, three spaces are used to indent. To change that set
345 $Debug::ShowStuff::indent_tab to whatever string you want to use for
346 indentation.
347  
348 B<option:> bottom_space
349  
350 The C<bottom_space> option indicates to output an extra line at the bottom
351 of the indented output, just to give some extra division before the next
352 batch of code. For example, the following code does not use C<bottom_space>:
353  
354 foreach my $name (qw[Larry Moe]) {
355 println $name;
356 my $indent = indent(bottom_space=>1);
357 println 'years: ', length($name);
358 }
359  
360 and so produces the following output:
361  
362 Larry
363 years: 5
364 Moe
365 years: 3
366  
367 But this code:
368  
369 foreach my $name (qw[Larry Moe]) {
370 println $name;
371 my $indent = indent(bottom_space=>1);
372 println 'years: ', length($name);
373 }
374  
375 produces this output:
376  
377 Larry
378 years: 5
379  
380 Moe
381 years: 3
382  
383 =cut
384  
385 0 ▶sub indent {
Subroutine indent: not called CC=1 SCAR=6.9
Pod documented
386 0 my (%opts) = @_;
387 0 return Debug::ShowStuff::Indent->new(%opts);
388 }
389 #
390 # indent
391 #------------------------------------------------------------------------------
392  
393  
394 #------------------------------------------------------------------------------
395 # showstuff
396 #
397  
398 =head2 showstuff()
399  
400 This function turns on/off most of the functions in this module, with one
401 important exception explained below. The function also returns the state of
402 whether or not Debug::ShowStuff is on/off.
403  
404 If a parameter is sent, that param is used to turn display on/off. The
405 value is stored in the global variable $Debug::ShowStuff::active.
406  
407 The function is also used by most subroutines to determine if they should
408 actually output anything. $Debug::ShowStuff::active is not the only criteria
409 used to determine if Debug::ShowStuff is active. The algorithm is as follows:
410  
411 - If the environment variable $ENV{'SHOWSTUFF'} is defined and false then
412 showstuff() returns false regardless of the state of $active.
413  
414 - If the environment variable $ENV{'SHOWSTUFF'} is not defined or is defined
415 and true then showstuff() uses $Debug::ShowStuff::active to determine on/off.
416  
417 The purpose of this algorithm is to allow the use of debugging display in
418 situations where one perl script calls another, such as in regression testing.
419  
420 For example, suppose you have a script as follows:
421  
422 #!/usr/bin/perl -w
423 use strict;
424 use Debug::ShowStuff ':all';
425
426 my ($rv);
427
428 println 'running my_function()';
429 $rv = my_function();
430 println 'the returned value is: ', $rv;
431
432 $rv or die 'error!';
433  
434 The output of the script might look something like this:
435  
436 running my_function()
437 1
438  
439 Now suppose you call that and other scripts from some OTHER script, and
440 you don't want the screen cluttered with all that debugging, you just want
441 to see if all those scripts run successfully. You could use $ENV{'SHOWSTUFF'}
442 to turn off showing stuff:
443  
444 #!/usr/bin/perl -w
445 use strict;
446 use Debug::ShowStuff ':all';
447
448 my @tests = ("./script1.pl", "./script2.pl", "./script3.pl");
449 $ENV{'SHOWSTUFF'} = 0;
450
451 foreach my $test () {
452 system($test) and die "$test failed";
453 }
454  
455 In that case, none of the stuff from the test scripts would be output.
456  
457 =cut
458  
459 0 ▶sub showstuff {
Subroutine showstuff: not called CC=4 SCAR=30.0
Pod documented
460 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
461 0 { $active = $_[0] }
462
463 # if SHOWSTUFF environment variable is defined and false
464 0 ▶ if ( defined($ENV{'SHOWSTUFF'}) && (! $ENV{'SHOWSTUFF'}) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{"SHOWSTUFF"} and not $ENV{"SHOWSTUFF"}) 0 0 0
Condition0%0/3
defined $ENV{"SHOWSTUFF"} and not $ENV{"SHOWSTUFF"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ENV{"SHOWSTUFF"} and not $ENV{"SHOWSTUFF"}
defined $ENV{"SHOWSTUFF"} not $ENV{"SHOWSTUFF"}
Truth table0%0/3
A and not(B)
A= defined $ENV{"SHOWSTUFF"}B= not $ENV{"SHOWSTUFF"}
ABresult
0X0
100
111
465 0 { return 0 }
466
467 # else use $active
468 0 return $active;
469 }
470 #
471 # showstuff
472 #------------------------------------------------------------------------------
473  
474  
475  
476 #------------------------------------------------------------------------------
477 # htmlesc
478 #
479 # Private sub. Formats a string for literal output in HTML. An undefined
480 # first argument is returned as an empty string.
481 #
482 # sub htmlesc {
483 # my ($rv) = @_;
484 # return '' unless defined($rv);
485 # $rv =~ s|&|&#38;|g;
486 # $rv =~ s|"|&#34;|g;
487 # $rv =~ s|'|&#39;|g;
488 # $rv =~ s|<|&#60;|g;
489 # $rv =~ s|>|&#62;|g;
490 # return $rv;
491 # }
492 #
493 # htmlesc
494 #------------------------------------------------------------------------------
495  
496  
497  
498 #------------------------------------------------------------------------------
499 # printnorm
500 #
501  
502 =head2 printnorm
503  
504 Works like println but doesn't add trailing newline. In web environment uses
505 <span> instead of <p>.
506  
507 =cut
508  
509 0 ▶sub printnorm {
Subroutine printnorm: not called CC=1 SCAR=6.9
Pod documented
510 0 return printer (wantarray(), 'span', '', @_);
511 }
512 #
513 # printnorm
514 #------------------------------------------------------------------------------
515  
516  
517 #------------------------------------------------------------------------------
518 # printer
519 # Private sub: called by both println and printnorm
520 #
521 0 ▶sub printer {
Subroutine printer: not called CC=6 SCAR=37.4
Pod undocumented
522 0 my ($context, $html_el, $newline, @args) = @_;
523
524 0 ▶ showstuff() or return '';
Branch: 1 branch
BranchTrueFalseTotal
showstuff() or return "" 0 0 0
525
526 0 my ($str);
527 0 my $fh = getfh($context);
528
529 # special case: no arguments: just output an eol and return
530 0 ▶ if (! @args) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@args) 0 0 0
531 0 print $fh "\n";
532 0 return;
533 }
534
535 # get final string, including stuff like [undef]
536 0 $str = join('', map {define_show($_)} @args);
537
538 # print as web page
539 0 ▶ if (inweb()) {
Branch: 1 branch
BranchTrueFalseTotal
if (inweb()) { } 0 0 0
540 0 my $indent = web_indent();
541
542 0 print $fh
543 qq|<$html_el style="${indent}background-color:white;color:black;text-align:left">|,
544 htmlesc($str),
545 "</$html_el>\n";
546 }
547
548 # else print as text
549 # add indents at start and at every newline
550 else {
551 0 my ($indent);
552 0 $indent = '';
553
554 0 for (1..$indent_level)
555 0 { $indent .= $indent_tab }
556
557 0 $str =~ s|\n|\n$indent|gs;
558 0 $str = $indent . $str;
559
560 0 print $fh $str, $newline;
561 }
562
563 0 ▶ ismem($fh) and return $fh->mem();
Branch: 1 branch
BranchTrueFalseTotal
ismem($fh) and return $fh->mem 0 0 0
564 }
565 #
566 # printer
567 #------------------------------------------------------------------------------
568  
569  
570 #------------------------------------------------------------------------------
571 # web_indent
572 #
573  
574 # constant: how many pixels for a single web indent
575 1 ▶use constant WEB_INDENT => 20;
Subroutine BEGIN: called CC=1 SCAR=0.0
576  
577 0 ▶sub web_indent {
Subroutine web_indent: not called CC=2 SCAR=17.9
Pod undocumented
578 0 my ($indent);
579
580 0 ▶ if ($indent_level) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Debug::ShowStuff::indent_level) { } 0 0 0
581 0 $indent =
582 'margin-left:' .
583 ($indent_level * WEB_INDENT) .
584 ';';
585 }
586 else {
587 0 $indent = '';
588 }
589
590 0 return $indent;
591 }
592 #
593 # web_indent
594 #------------------------------------------------------------------------------
595  
596  
597 #------------------------------------------------------------------------------
598 # showhash
599 #
600  
601 =head2 showhash
602  
603 Displays the keys and values in a hash. Input is either a single hash
604 reference or a regular hash. The key=values pairs are sorted by the names
605 of the keys.
606  
607 So, for example, the following code:
608  
609 my %hash = (
610 Larry => 'curly headed guy',
611 Curly => 'funny bald guy',
612 Moe => 'guy in charge',
613 );
614  
615 showhash %hash;
616  
617 Produces the following output. Notice that the keys are in alphabetic order:
618  
619 ---------------------------------------
620 Curly = funny bald guy
621 Larry = curly headed guy
622 Moe = guy in charge
623 ---------------------------------------
624  
625 This code, using a hash reference, produces exactly the same output:
626  
627 my $hash = {
628 Larry => 'curly headed guy',
629 Curly => 'funny bald guy',
630 Moe => 'guy in charge',
631 };
632  
633 showhash $hash;
634  
635 If the hash is empty, then that fact is output. So, this code:
636  
637 showhash {};
638  
639 produces this output:
640  
641 ---------------------------------------
642 [empty hash]
643 ---------------------------------------
644  
645 If an undef value is sent instead of a hashref, then that fact is displayed
646 instead of a hash. For example consider the following code that uses
647 a variable that is undef:
648  
649 my ($hash);
650 showhash $hash;
651  
652 That code produces this output:
653  
654 ---------------------------------------
655 Only one element input and it was undefined
656 ---------------------------------------
657  
658 Optional arguments only come into play if the first argument is a hashref.
659  
660 B<option:> title => "string"
661  
662 If this option is sent, the string is displayed at the top of the
663 display of the hash values. So this code:
664  
665 my $hash = {
666 Larry => 'curly headed guy',
667 Curly => 'funny bald guy',
668 Moe => 'guy in charge',
669 };
670  
671 showhash $hash, title=>'Stooges';
672  
673 produces this output:
674  
675 --- Stooges ---------------------------------
676 Curly = funny bald guy
677 Larry = curly headed guy
678 Moe = guy in charge
679 ---------------------------------------------
680  
681  
682 B<option:> line_cut => 1
683  
684 If the C<line_cut> option is sent, then each value is truncated after the first
685 newline if there is one. The fact that there is more output is mentioned. So
686 the following code:
687  
688 my $hash = {
689 Larry => "curly\nheaded guy",
690 Curly => "funny\nbald guy",
691 Moe => "guy\nin charge",
692 };
693  
694 showhash $hash, line_cut =>1;
695  
696 produces this output.
697  
698 ---------------------------------------
699 Curly = funny [more lines...]
700 Larry = curly [more lines...]
701 Moe = guy [more lines...]
702 ---------------------------------------
703  
704 Several other options do exactly the same thing: linecut, line_chop, and first_line.
705  
706 =cut
707  
708 0 ▶sub showhash {
Subroutine showhash: not called CC=3 SCAR=24.8
Pod documented
709 0 ▶ showstuff() or return '';
Branch: 1 branch
BranchTrueFalseTotal
showstuff() or return "" 0 0 0
710
711 # HTML
712 0 ▶ if (inweb())
Branch: 1 branch
BranchTrueFalseTotal
if (inweb()) { } 0 0 0
713 0 { return showhashhtml(@_) }
714
715 # plain text
716 else
717 0 { return showhashplain(@_) }
718 }
719 #
720 # showhash
721 #------------------------------------------------------------------------------
722  
723  
724 #------------------------------------------------------------------------------
725 # showhashhtml
726 #
727 # Private sub. Displays the keys and values in a hash, formatted for HTML.
728 #
729 # Code in this sub is not indented because it uses a lot of here documents.
730 #
731 0 ▶sub showhashhtml {
Subroutine showhashhtml: not called CC=16 SCAR=56.1
Pod undocumented
732  
733 0 my %myhash;
734 0 my $maxkey = 0;
735 0 my $maxval = 0;
736 0 my $fh = getfh(wantarray);
737 0 my $indent = web_indent();
738 0 my (%opts, $linecut, @keys, $id_prefix, $table_tag);
739  
740 # open table
741 0 $table_tag = STDTABLE;
742 0 $table_tag =~ s|STYLE|$indent|s;
743 0 print $fh $table_tag;
744  
745 # special case: only one element and it's undefined
746 0 ▶if ( (@_ == 1) && (! defined($_[0])) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and not defined $_[0]) 0 0 0
Condition0%0/3
@_ == 1 and not defined $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 1 and not defined $_[0]
@_ == 1 not defined $_[0]
Truth table0%0/3
A and not(B)
A= @_ == 1B= not defined $_[0]
ABresult
0X0
100
111
747 0 print $fh "<tr><td>Only element input and it was undefined</td></tr></table>\n";
748 0 return;
749 }
750  
751 0 ▶if (ref $_[0]){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 0 0 0
752 0 %myhash = %{$_[0]};
753 0 %opts = @_[1..$#_];
754 0 ▶ $linecut = $opts{'line_cut'} || $opts{'linecut'} || $opts{'line_chop'} || $opts{'first_line'};
Condition0%0/9
$opts{'line_cut'} || $opts{'linecut'} || $opts{'line_chop'} || $opts{'first_line'}
l!l&&r!l&&!r
000
$opts{"line_cut"} or $opts{"linecut"} or $opts{"line_chop"}
l!l&&r!l&&!r
000
$opts{"line_cut"} or $opts{"linecut"}
l!l&&r!l&&!r
000
MC/DC0%0/4
$opts{'line_cut'} || $opts{'linecut'} || $opts{'line_chop'} || $opts{'first_line'}
$opts{"line_cut"} $opts{"linecut"} $opts{"line_chop"} $opts{'first_line'}
Truth table0%0/5
A or B or C || D
A= $opts{"line_cut"}B= $opts{"linecut"}C= $opts{"line_chop"}D= $opts{'first_line'}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
755 }
756 else {
757 0 %myhash = @_;
758 }
759  
760 0 ▶if (ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) 0 0 0
761 0 my $title;
762
763 0 ▶ if (defined $opts{'title'})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{'title'}) { } 0 0 0
764 0 {$title = $opts{'title'}}
765 else
766 0 {$title = $_[0]}
767
768 0 print $fh '<tr><th colspan="3">', htmlesc($title), "</th></tr>\n"
769 }
770  
771 0 print $fh <<"(TABLETOP2)";
772 <tr bgcolor="navy">
773 <th style="background-color:navy;color:white">key</th>
774 <td>&nbsp;&nbsp;&nbsp;</td>
775 <th style="background-color:navy;color:white">value</th>
776 </tr>
777 (TABLETOP2)
778  
779 # get array of keys
780 0 @keys = keys(%myhash);
781  
782 # set ID prefix if opted to do so
783 0 $id_prefix = $opts{'id_prefix'};
784  
785 # sort keys unless sort param is sent as false
786 0 ▶if (default $opts{'sort'}, 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (default($opts{"sort"}, 1)) 0 0 0
787 0 @keys = sort { lc($a) cmp lc($b) } @keys;
788 }
789  
790 # loop through keys, outputting key=value pairs
791 0 foreach my $key (@keys) {
792 0 my $val = $myhash{$key};
793
794 0 ▶ if ($linecut){
Branch: 1 branch
BranchTrueFalseTotal
if ($linecut) 0 0 0
795 0 $val =~ s|\n.*||s;
796 0 $val .= '<b>...</b>';
797 }
798
799 # $id_prefix
800
801 0 print $fh
802 '<tr valign="top">',
803 '<td>', htmlesc($key), '</td>',
804 '<td></td>',
805 '<td>';
806
807 0 ▶ if (defined $id_prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id_prefix) 0 0 0
808 0 print $fh
809 '<span id="',
810 $id_prefix, '_',
811 htmlesc($key),
812 '">',
813 }
814
815 0 print $fh htmlesc($val);
816
817 0 ▶ if (defined $id_prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id_prefix) 0 0 0
818 0 print $fh '</span>',
819 }
820
821 0 print $fh '</td>', "</tr>\n";
822 }
823  
824 # if empty hash, output that fact
825 0 ▶if (! @keys) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@keys) 0 0 0
826 0 print $fh
827 '<tr>',
828 '<td align="center" colspan="3"><i>empty hash</i></td>',
829 "</tr>\n";
830 }
831  
832 # close table
833 0 print $fh "</table>\n<p>\n";
834  
835 0 ▶ismem($fh) and return $fh->mem;
Branch: 1 branch
BranchTrueFalseTotal
ismem($fh) and return $fh->mem 0 0 0
836 0 return '';
837 }
838 #
839 # showhashhtml
840 #------------------------------------------------------------------------------
841  
842  
843 #------------------------------------------------------------------------------
844 # showhashplain
845 #
846 # Private sub. Displays the keys and values in a hash, formatted using plain text.
847 #
848 0 ▶sub showhashplain {
Subroutine showhashplain: not called CC=24 SCAR=64.0
Pod undocumented
849 0 my %myhash;
850 0 my $maxkey = 0;
851 0 my $maxval = 0;
852 0 my $fh = getfh(wantarray);
853 0 my (%opts, $skipempty, %showfields, $linecut, $divider);
854
855 # get title
856 0 ▶ if (ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) 0 0 0
857 0 %myhash = %{$_[0]};
858 0 %opts = @_[1..$#_];
859
860 0 ▶ if (defined $opts{'title'})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"title"}) 0 0 0
861 0 {$divider = '--- ' . $opts{'title'} . ' ---------------------------------'}
862 }
863
864 0 ▶ if (! defined $divider)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $divider) 0 0 0
865 0 { $divider = '---------------------------------------' }
866
867 0 add_indents($fh);
868 0 print $fh $divider, "\n";
869  
870 0 $divider =~ s|.|\-|gs;
871
872 # special case: only one element and it's undefined
873 0 ▶ if ( (@_ == 1) && (! defined($_[0])) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and not defined $_[0]) 0 0 0
Condition0%0/3
@_ == 1 and not defined $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 1 and not defined $_[0]
@_ == 1 not defined $_[0]
Truth table0%0/3
A and not(B)
A= @_ == 1B= not defined $_[0]
ABresult
0X0
100
111
874 0 add_indents($fh);
875 0 print $fh "Only one element input and it was undefined\n";
876 0 add_indents($fh);
877 0 print $fh $divider, "\n\n";
878
879 0 ▶ ismem($fh) and return $fh->mem();
Branch: 1 branch
BranchTrueFalseTotal
ismem($fh) and return $fh->mem 0 0 0
880 0 return '';
881 }
882
883 # if first element is a reference, assume it's the hash to be displayed
884 0 ▶ if (ref $_[0]){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 0 0 0
885 0 ▶ $linecut = $opts{'line_cut'} || $opts{'line_chop'} || $opts{'first_line'};
Condition0%0/6
$opts{'line_cut'} || $opts{'line_chop'} || $opts{'first_line'}
l!l&&r!l&&!r
000
$opts{"line_cut"} or $opts{"line_chop"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$opts{'line_cut'} || $opts{'line_chop'} || $opts{'first_line'}
$opts{"line_cut"} $opts{"line_chop"} $opts{'first_line'}
Truth table0%0/4
A or B || C
A= $opts{"line_cut"}B= $opts{"line_chop"}C= $opts{'first_line'}
ABCresult
1XX1
01X1
0011
0000
886 0 $skipempty = $opts{'skipempty'};
887
888 0 ▶ if (defined $opts{'showfields'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"showfields"}) 0 0 0
889 0 ▶ if (ref $opts{'showfields'})
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts{'showfields'}) { } 0 0 0
890 0 {@showfields{@{$opts{'showfields'}}} = ()}
891 else
892 0 {$showfields{ $opts{'showfields'} } = ()}
893 }
894 }
895
896 # else all arguments form hash to be displayed
897 else {
898 0 %myhash = @_;
899 }
900
901 # if empty hash
902 0 ▶ if (! keys(%myhash)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not keys %myhash) { } 0 0 0
903 0 add_indents($fh);
904 0 print $fh "[empty hash]\n";
905 }
906
907 # not empty hash
908 else {
909 0 my ($keywidth, @keys);
910 0 $keywidth = longestkey(\%myhash);
911
912 0 @keys = keys(%myhash);
913
914 # sort keys unless sort param is sent as false
915 0 ▶ if (default $opts{'sort'}, 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (default($opts{"sort"}, 1)) 0 0 0
916 0 @keys = sort { lc($a) cmp lc($b) } @keys;
917 }
918
919 # @keys = sort { lc($a) cmp lc($b) } @keys;
920  
921 KEYLOOP:
922 0 foreach my $key (@keys) {
923 # if there's a specified list of fields to show
924 0 ▶ if ( %showfields && (! exists $showfields{$key}) )
Branch: 1 branch
BranchTrueFalseTotal
if (%showfields and not exists $showfields{$key}) 0 0 0
Condition0%0/3
%showfields and not exists $showfields{$key}
!ll&&!rl&&r
000
MC/DC0%0/2
%showfields and not exists $showfields{$key}
%showfields not exists $showfields{$key}
Truth table0%0/3
A and not(B)
A= %showfieldsB= not exists $showfields{$key}
ABresult
0X0
100
111
925 0 {next KEYLOOP}
926
927 0 my $value = $myhash{$key};
928
929 # if we should skip empty values
930 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($skipempty and not defined $value && $value =~ /\S/s) 0 0 0
Condition0%0/6
$skipempty and not defined $value && $value =~ /\S/s
!ll&&!rl&&r
000
defined $value && $value =~ /\S/s
!ll&&!rl&&r
000
MC/DC0%0/3
$skipempty and not defined $value && $value =~ /\S/s
$skipempty defined $value $value =~ /\S/s
Truth table0%0/4
A and not(B && C)
A= $skipemptyB= defined $valueC= $value =~ /\S/s
ABCresult
0XX0
1110
10X1
1101
931 $skipempty &&
932 (
933 (! defined $value) ||
934 ($value !~ m|\S|s)
935 )
936 )
937 0 { next KEYLOOP }
938
939 # linecut
940 0 ▶ if ($linecut) {
Branch: 1 branch
BranchTrueFalseTotal
if ($linecut) 0 0 0
941 0 ▶ if ( (defined $value) && ($value =~ s|[\r\n].*||s) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and $value =~ s/[\r\n].*//s) 0 0 0
Condition0%0/3
defined $value and $value =~ s/[\r\n].*//s
!ll&&!rl&&r
000
MC/DC0%0/2
defined $value and $value =~ s/[\r\n].*//s
defined $value $value =~ s/[\r\n].*//s
Truth table0%0/3
A and B
A= defined $valueB= $value =~ s/[\r\n].*//s
ABresult
0X0
100
111
942 0 { $value .= ' [more lines...]' }
943 }
944
945 0 add_indents($fh);
946 0 print $fh
947 spacepad($key, $keywidth), ' = ',
948 define_show($value), "\n";
949 }
950  
951 }
952
953 0 add_indents($fh);
954 0 print $fh $divider, "\n\n";
955
956 0 ▶ ismem($fh) and return $fh->mem();
Branch: 1 branch
BranchTrueFalseTotal
ismem($fh) and return $fh->mem 0 0 0
957 0 return '';
958 }
959 #
960 # showhashplain
961 #------------------------------------------------------------------------------
962  
963  
964 #------------------------------------------------------------------------------
965 # showarr, showarray
966 #
967  
968 =head2 showarr, showarray
969  
970 Displays the values of an array. c<showarr> and c<showarray>
971  
972 Each element is displayed in a table row (in L<web mode|/"Text and web modes">)
973 or on a separate line (in text mode).
974  
975 If C<showarray> receives exactly one argument, and if that item is an array
976 reference, then the routine assumes that you want to display the elements in
977 the referenced array. Therefore, the following blocks of code display the
978 same thing:
979  
980 showarray @myarr;
981 showarray \@myarr;
982  
983 =cut
984  
985 0 ▶sub showarray {
Subroutine showarray: not called CC=11 SCAR=48.8
Pod documented
986 0 ▶ showstuff() or return '';
Branch: 1 branch
BranchTrueFalseTotal
showstuff() or return "" 0 0 0
987
988 0 my (@arr) = @_;
989 0 my $fh = getfh(wantarray);
990
991 # if first and only element is an array ref, use that as full array
992 0 ▶ if ( (@arr == 1) && UNIVERSAL::isa($arr[0], 'ARRAY') )
Branch: 1 branch
BranchTrueFalseTotal
if (@arr == 1 and UNIVERSAL::isa($arr[0], "ARRAY")) 0 0 0
Condition0%0/3
@arr == 1 and UNIVERSAL::isa($arr[0], "ARRAY")
!ll&&!rl&&r
000
MC/DC0%0/2
@arr == 1 and UNIVERSAL::isa($arr[0], "ARRAY")
@arr == 1 UNIVERSAL::isa($arr[0], "ARRAY")
Truth table0%0/3
A and B
A= @arr == 1B= UNIVERSAL::isa($arr[0], "ARRAY")
ABresult
0X0
100
111
993 0 {@arr = @{$arr[0]}}
994
995 #------------------------------------------------------
996 # HTML
997 #
998 0 ▶ if (inweb()) {
Branch: 1 branch
BranchTrueFalseTotal
if (inweb()) { } 0 0 0
999 0 print $fh
1000 qq|<p>\n<table border="4" rules="rows" cellspacing="0" cellpadding="3">\n|,
1001 qq|<tr><th bgcolor="#aaaaff">array</th></tr>|;
1002
1003 0 foreach my $el (@arr){
1004 0 print $fh '<tr><td>';
1005
1006 0 ▶ if (defined $el){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $el) { } 0 0 0
1007 0 ▶ if ($el =~ m|\S|)
Branch: 1 branch
BranchTrueFalseTotal
if ($el =~ /\S/) { } 0 0 0
1008 0 { print $fh htmlesc($el) }
1009 else
1010 0 { print $fh '<i>no content string</i>' }
1011 }
1012
1013 else {
1014 0 print $fh '<i>undefined</i>';
1015 }
1016
1017 0 print $fh "</td></tr>\n";
1018 }
1019
1020 0 print $fh qq|</table>\n<p>\n|;
1021 }
1022 #
1023 # HTML
1024 #------------------------------------------------------
1025
1026
1027 #------------------------------------------------------
1028 # text
1029 #
1030 else {
1031 0 my $line = "------------------------------------\n";
1032 0 my ($firstdone);
1033
1034 0 add_indents($fh);
1035 0 print $fh $line;
1036
1037 0 foreach my $el (@arr){
1038 0 add_indents($fh);
1039 0 print $fh define_show($el), "\n";
1040
1041 0 $firstdone = 1;
1042 }
1043
1044
1045 0 ▶ if (! $firstdone) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($firstdone) 0 0 0
1046 0 add_indents($fh);
1047 0 print $fh "[empty array]\n";
1048 }
1049
1050 0 add_indents($fh);
1051 0 print $fh $line;
1052 }
1053 #
1054 # text
1055 #------------------------------------------------------
1056
1057 0 ▶ ismem($fh) and return $fh->mem();
Branch: 1 branch
BranchTrueFalseTotal
ismem($fh) and return $fh->mem 0 0 0
1058 }
1059  
1060 0 ▶sub showarr{showarray(@_)}
Subroutine showarr: not called CC=1 SCAR=6.9
Pod documented
1061 #
1062 # showarr, showarray
1063 #------------------------------------------------------------------------------
1064  
1065  
1066  
1067 #------------------------------------------------------------------------------
1068 # showarrdiv
1069 #
1070  
1071 =head2 showarraydiv
1072  
1073 Works just like C<showarray>, except that in text mode displays a solid line between
1074 each element of the array.
1075  
1076 =cut
1077  
1078 0 ▶sub showarraydiv {
Subroutine showarraydiv: not called CC=9 SCAR=45.0
Pod documented
1079 0 ▶ showstuff() or return '';
Branch: 1 branch
BranchTrueFalseTotal
showstuff() or return "" 0 0 0
1080
1081 0 my (@arr) = @_;
1082 0 my $fh = getfh(wantarray);
1083
1084 0 ▶ if ( (@arr == 1) && UNIVERSAL::isa($arr[0], 'ARRAY') )
Branch: 1 branch
BranchTrueFalseTotal
if (@arr == 1 and UNIVERSAL::isa($arr[0], "ARRAY")) 0 0 0
Condition0%0/3
@arr == 1 and UNIVERSAL::isa($arr[0], "ARRAY")
!ll&&!rl&&r
000
MC/DC0%0/2
@arr == 1 and UNIVERSAL::isa($arr[0], "ARRAY")
@arr == 1 UNIVERSAL::isa($arr[0], "ARRAY")
Truth table0%0/3
A and B
A= @arr == 1B= UNIVERSAL::isa($arr[0], "ARRAY")
ABresult
0X0
100
111
1085 0 {@arr = @{$arr[0]}}
1086
1087 #------------------------------------------------------
1088 # HTML
1089 #
1090 0 ▶ if (inweb()) {
Branch: 1 branch
BranchTrueFalseTotal
if (inweb()) { } 0 0 0
1091 0 print $fh
1092 "<P>\n<TABLE BORDER=4 RULES=ROWS CELLSPACING=0 CELLPADDING=3>\n",
1093 "<TR><TH BGCOLOR=\"#AAAAFF\">array</TH></TR>";
1094
1095 0 foreach my $el (@arr)
1096 0 {print $fh '<TR><TD>', htmlesc($el), "</TD></TR>\n"}
1097
1098 0 print $fh "</TABLE>\n<P>\n";
1099 }
1100 #
1101 # HTML
1102 #------------------------------------------------------
1103
1104
1105 #------------------------------------------------------
1106 # text
1107 #
1108 else {
1109 0 my $line = "------------------------------------\n";
1110 0 my ($firstdone);
1111
1112 0 print $fh $line;
1113
1114 0 foreach my $el (@arr){
1115 #if (defined $el)
1116 # {print $fh $el, "\n"}
1117 #else
1118 # {print $fh "[undef]\n"}
1119
1120 0 print $fh define_show($el);
1121
1122 0 print $fh $line;
1123 0 $firstdone = 1;
1124 }
1125
1126 0 ▶ if (! $firstdone)
Branch: 1 branch
BranchTrueFalseTotal
unless ($firstdone) 0 0 0
1127 0 {print $fh "[empty array]\n", $line}
1128 }
1129 #
1130 # text
1131 #------------------------------------------------------
1132
1133 0 ▶ ismem($fh) and return $fh->mem();
Branch: 1 branch
BranchTrueFalseTotal
ismem($fh) and return $fh->mem 0 0 0
1134 }
1135  
1136 0 ▶sub showarrdiv{showarraydiv(@_)}
Subroutine showarrdiv: not called CC=1 SCAR=6.9
Pod undocumented
1137 #
1138 # showarraydiv
1139 #------------------------------------------------------------------------------
1140  
1141  
1142  
1143 #------------------------------------------------------------------------------
1144 # showscalar
1145 #
1146  
1147 =head2 showscalar
1148  
1149 Outputs the value of a scalar. The name is slightly innaccurate: you can
1150 input an array. The array will be joined together to form a single scalar.
1151  
1152 Actually, I hardly ever use C<showscalar>, but it seemed unbalanced to have
1153 C<showhash> and C<showarray> without C<showscalar>.
1154  
1155 =cut
1156  
1157 0 ▶sub showscalar {
Subroutine showscalar: not called CC=6 SCAR=37.4
Pod documented
1158 0 ▶ showstuff() or return '';
Branch: 1 branch
BranchTrueFalseTotal
showstuff() or return "" 0 0 0
1159
1160 0 my (@arr) = @_;
1161 0 my $fh = getfh(wantarray);
1162
1163  
1164 #------------------------------------------------------
1165 # HTML
1166 #
1167 0 ▶ if (inweb()) {
Branch: 1 branch
BranchTrueFalseTotal
if (inweb()) { } 0 0 0
1168 0 print $fh
1169 "<P>\n<TABLE BORDER=4 RULES=ROWS CELLSPACING=0 CELLPADDING=3>\n",
1170 "<TR><TH BGCOLOR=\"#AAAAFF\">scalar</TH></TR><TR><TD><PRE>";
1171
1172 0 foreach my $el (@arr)
1173 0 {print $fh htmlesc($el)}
1174
1175 0 print $fh "</PRE></TD></TR></TABLE>\n<P>\n";
1176 }
1177 #
1178 # HTML
1179 #------------------------------------------------------
1180
1181
1182 #------------------------------------------------------
1183 # text
1184 #
1185 else {
1186 0 print $fh "------------------------------------\n";
1187
1188 0 ▶ if (@arr) {
Branch: 1 branch
BranchTrueFalseTotal
if (@arr) { } 0 0 0
1189 print $fh
1190 join(
1191 '',
1192 0 map {define_show($_)}
1193
1194 sort(
1195 {
1196 0 define_show($a) cmp
1197 define_show($b)
1198 } @arr
1199 )
1200 ),
1201
1202 "\n";
1203 }
1204
1205 # {print $fh join('', map {defined($_) ? $_ : '[undef]'} sort({(defined($a) ? $a : '') cmp (defined($b) ? $b : '')} @arr)), "\n"}
1206
1207 else {
1208 0 print $fh "[no elements]\n";
1209 }
1210
1211 0 print $fh "------------------------------------\n";
1212 }
1213 #
1214 # text
1215 #------------------------------------------------------
1216
1217 0 ▶ ismem($fh) and return $fh->mem();
Branch: 1 branch
BranchTrueFalseTotal
ismem($fh) and return $fh->mem 0 0 0
1218 }
1219  
1220 0 ▶sub showsca{showscalar(@_)}
Subroutine showsca: not called CC=1 SCAR=6.9
Pod undocumented
1221 #
1222 # showscalar
1223 #------------------------------------------------------------------------------
1224  
1225  
1226 #------------------------------------------------------------------------------
1227 # showcgi
1228 #
1229  
1230 =head2 showcgi
1231  
1232 Displays the CGI parameter keys and values. This sub always outputs HTML.
1233  
1234 There are several optional parameters, described in the following sections.
1235  
1236 B<option:> q
1237  
1238 The optional parameter C<q>, may be a CGI query object:
1239  
1240 my $query = CGI->new();
1241 showcgi q => $query;
1242  
1243 If C<q> is not sent, then a CGI object is created on the fly.
1244  
1245 B<option:> skipempty
1246  
1247 If the optional parameter C<skipempty> is true:
1248  
1249 showcgi skipempty => 1;
1250  
1251 then CGI params that are empty (i.e. do not have
1252 at least one non-space character) are not displayed.
1253  
1254 B<option:> skip
1255  
1256 C<skip> sets a list of parameters to not display. For example, if you don't
1257 want to see the C<choices> or C<toppings> params, then call showcgi like this:
1258  
1259 showcgi skip => ['choices', 'toppings'];
1260  
1261 Single item lists can be passed in directly without putting them in
1262 an anonymous array:
1263  
1264 showcgi skip => 'choices';
1265  
1266 =cut
1267  
1268 0 ▶sub showcgi {
Subroutine showcgi: not called CC=13 SCAR=52.0
Pod documented
1269 0 ▶showstuff() or return '';
Branch: 1 branch
BranchTrueFalseTotal
showstuff() or return "" 0 0 0
1270  
1271 0 my ($q, %opts) = @_;
1272 0 my (@keys, $fh, $skipempty, %skip, $table_tag);
1273 0 my $indent = web_indent();
1274  
1275 # $q = $opts{'r'} || $opts{'q'} || $opts{'cgi'} || CGI->new();
1276 0 $skipempty = $opts{'skipempty'};
1277  
1278 0 ▶if (defined $opts{'skip'})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"skip"}) 0 0 0
1279 0 {@skip{asarr($opts{'skip'})} = ()}
1280  
1281 0 @keys = sort $q->param;
1282 0 $fh = getfh(wantarray);
1283  
1284 # open table
1285 0 $table_tag = STDTABLE;
1286 0 $table_tag =~ s|STYLE|$indent|s;
1287 0 print $fh $table_tag;
1288  
1289 # title
1290 0 print $fh <<"(TABLETITLE)";
1291 <tr style="background-color:yellow">
1292 <th style="color:black" colspan="3">CGI params</th>
1293 </tr>
1294 (TABLETITLE)
1295  
1296  
1297 # special case: no elements
1298 0 ▶if (! @keys) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@keys) 0 0 0
1299 0 print $fh "<tr><td>No params</td></tr></table>\n";
1300 0 return;
1301 }
1302  
1303 0 print $fh <<"(TABLETOP2)";
1304 <tr style="background-color:navy">
1305 <th style="color:white">key</th>
1306 <td>&nbsp;&nbsp;&nbsp;</td>
1307 <th style="color:white">value</th>
1308 </tr>
1309 (TABLETOP2)
1310  
1311 PARAMLOOP:
1312 0 foreach my $key (@keys){
1313 0 my @vals = $q->param($key);
1314
1315 0 ▶ if (exists $skip{$key})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $skip{$key}) 0 0 0
1316 0 {next PARAMLOOP}
1317
1318 0 ▶ if ($skipempty && @vals <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skipempty and @vals <= 1) 0 0 0
Condition0%0/3
$skipempty and @vals <= 1
!ll&&!rl&&r
000
MC/DC0%0/2
$skipempty and @vals <= 1
$skipempty @vals <= 1
Truth table0%0/3
A and B
A= $skipemptyB= @vals <= 1
ABresult
0X0
100
111
1319 0 my $val = $vals[0];
1320
1321 0 ▶ if ( (! defined $val) || ($val !~ m|\S|) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val and $val =~ /\S/) 0 0 0
Condition0%0/3
defined $val and $val =~ /\S/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $val and $val =~ /\S/
defined $val $val =~ /\S/
Truth table0%0/3
A and B
A= defined $valB= $val =~ /\S/
ABresult
0X0
100
111
1322 0 {next PARAMLOOP}
1323 }
1324
1325 0 print $fh
1326 '<tr valign="top"><td>',
1327 htmlesc($key),
1328 '</td><td></td><td>';
1329
1330 0 ▶ if (@vals > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@vals > 1) { } 0 0 0
1331 # open table
1332 0 $table_tag = STDTABLE;
1333 0 $table_tag =~ s|STYLE||s;
1334 0 print $fh $table_tag;
1335
1336 0 foreach my $val (@vals) {
1337 0 print $fh
1338 '<tr><td>',
1339 htmlesc($val),
1340 "</td></tr>\n";
1341 }
1342
1343 0 print "</table>\n";
1344 }
1345
1346 else {
1347 0 print $fh
1348 htmlesc($vals[0]);
1349 }
1350
1351 0 print $fh "</td></tr>\n";
1352 }
1353  
1354 0 print $fh "</table>\n<p>\n";
1355  
1356 0 ▶ismem($fh) and return $fh->mem;
Branch: 1 branch
BranchTrueFalseTotal
ismem($fh) and return $fh->mem 0 0 0
1357 0 return '';
1358 }
1359 #
1360 # showcgi
1361 #------------------------------------------------------------------------------
1362  
1363  
1364 #------------------------------------------------------------------------------
1365 # showref
1366 #
1367  
1368 =head2 showref($ref, %options)
1369  
1370 Displays a hash, array, or scalar references, treeing down through other
1371 references it contains. So, for example, the following code:
1372  
1373 my $ob = {
1374 name => 'Raha',
1375 email => 'raha@idocs.com',
1376 friends => [
1377 'Shalom',
1378 'Joe',
1379 'Furocha',
1380 ],
1381 };
1382
1383 showref $ob;
1384  
1385 produces the following output:
1386  
1387 /-----------------------------------------------------------\
1388 friends =
1389 ARRAY
1390 Shalom
1391 Joe
1392 Furocha
1393 email = raha@idocs.com
1394 name = Raha
1395 \-----------------------------------------------------------/
1396  
1397 The values of the hash or arrays being referenced are only displayed once, so
1398 you're safe from infinite recursion.
1399  
1400 There are several optional parameters, described in the following sections.
1401  
1402 B<option:> maxhash
1403  
1404 The C<maxhash> option allows you to indicate the maximum number of hash
1405 elements to display. If a hash has more then C<maxhash> elements then none of
1406 them are displayed or recursed through, and instead an indicator of how many
1407 elements there are is output. So, for example, the following command only
1408 displays the hash values if there are 10 or fewer elements in the hash:
1409  
1410 showref $myob, maxhash=>10;
1411
1412  
1413 If C<maxhash> is not sent then there is no maximum.
1414  
1415 B<option:> maxarr
1416  
1417 The C<maxarr> option allows you to indicate the maximum number of array
1418 elements to display. If an array has more then C<maxarr> elements then none
1419 of them are displayed or recursed through, and instead an indicator of how
1420 many elements there are is output. If C<maxarr> is not sent then there is no
1421 maximum.
1422  
1423 B<option:> depth
1424  
1425 The C<depth> option allows you to indicate a maximum depth to display in the
1426 tree. If C<depth> is not sent then there is no maximum depth.
1427  
1428 B<option:> skip
1429  
1430 A list of hash elements to skip. Only applies to the top element and only if
1431 it is a hash.
1432  
1433 B<option:> skipall
1434  
1435 Works the same as C<skip>, but applies to all hashes in the structure, not
1436 just the top-level hash.
1437  
1438 =cut
1439  
1440 0 ▶sub showref {
Subroutine showref: not called CC=33 SCAR=70.2
Pod documented
1441 0 ▶ showstuff() or return '';
Branch: 1 branch
BranchTrueFalseTotal
showstuff() or return "" 0 0 0
1442
1443 0 my ($ref, %opts) = @_;
1444 0 my ($indentnum, $indent, $type, $tab, %skip, $finalfh);
1445 0 ▶ my $fh = $opts{'fh'} || getfh(wantarray);
Condition0%0/3
$opts{'fh'} || getfh(wantarray)
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'fh'} || getfh(wantarray)
$opts{'fh'} getfh(wantarray)
Truth table0%0/3
A || B
A= $opts{'fh'}B= getfh(wantarray)
ABresult
1X1
011
000
1446
1447
1448 # hash of keys to skip
1449 0 @skip{asarr(delete $opts{'skip'})} = ();
1450 0 @skip{asarr($opts{'skipall'})} = ();
1451
1452 # set some variables
1453 0 $indentnum = $opts{'indent'};
1454 0 ▶ $indentnum ||= 0;
Condition0%0/2
$indentnum ||= 0
l!l
00
MC/DC0%0/1
$indentnum ||= 0
$indentnum
Truth table0%0/2
A
A= $indentnum
Aresult
11
00
1455 0 $tab = ' ';
1456 0 $indent = $tab x $indentnum;
1457 0 ▶ $opts{'done'} ||= {};
Condition0%0/2
$opts{"done"} ||= {}
l!l
00
MC/DC0%0/1
$opts{"done"} ||= {}
$opts{"done"}
Truth table0%0/2
A
A= $opts{"done"}
Aresult
11
00
1458
1459 0 $type = "$ref";
1460 0 $type =~ s|^[^\=]*\=||;
1461 0 $type =~ s|\(.*||;
1462
1463 0 ▶ if (inweb())
Branch: 1 branch
BranchTrueFalseTotal
if (inweb()) 0 0 0
1464 0 {print $fh qq|<pre style="text-align:left;">\n|}
1465
1466 0 ▶ if (! $indent)
Branch: 1 branch
BranchTrueFalseTotal
unless ($indent) 0 0 0
1467 0 {print $fh "/----------------------------------------------------------------------------\\\n"}
1468
1469 0 ▶ if ($type eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'HASH') { } 0 0 0
elsif ($type eq 'ARRAY') { } 0 0 0
1470 # if we've recursed to the maximum level
1471 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'indent'} and $opts{'indent'} > 1 and $opts{'maxhash'} and keys %{$ref;} > $opts{'maxhash'}) { } 0 0 0
Condition0%0/9
$opts{"indent"} and $opts{"indent"} > 1 and $opts{"maxhash"} and keys %{$ref;} > $opts{"maxhash"}
!ll&&!rl&&r
000
$opts{"indent"} and $opts{"indent"} > 1 and $opts{"maxhash"}
!ll&&!rl&&r
000
$opts{"indent"} and $opts{"indent"} > 1
!ll&&!rl&&r
000
MC/DC0%0/4
$opts{"indent"} and $opts{"indent"} > 1 and $opts{"maxhash"} and keys %{$ref;} > $opts{"maxhash"}
$opts{"indent"} $opts{"indent"} > 1 $opts{"maxhash"} keys %{$ref;} > $opts{"maxhash"}
Truth table0%0/5
A and B and C and D
A= $opts{"indent"}B= $opts{"indent"} > 1C= $opts{"maxhash"}D= keys %{$ref;} > $opts{"maxhash"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1472 $opts{'indent'} &&
1473 ($opts{'indent'}>1) &&
1474 0 $opts{'maxhash'} && (keys(%{$ref}) > $opts{'maxhash'}) ) {
1475 0 my $count = keys %{$ref};
1476
1477 0 ▶ print $fh
Branch: 1 branch
BranchTrueFalseTotal
$count > 1 ? : 0 0 0
1478 $indent, '[', $count, ' hash element',
1479 ($count>1 ? 's' : ''), "]\n";
1480 }
1481  
1482 # else we haven't recursed to the maximum level
1483 else {
1484 0 ▶ if ($opts{'labelself'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"labelself"}) 0 0 0
1485 0 print $fh $indent, "HASH\n";
1486 0 $indentnum++;
1487 0 $indent .= $tab;
1488 }
1489
1490 ELLOOP:
1491 0 ▶ while ( my($n, $v) = each(%{$ref}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$ref;}) 0 0 0
1492 0 ▶ exists($skip{$n}) and next ELLOOP;
Branch: 1 branch
BranchTrueFalseTotal
exists $skip{$n} and next ELLOOP 0 0 0
1493
1494 0 print $fh $indent, $n, ' = ';
1495
1496 0 ▶ if (ref $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v) { } 0 0 0
1497 0 ▶ if ( $opts{'depth'} ? ($opts{'depth'} >= $indentnum) : 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts{'depth'} ? $opts{'depth'} >= $indentnum : 1) { } 0 0 0
$opts{'depth'} ? : 0 0 0
1498 0 ▶ if ($opts{'done'}->{$v})
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'done'}{$v}) { } 0 0 0
1499 0 {print $fh "[redundant]\n"}
1500 else {
1501 0 $opts{'done'}->{$v} = 1;
1502 0 print $fh "\n";
1503 0 showref($v, %opts, done=>$opts{'done'}, indent=>$indentnum+1, fh=>$fh)
1504 }
1505 }
1506 else
1507 0 {print $fh $v}
1508 }
1509
1510 #elsif (defined $v)
1511 # {print $fh $v, "\n"}
1512 #else
1513 # {print $fh "[undef]\n"}
1514
1515 else {
1516 0 print $fh define_show($v);
1517 }
1518 }
1519 }
1520 }
1521
1522 elsif ($type eq 'ARRAY') {
1523 0 print $fh $indent, "ARRAY\n";
1524
1525 0 ▶ if ($opts{'maxarr'} && (@{$ref}) > $opts{'maxarr'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'maxarr'} and @{$ref;} > $opts{'maxarr'}) { } 0 0 0
Condition0%0/3
$opts{"maxarr"} and @{$ref;} > $opts{"maxarr"}
!ll&&!rl&&r
000
MC/DC0%0/2
$opts{"maxarr"} and @{$ref;} > $opts{"maxarr"}
$opts{"maxarr"} @{$ref;} > $opts{"maxarr"}
Truth table0%0/3
A and B
A= $opts{"maxarr"}B= @{$ref;} > $opts{"maxarr"}
ABresult
0X0
100
111
1526 print
1527 0 ▶ $indent, $tab, '[', scalar(@{$ref}), ' element',
Branch: 1 branch
BranchTrueFalseTotal
@{$ref;} > 1 ? : 0 0 0
1528 0 (@{$ref}>1 ? 's' : ''), "]\n";
1529 }
1530
1531 else {
1532 0 my ($firstdone);
1533
1534 0 foreach my $v ( @{$ref} ) {
1535 0 ▶ if (ref $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v) { } 0 0 0
1536 0 ▶ if ( $opts{'depth'} ? ($opts{'depth'} >= $indentnum) : 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts{'depth'} ? $opts{'depth'} >= $indentnum : 1) { } 0 0 0
$opts{'depth'} ? : 0 0 0
1537 0 ▶ if ($opts{'done'}->{$v})
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'done'}{$v}) { } 0 0 0
1538 0 {print $fh $indent, $tab, '[redundant]'}
1539 else {
1540 0 $opts{'done'}->{$v} = 1;
1541
1542 0 ▶ if ($firstdone)
Branch: 1 branch
BranchTrueFalseTotal
if ($firstdone) { } 0 0 0
1543 0 {print $fh "\n"}
1544 else
1545 0 {$firstdone = 1}
1546
1547 0 showref($v, %opts, done=>$opts{'done'}, indent=>$indentnum+1, labelself=>1, fh=>$fh)
1548 }
1549 }
1550 else
1551 0 {print $fh $indent, $tab, $v}
1552 }
1553
1554 #elsif (defined $v)
1555 # {print $fh $indent, $tab, $v, "\n"}
1556 #else
1557 # {print $fh $indent, $tab, "[undef]\n"}
1558
1559 else {
1560 0 print $fh $indent, $tab, define_show($v), "\n"
1561 }
1562  
1563 }
1564 }
1565 }
1566
1567 0 ▶ if (! $indent)
Branch: 1 branch
BranchTrueFalseTotal
unless ($indent) 0 0 0
1568 0 {print $fh "\\----------------------------------------------------------------------------/\n\n"}
1569
1570 0 ▶ if (inweb())
Branch: 1 branch
BranchTrueFalseTotal
if (inweb()) 0 0 0
1571 0 {print $fh "</pre>\n"}
1572
1573 0 ▶ ismem($fh) and return $fh->mem();
Branch: 1 branch
BranchTrueFalseTotal
ismem($fh) and return $fh->mem 0 0 0
1574 }
1575 #
1576 # showref
1577 #------------------------------------------------------------------------------
1578  
1579  
1580 #------------------------------------------------------------------------------
1581 # getfh
1582 #
1583 # Private sub. Returns a file handle that is either STDOUT or STDERR (depending
1584 # on the value of the global variable $Debug::ShowStuff::out), or a MemHandle
1585 # object that will be used to return a string to the caller of the function
1586 # that called getfh.
1587 #
1588 0 ▶sub getfh {
Subroutine getfh: not called CC=3 SCAR=24.8
Pod undocumented
1589 0 my ($wa) = @_;
1590 0 my ($fh);
1591
1592 # if explicit context
1593 0 ▶ if ($always_void)
Branch: 1 branch
BranchTrueFalseTotal
if ($Debug::ShowStuff::always_void) 0 0 0
1594 0 { undef $wa }
1595
1596 # if called in void context, outputs to STDOUT,
1597 # otherwise returns string
1598 0 ▶ if (defined $wa) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $wa) { } 0 0 0
1599 0 require MemHandle;
1600 0 $fh = MemHandle->new('');
1601 }
1602
1603 else {
1604 0 $fh = $out;
1605 }
1606
1607 # return file handle
1608 0 return $fh;
1609 }
1610 #
1611 # getfh
1612 #------------------------------------------------------------------------------
1613  
1614  
1615 #------------------------------------------------------------------------------
1616 # ismem
1617 #
1618 # This private function returns true if the given file handle
1619 # is a MemHandle filehandle.
1620 #
1621 0 ▶sub ismem {
Subroutine ismem: not called CC=1 SCAR=6.9
Pod undocumented
1622 0 return UNIVERSAL::isa($_[0], 'MemHandle');
1623 }
1624 #
1625 # ismem
1626 #------------------------------------------------------------------------------
1627  
1628  
1629 #------------------------------------------------------------------------------
1630 # spacepad
1631 #
1632 # This private function returns the given string padded with
1633 # the indicated number of spaces. The spaces are added to the right
1634 #
1635 0 ▶sub spacepad {
Subroutine spacepad: not called CC=1 SCAR=6.9
Pod undocumented
1636 0 my ($str, $width) = @_;
1637 0 return sprintf("%-${width}s", $str);
1638 }
1639 #
1640 # spacepad
1641 #------------------------------------------------------------------------------
1642  
1643  
1644 #------------------------------------------------------------------------------
1645 # longestkey
1646 #
1647 # This private function returns the length of the longest key
1648 # in the given hash. Only pass in hash references, not hashes.
1649 #
1650 0 ▶sub longestkey {
Subroutine longestkey: not called CC=3 SCAR=24.8
Pod undocumented
1651 0 my ($hash) = @_;
1652 0 my $max = 0;
1653
1654 0 foreach my $key (keys %$hash) {
1655 0 ▶ if (length($key) > $max)
Branch: 1 branch
BranchTrueFalseTotal
if (length $key > $max) 0 0 0
1656 0 { $max = length($key) }
1657 }
1658
1659 0 return $max;
1660 }
1661 #
1662 # longestkey
1663 #------------------------------------------------------------------------------
1664  
1665  
1666 #------------------------------------------------------------------------------
1667 # asarr
1668 #
1669 # Private function. Allows an optional argument to be passed as
1670 # either an array reference or as a single item. Always returns
1671 # an array reference in scalar context and an array in array context.
1672 #
1673 0 ▶sub asarr {
Subroutine asarr: not called CC=4 SCAR=30.0
Pod undocumented
1674 0 my $arg = shift;
1675 0 my @rv;
1676
1677 0 ▶ if (ref $arg)
Branch: 2 branches
BranchTrueFalseTotal
if (ref $arg) { } 0 0 0
elsif (defined $arg) { } 0 0 0
1678 0 {@rv = @$arg}
1679 elsif (defined $arg)
1680 0 {@rv = $arg}
1681
1682 0 ▶ wantarray and return @rv;
Branch: 1 branch
BranchTrueFalseTotal
wantarray and return @rv 0 0 0
1683 0 return \@rv;
1684 }
1685 #
1686 # asarr
1687 #------------------------------------------------------------------------------
1688  
1689  
1690 #------------------------------------------------------------------------------
1691 # printhr
1692 #
1693  
1694 =head2 printhr
1695  
1696 Prints a horizontal rule. Handy for dividing up multiple println's.
1697  
1698 In text mode, the horizontal rule is a set of 80 dashes. In
1699 L<web mode|/"Text and web modes">, the output is either a <hr> element or a <p>
1700 element, depending on the title option (see "title" below).
1701  
1702 So, for example, the following line outputs a simple horizontal rule:
1703  
1704 B<option:> title
1705  
1706 If the C<title>option is sent, the title is embedded in the horizontal rule.
1707 So, for example, the following code produces a horizontal rule with with the
1708 string "test" embedded in it:
1709  
1710 printhr title=>'test';
1711  
1712 If only one param is sent, it is assumed that param is the title. So the'
1713 following code produces exactly the same thing as the example above:
1714  
1715 printhr 'test';
1716  
1717 In web mode, a title changes the HTML element that is output. If no title is
1718 given then printhr outputs an <hr> element. If a title is given the output is
1719 C<p> element with the title as the content. The <p> element has a gray
1720 background and a black border.
1721  
1722 B<option:> dash
1723  
1724 If the C<dash>option is sent, the given character is used as the separator.
1725 This param only applies to text mode;
1726  
1727 =cut
1728  
1729 0 ▶sub printhr {
Subroutine printhr: not called CC=8 SCAR=42.8
Pod documented
1730 0 ▶ showstuff() or return '';
Branch: 1 branch
BranchTrueFalseTotal
showstuff() or return "" 0 0 0
1731 0 my (%opts, $title);
1732 0 my $fh = getfh(wantarray);
1733
1734 # single argument: title
1735 0 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
1736 0 $title = $_[0];
1737 }
1738 else {
1739 0 %opts = @_;
1740 0 ▶ $title = $opts{'title'} || $opts{'msg'};
Condition0%0/3
$opts{'title'} || $opts{'msg'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'title'} || $opts{'msg'}
$opts{'title'} $opts{'msg'}
Truth table0%0/3
A || B
A= $opts{'title'}B= $opts{'msg'}
ABresult
1X1
011
000
1741 }
1742
1743 # web mode
1744 0 ▶ if (inweb()) {
Branch: 1 branch
BranchTrueFalseTotal
if (inweb()) { } 0 0 0
1745 0 my $indent = web_indent();
1746
1747 0 ▶ if (defined $title) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $title) { } 0 0 0
1748 0 print $fh <<"(HTML)";
1749 <p
1750 style="
1751 $indent
1752 background-color: #cccccc;
1753 border: 1px solid black;
1754 color: black;
1755 padding: 4px;
1756 font-weight: bold;
1757 font-size: 8pt;
1758 -moz-border-radius: 5px;
1759 -webkit-border-radius: 5px;
1760 ">
1761 0 @{ [ htmlesc($title) ] }
1762 </p>
1763 (HTML)
1764 }
1765
1766 else {
1767 0 print $fh qq|<hr style="$indent">\n|;
1768 }
1769 }
1770
1771 # else text mode
1772 else {
1773 # set dash
1774 0 my $dash = default($opts{'dash'}, '-');
1775
1776 # add vertical space
1777 0 ▶ if ($opts{'vspace'})
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"vspace"}) 0 0 0
1778 0 { print "\n" }
1779
1780 0 add_indents($fh);
1781
1782 0 ▶ if (defined $title) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $title) { } 0 0 0
1783 0 print $fh
1784 ($dash x 3),
1785 " $title ",
1786 ($dash x (75 - length($title))),
1787 "\n";
1788 }
1789
1790 else {
1791 0 print $fh ($dash x 80), "\n";
1792 }
1793 }
1794 }
1795 #
1796 # printhr
1797 #------------------------------------------------------------------------------
1798  
1799  
1800 #------------------------------------------------------------------------------
1801 # text_window_size
1802 #
1803 0 ▶sub text_window_size {
Subroutine text_window_size: not called CC=1 SCAR=6.9
Pod undocumented
1804 0 my (@dimensions, %rv);
1805
1806 # load necessary module
1807 0 require Term::ReadKey;
1808
1809 # get sizes
1810 # ($wchar, $hchar, $wpixels, $hpixels) = GetTerminalSize();
1811 0 @dimensions = Term::ReadKey::GetTerminalSize();
1812
1813 # set hash
1814 0 $rv{'chars_wide'} = $dimensions[0];
1815 0 $rv{'chars_tall'} = $dimensions[1];
1816 0 $rv{'pixels_wide'} = $dimensions[2];
1817 0 $rv{'pixels_tall'} = $dimensions[3];
1818
1819 # return
1820 0 return \%rv;
1821 }
1822 #
1823 # text_window_size
1824 #------------------------------------------------------------------------------
1825  
1826  
1827  
1828 #------------------------------------------------------------------------------
1829 # add_indents
1830 #
1831 # Private function
1832 #
1833 0 ▶sub add_indents {
Subroutine add_indents: not called CC=2 SCAR=17.9
Pod undocumented
1834 0 my ($fh) = @_;
1835
1836 0 for (1..$indent_level) {
1837 0 print $fh $indent_tab;
1838 }
1839 }
1840 #
1841 # add_indents
1842 #------------------------------------------------------------------------------
1843  
1844  
1845 #------------------------------------------------------------------------------
1846 # printval
1847 #
1848 # I don't even remember what this function is for, so for now I'm commenting
1849 # it out.
1850 #
1851 # sub printval {
1852 # showstuff() or return '';
1853 #
1854 # my ($val) = @_;
1855 # my $fh = getfh(wantarray);
1856 #
1857 # # get value to print
1858 # $val = define_show($val);
1859 #
1860 # if (inweb())
1861 # { $val = htmlesc($val) }
1862 #
1863 # print $fh $val;
1864 #
1865 # ismem($fh) and return $fh->mem();
1866 # }
1867 #
1868 # printval
1869 #------------------------------------------------------------------------------
1870  
1871  
1872 #------------------------------------------------------------------------------
1873 # preln
1874 #
1875  
1876 =head2 preln
1877  
1878 Outputs the given values inside a <pre> element. If not in a web environment,
1879 works just like preln.
1880  
1881 =cut
1882  
1883 0 ▶sub preln {
Subroutine preln: not called CC=7 SCAR=40.3
Pod documented
1884 0 ▶ showstuff() or return '';
Branch: 1 branch
BranchTrueFalseTotal
showstuff() or return "" 0 0 0
1885
1886 0 ▶ if (! inweb())
Branch: 1 branch
BranchTrueFalseTotal
unless (inweb()) 0 0 0
1887 0 { return println(@_) }
1888
1889 0 my ($str);
1890 0 my $fh = getfh(wantarray);
1891
1892 # special case: no inputs
1893 0 ▶ if (! @_ )
Branch: 2 branches
BranchTrueFalseTotal
if (not @_) { } 0 0 0
elsif (@_ == 1 and not defined $_[0]) { } 0 0 0
1894 0 {$str = '<i>empty</i>'}
1895
1896 # special case: just one element and it's undefined
1897 ▶ elsif ( (@_ == 1) && (! defined $_[0]) )
Condition0%0/3
@_ == 1 and not defined $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 1 and not defined $_[0]
@_ == 1 not defined $_[0]
Truth table0%0/3
A and not(B)
A= @_ == 1B= not defined $_[0]
ABresult
0X0
100
111
1898 0 {$str = '<i>undefined</i>'}
1899
1900 # else lump them all together
1901 else
1902 0 {$str = htmlesc(join('', @_))}
1903
1904 0 print $fh
1905 '<pre style="background-color:white;color:black;text-align:left;border:gray solid 1px;margin:3px;padding:3px; font-size:16px;">',
1906 $str,
1907 "</pre>\n";
1908
1909 0 ▶ ismem($fh) and return $fh->mem();
Branch: 1 branch
BranchTrueFalseTotal
ismem($fh) and return $fh->mem 0 0 0
1910 }
1911 #
1912 # preln
1913 #------------------------------------------------------------------------------
1914  
1915  
1916  
1917 #------------------------------------------------------------------------------
1918 # dieln
1919 #
1920  
1921 =head2 dieln
1922  
1923 Works like the C<die> command, except it always adds an end-of-line to the
1924 input array so you never get those "at line blah-blah-blah" additions.
1925  
1926 =cut
1927  
1928 0 ▶sub dieln {
Subroutine dieln: not called CC=1 SCAR=6.9
Pod documented
1929 0 my ($str);
1930 0 $str = join('', map {define_show($_)} @_);
1931
1932 0 die $str . "\n";
1933 }
1934 #
1935 # dieln
1936 #------------------------------------------------------------------------------
1937  
1938  
1939 #------------------------------------------------------------------------------
1940 # devexit
1941 #
1942  
1943 =head2 devexit
1944  
1945 Works like C<dieln> except it prepends 'dev exit: ' to the end of the string.
1946 If no string is sent, just outputs "dev exit".
1947  
1948 =cut
1949  
1950 0 ▶sub devexit {
Subroutine devexit: not called CC=3 SCAR=24.8
Pod documented
1951 0 my (@str) = @_;
1952 0 my ($rv);
1953
1954 0 ▶ if (defined $str[0])
Branch: 1 branch
BranchTrueFalseTotal
if (defined $str[0]) { } 0 0 0
1955 0 { $rv = 'dev exit: ' . join('', @str) }
1956 else
1957 0 { $rv = 'dev exit' }
1958
1959 # exit in web, die in commandline
1960 0 ▶ if ( inweb(strict=>1) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (inweb('strict', 1)) { } 0 0 0
1961 0 println $rv;
1962 0 exit;
1963 }
1964 else {
1965 0 dieln $rv;
1966 }
1967 }
1968 #
1969 # devexit
1970 #------------------------------------------------------------------------------
1971  
1972  
1973 #------------------------------------------------------------------------------
1974 # diearr
1975 #
1976  
1977 =head2 diearr
1978  
1979 Displays an array, then dies using C<dieln>.
1980  
1981 =cut
1982  
1983 0 ▶sub diearr {
Subroutine diearr: not called CC=1 SCAR=6.9
Pod documented
1984 0 my $err = shift;
1985 0 showarr @_;
1986 0 dieln $err;
1987 }
1988 #
1989 # diearr
1990 #------------------------------------------------------------------------------
1991  
1992  
1993 #------------------------------------------------------------------------------
1994 # pressenter
1995 #
1996  
1997 =head2 pressenter
1998  
1999 For use at the command line. Outputs a prompt to "press enter to continue",
2000 then waits for you to do exactly that.
2001  
2002 =cut
2003  
2004 0 ▶sub pressenter {
Subroutine pressenter: not called CC=2 SCAR=17.9
Pod documented
2005 0 my ($msg) = @_;
2006 0 my ($fh);
2007 0 ▶ $msg ||= 'press enter to continue';
Condition0%0/2
$msg ||= "press enter to continue"
l!l
00
MC/DC0%0/1
$msg ||= "press enter to continue"
$msg
Truth table0%0/2
A
A= $msg
Aresult
11
00
2008 0 $msg =~ s|\s*$||s;
2009
2010 # load IO::Handle
2011 0 require IO::Handle;
2012
2013 # output message
2014 0 println $msg;
2015
2016 # flush output file handle
2017 0 $fh = getfh(wantarray);
2018 0 $fh->flush();
2019
2020 # wait
2021 0 <STDIN>;
2022 }
2023 #
2024 # pressenter
2025 #------------------------------------------------------------------------------
2026  
2027  
2028 #------------------------------------------------------------------------------
2029 # confirm
2030 #
2031  
2032 =head2 confirm
2033  
2034 Prompts the user for a y or n. Exits quietly if y is pressed.
2035  
2036 =cut
2037  
2038 0 ▶sub confirm {
Subroutine confirm: not called CC=4 SCAR=30.0
Pod documented
2039 0 my ($msg) = @_;
2040 0 ▶ $msg ||= 'continue?';
Condition0%0/2
$msg ||= "continue?"
l!l
00
MC/DC0%0/1
$msg ||= "continue?"
$msg
Truth table0%0/2
A
A= $msg
Aresult
11
00
2041 0 $msg =~ s|\s*$| |s;
2042
2043 RESPONSELOOP:
2044 0 while (1) {
2045 0 my ($response);
2046
2047 0 print $msg;
2048 0 $response = <STDIN>;
2049 0 $response =~ s|^\s*(.).*|$1|s;
2050 0 $response = lc($response);
2051
2052 0 ▶ if ($response eq 'y')
Branch: 2 branches
BranchTrueFalseTotal
if ($response eq 'y') { } 0 0 0
elsif ($response eq 'n') { } 0 0 0
2053 0 { return 1 }
2054 elsif ($response eq 'n')
2055 0 { exit }
2056 }
2057 }
2058 #
2059 # confirm
2060 #------------------------------------------------------------------------------
2061  
2062  
2063 #------------------------------------------------------------------------------
2064 # httpheader
2065 #
2066  
2067 =head2 httpheader
2068  
2069 Outputs a text/html HTTP header. Not useful if you're using mod_perl.
2070  
2071 =cut
2072  
2073 0 ▶sub httpheader {
Subroutine httpheader: not called CC=3 SCAR=24.8
Pod documented
2074 0 my (%opts) = @_;
2075
2076 0 ▶ if (wantarray)
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 0 0
2077 0 {return "Content-type:text/html\n\n"}
2078
2079 0 ▶ my $fh = $opts{'fh'} || getfh(wantarray);
Condition0%0/3
$opts{'fh'} || getfh(wantarray)
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'fh'} || getfh(wantarray)
$opts{'fh'} getfh(wantarray)
Truth table0%0/3
A || B
A= $opts{'fh'}B= getfh(wantarray)
ABresult
1X1
011
000
2080 0 print $fh "Content-type:text/html\n\n";
2081 }
2082  
2083 0 ▶sub httpheaders{return httpheader(@_)}
Subroutine httpheaders: not called CC=1 SCAR=6.9
Pod undocumented
2084 #
2085 # header
2086 #------------------------------------------------------------------------------
2087  
2088  
2089  
2090 #------------------------------------------------------------------------------
2091 # showstderr
2092 #
2093  
2094 =head2 showstderr
2095  
2096 This function allows you to see, in the web page produced by a CGI, everything
2097 the CGI output to STDERR.
2098  
2099 To use C<showstderr>, assign the return value of the function to a variable
2100 that is scoped to the entire CGI script:
2101  
2102 my $stderr = showstderr();
2103  
2104 You need not do anything more with that variable. The object reference by
2105 your variable holds on to everything output to both STDOUT and STDERR. When
2106 the variable goes out of scope, the object outputs the STDOUT content with the
2107 STDERR content at the top of the web page.
2108  
2109 =cut
2110  
2111 0 ▶sub showstderr {
Subroutine showstderr: not called CC=1 SCAR=6.9
Pod documented
2112 0 return Debug::ShowStuff::ShowStdErr->new(@_);
2113 }
2114 #
2115 # showstderr
2116 #------------------------------------------------------------------------------
2117  
2118  
2119  
2120 #------------------------------------------------------------------------------
2121 # forceenv
2122 #
2123  
2124 =head2 forcetext, forceweb, forcenone
2125  
2126 By default, Debug::Showstuff guesses if it should be in
2127 L<text or web mode|/"Text and web modes">. These functions are for when you
2128 want to explicitly tell Debug::ShowStuff what mode it should be in.
2129 C<forcetext> forces text mode. C<forceweb> forces web mode. C<forcenone>
2130 tells Debug::Showstuff that you don't want to force either mode.
2131  
2132 =cut
2133  
2134 0 ▶sub forcetext {
Subroutine forcetext: not called CC=1 SCAR=6.9
Pod documented
2135 0 forceenv(0);
2136 }
2137  
2138 0 ▶sub forceweb {
Subroutine forceweb: not called CC=1 SCAR=6.9
Pod documented
2139 0 forceenv(1);
2140 }
2141  
2142 0 ▶sub forcenone {
Subroutine forcenone: not called CC=1 SCAR=6.9
Pod documented
2143 0 forceenv(undef);
2144 }
2145  
2146 0 ▶sub forceenv {
Subroutine forceenv: not called CC=1 SCAR=6.9
Pod undocumented
2147 0 $forceweb = $_[0];
2148 }
2149  
2150 #
2151 # forceenv
2152 #------------------------------------------------------------------------------
2153  
2154  
2155 #------------------------------------------------------------------------------
2156 # inweb
2157 #
2158  
2159 =head2 inweb
2160  
2161 Returns a guess on if we're in a L<web environment|/"Text and web modes">. The
2162 guess is pretty simple: if the environment variable C<REQUEST_URI> is true (in
2163 the Perlish sense) then this function returns true.
2164  
2165 If the global C<$Debug::ShowStuff::forceenv> is defined, this function returns
2166 the value of C<$Debug::ShowStuff::forceenv>.
2167  
2168 =cut
2169  
2170 0 ▶sub inweb {
Subroutine inweb: not called CC=4 SCAR=30.0
Pod documented
2171 0 my (%opts) = @_;
2172
2173 0 ▶ unless ($opts{'strict'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"strict"}) 0 0 0
2174 0 ▶ if (defined $forceweb)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Debug::ShowStuff::forceweb) 0 0 0
2175 0 { return $forceweb }
2176 }
2177
2178 0 ▶ return ($ENV{'REQUEST_URI'} || $ENV{'SERVER_NAME'});
Condition0%0/3
$ENV{'REQUEST_URI'} || $ENV{'SERVER_NAME'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{'REQUEST_URI'} || $ENV{'SERVER_NAME'}
$ENV{'REQUEST_URI'} $ENV{'SERVER_NAME'}
Truth table0%0/3
A || B
A= $ENV{'REQUEST_URI'}B= $ENV{'SERVER_NAME'}
ABresult
1X1
011
000
2179 }
2180 #
2181 # inweb
2182 #------------------------------------------------------------------------------
2183  
2184  
2185  
2186  
2187 #------------------------------------------------------------------------------
2188 # output_to_file
2189 #
2190  
2191 =head2 output_to_file($path)
2192  
2193 Sends Debug::ShowStuff output to a file instead of to STDOUT or STDERR. The
2194 value of this function must be assigned to a variable or it has no effect.
2195 Don't do anything with the returned value... it is NOT a file handle. The
2196 returned value is an object that, when it goes out of scope, closes the output
2197 file handle.
2198  
2199 For example, the following code will output to three files names
2200 Larry.txt, Curly.txt, and Moe.txt:
2201  
2202 foreach my $name (qw[Larry Curyl Moe]) {
2203 my $output = output_to_file("$name.txt");
2204 println $name;
2205 println 'length: ', length($name);
2206 }
2207  
2208 =cut
2209  
2210 0 ▶sub output_to_file {
Subroutine output_to_file: not called CC=4 SCAR=30.0
Pod documented
2211 0 my ($path, %opts) = @_;
2212 0 my ($fh, $arrows, $closer);
2213
2214 0 ▶ if (! defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 0 0 0
2215 0 my $msg =
2216 "Do not call outputfile in void context. Save the return value to a scalar.\n" .
2217 "REMINDER: The return value is NOT a filehandle, it is just an object that, when\n" .
2218 "it goes out of scope, closes the global filehandle.\n";
2219
2220 0 croak $msg;
2221 }
2222
2223 # if path is a reference, assume it's a filehandle object
2224 0 ▶ if (ref $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $path) { } 0 0 0
2225 0 $fh = $path;
2226 }
2227
2228 # else open filehandle to path
2229 else {
2230 0 require FileHandle;
2231
2232 # open new or open append
2233 0 ▶ if ($opts{'append'})
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'append'}) { } 0 0 0
2234 0 { $arrows = '>>' }
2235 else
2236 0 { $arrows = '>' }
2237
2238 0 $fh = FileHandle->new("$arrows$path");
2239 0 $fh->autoflush(1);
2240 }
2241
2242 0 setoutput($fh);
2243
2244 0 $closer = Debug::ShowStuff::CloseGlobalHandle->new($fh);
2245 0 return $closer;
2246 }
2247 #
2248 # output_to_file
2249 #------------------------------------------------------------------------------
2250  
2251  
2252  
2253 #------------------------------------------------------------------------------
2254 # setoutput
2255 #
2256  
2257 =head2 setoutput
2258  
2259 Sets the default output handle. By default, routines in C<Debug::ShowStuff>
2260 output to STDOUT. With this command you can set the default output to STDERR,
2261 back to STDOUT, to a filehandle you specify, or to a
2262 Debug::ShowStuff::SeparatePrint file handle.
2263  
2264 The following command sets default output to STDERR:
2265  
2266 setoutput 'stderr';
2267  
2268 This command sets output back to STDOUT:
2269  
2270 setoutput 'stdout';
2271  
2272 This command sends output to a file handle of your choice:
2273  
2274 setoutput $fh;
2275  
2276 This command sends output to a Debug::ShowStuff::SeparatePrint file handle.
2277 This option is a good way to create a simple log file. When you print to this
2278 type of file handle, the file is opened separately just for that write, an
2279 exclusive lock on the file is obtained, and the end of the file is sought.
2280  
2281 Note that the next parameter must be the path to the output file:
2282  
2283 setoutput 'separateprint', $file_path;
2284  
2285 B<option:> new
2286  
2287 If the C<new> parameter is true, then the output file is open in non-append
2288 mode, which means any existing contents are removed.
2289  
2290 =cut
2291  
2292 0 ▶sub setoutput {
Subroutine setoutput: not called CC=5 SCAR=34.0
Pod documented
2293 0 my $outname = shift;
2294
2295 # if reference, assume it's filehandle
2296 # if ( UNIVERSAL::isa($outname, 'FileHandle') ) {
2297 0 ▶ if ( ref $outname ) {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $outname) { } 0 0 0
elsif (lc $outname eq 'stdout') { } 0 0 0
elsif (lc $outname eq 'stderr') { } 0 0 0
elsif (lc $outname eq 'separateprint') { } 0 0 0
2298 0 $out = $outname;
2299 0 $forceweb = 0;
2300 }
2301
2302 # STDOUT
2303 elsif (lc($outname) eq 'stdout') {
2304 0 $out = *STDOUT;
2305 }
2306
2307 # STDERR
2308 elsif (lc($outname) eq 'stderr') {
2309 0 $out = *STDERR;
2310 0 $forceweb = 0;
2311 }
2312
2313 # Debug::ShowStuff::SeparatePrint
2314 elsif (lc($outname) eq 'separateprint') {
2315 0 $out = Debug::ShowStuff::SeparatePrint->new(@_);
2316 }
2317
2318 # else don't know
2319 else {
2320 0 croak "do not know this type of output: $outname";
2321 }
2322 }
2323 #
2324 # setoutput
2325 #------------------------------------------------------------------------------
2326  
2327  
2328  
2329 #------------------------------------------------------------------------------
2330 # fixundef
2331 #
2332  
2333 =head2 fixundef
2334  
2335 Takes a single argument. If that argument is undefined, returns an
2336 empty string. Otherwise returns the argument exactly as it is.
2337  
2338 =cut
2339  
2340 0 ▶sub fixundef {
Subroutine fixundef: not called CC=2 SCAR=17.9
Pod documented
2341 0 ▶ defined($_[0]) or return '';
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] or return "" 0 0 0
2342 0 return $_[0];
2343 }
2344 #
2345 # fixundef
2346 #------------------------------------------------------------------------------
2347  
2348  
2349 #------------------------------------------------------------------------------
2350 # findininc
2351 #
2352  
2353 =head2 findininc
2354  
2355 Given one or more file names, searches @INC for where they are located.
2356 Returns an array of full file names.
2357  
2358 =cut
2359  
2360 0 ▶sub findininc {
Subroutine findininc: not called CC=5 SCAR=34.0
Pod documented
2361 0 my (@filenames) = @_;
2362 0 my (@rv);
2363
2364 0 foreach my $dir (@INC) {
2365 0 foreach my $filename (@filenames) {
2366 0 my $path = "$dir/$filename";
2367
2368 0 ▶ if (-e $path)
Branch: 1 branch
BranchTrueFalseTotal
if (-e $path) 0 0 0
2369 0 {push @rv, $path}
2370 }
2371 }
2372
2373 0 ▶ if (! defined(wantarray))
Branch: 1 branch
BranchTrueFalseTotal
if (not defined wantarray) { } 0 0 0
2374 0 {showarr @rv}
2375 else
2376 0 {return @rv}
2377 }
2378 #
2379 # findininc
2380 #------------------------------------------------------------------------------
2381  
2382  
2383 #------------------------------------------------------------------------------
2384 # define_show
2385 #
2386 # Private function. If the given value is undef, returns the string "[undef]".
2387 # If it is an empty string, returns "[empty string]". Otherwise the original
2388 # string is returned.
2389 #
2390 1 ▶use overload;
Subroutine BEGIN: called CC=1 SCAR=0.0
2391  
2392 0 ▶sub define_show {
Subroutine define_show: not called CC=5 SCAR=34.0
Pod undocumented
2393 0 my ($val) = @_;
2394
2395 # if overloaded object, get return value and
2396 # concatenate with string (which defines it).
2397 0 ▶ if (ref $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val) 0 0 0
2398 0 ▶ if (my $func = overload::Method($val, '""'))
Branch: 1 branch
BranchTrueFalseTotal
if (my $func = overload::Method($val, "\"\"")) 0 0 0
2399 0 { $val = &$func($val) }
2400 }
2401
2402 0 ▶ if (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) { } 0 0 0
2403 0 ▶ if ($val eq '')
Branch: 1 branch
BranchTrueFalseTotal
if ($val eq "") 0 0 0
2404 0 { $val = '[empty string]' }
2405 #elsif ($val =~ m|^\s+$|s)
2406 # { $val = '[space-only string]' }
2407 }
2408
2409 else {
2410 0 $val = '[undef]';
2411 }
2412
2413 0 return $val;
2414 }
2415 #
2416 # define_show
2417 #------------------------------------------------------------------------------
2418  
2419  
2420 #------------------------------------------------------------------------------
2421 # showtainted
2422 #
2423  
2424 =head2 showtainted(@values)
2425  
2426 Given an array of values, shows which are tainted and which are not. If the
2427 first argument is a hashref, displays the tainted status of each element value.
2428  
2429 =cut
2430  
2431 0 ▶sub showtainted {
Subroutine showtainted: not called CC=6 SCAR=37.4
Pod documented
2432 0 my (@vals) = @_;
2433 # require Taint;
2434 0 require Scalar::Util;
2435
2436 # if first value is a hashref, show as hash
2437 0 ▶ if (UNIVERSAL::isa $vals[0], 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($vals[0], 'HASH')) { } 0 0 0
2438 0 my $hashref = $vals[0];
2439 0 my $show = {};
2440
2441 0 ▶ while ( my($key, $val) = each(%$hashref) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$hashref) 0 0 0
2442 0 my ($tainted);
2443
2444 0 ▶ if (Scalar::Util::tainted($val))
Branch: 1 branch
BranchTrueFalseTotal
if (&Scalar::Util::tainted($val)) { } 0 0 0
2445 0 { $tainted = 'tainted' }
2446 else
2447 0 { $tainted = 'not tainted' }
2448
2449 0 $show->{$key} = $tainted;
2450 }
2451
2452 0 showhash $show;
2453 }
2454
2455 # loop through values
2456 else {
2457 0 foreach my $val (@vals) {
2458 0 my ($tainted);
2459
2460 0 ▶ if (Scalar::Util::tainted($val))
Branch: 1 branch
BranchTrueFalseTotal
if (&Scalar::Util::tainted($val)) { } 0 0 0
2461 0 { $tainted = 'tainted: ' }
2462 else
2463 0 { $tainted = 'not tainted: ' }
2464
2465 0 $val = "$tainted$val";
2466 }
2467 # show values
2468 0 showarr @vals;
2469 }
2470 }
2471 #
2472 # showtainted
2473 #------------------------------------------------------------------------------
2474  
2475  
2476 #------------------------------------------------------------------------------
2477 # showsth
2478 #
2479  
2480 =head2 showsth
2481  
2482 Outputs a table of all rows in the given DBI statement handle. Note that
2483 this function "uses up" the statement handle.
2484  
2485 =cut
2486  
2487 0 ▶sub showsth {
Subroutine showsth: not called CC=3 SCAR=24.8
Pod documented
2488 0 ▶ showstuff() or return '';
Branch: 1 branch
BranchTrueFalseTotal
showstuff() or return "" 0 0 0
2489
2490 0 ▶ if (inweb())
Branch: 1 branch
BranchTrueFalseTotal
if (inweb()) { } 0 0 0
2491 0 { return showsth_web(@_) }
2492 else
2493 0 { return showsth_text(@_) }
2494 }
2495 #
2496 # showsth
2497 #------------------------------------------------------------------------------
2498  
2499  
2500 #------------------------------------------------------------------------------
2501 # showsql
2502 #
2503  
2504 =head2 showsql
2505  
2506 showsql output the results of an SQL statement. showsql takes three
2507 parameters: the database handle, the SQL statement, and an array-ref of
2508 parameters for the SQL statement.
2509  
2510 =cut
2511  
2512 0 ▶sub showsql {
Subroutine showsql: not called CC=1 SCAR=6.9
Pod documented
2513 0 my $adbh = shift;
2514 0 my $sql = shift;
2515 0 my $params = shift;
2516 0 return showsth($adbh, sql=>$sql, params=>$params, @_);
2517 }
2518 #
2519 # showsql
2520 #------------------------------------------------------------------------------
2521  
2522  
2523 #------------------------------------------------------------------------------
2524 # explainsql
2525 #
2526  
2527 =head2 explainsql
2528  
2529 explainsql outputs the result of an SQL EXPLAIN call. This function works much
2530 like showsql. The parameters are the database handle, the SQL statement, and
2531 an array-ref of SQL parameters. explainsql prepends "EXPLAIN ANALYZE" to your
2532 SQL, runs the statement, then outputs the results.
2533  
2534 I have only used explainsql with PostGresql. I would be interested hear about
2535 how it works with other database management systems and how it might be
2536 improved to work in those environments.
2537  
2538 =cut
2539  
2540 0 ▶sub explainsql {
Subroutine explainsql: not called CC=4 SCAR=30.0
Pod documented
2541 0 my ($adbh, $sql, $params_org) = @_;
2542 0 my (@params, $row);
2543
2544 # if any params, put them in @params array
2545 0 ▶ if (defined $params_org) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params_org) 0 0 0
2546 0 ▶ if (UNIVERSAL::isa $params_org, 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($params_org, 'ARRAY')) { } 0 0 0
2547 0 { @params = @{$params_org} }
2548 else
2549 0 { @params = $params_org }
2550 }
2551
2552 # modify $sql
2553 0 $sql = qq|EXPLAIN ANALYZE\n$sql|;
2554
2555 # get results row
2556 0 $row = $adbh->selectrow_hashref($sql, undef, @params);
2557
2558 # if we got a row
2559 0 ▶ if ($row) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row) { } 0 0 0
2560 0 preln $sql;
2561
2562 preln
2563 qq|QUERY PLAN\n----------------------------------------\n|,
2564 0 $row->{'QUERY PLAN'};
2565 }
2566
2567 # else error
2568 else {
2569 0 println 'error getting SQL explain: ', $DBI::errstr;
2570 }
2571 }
2572 #
2573 # explainsql
2574 #------------------------------------------------------------------------------
2575  
2576  
2577  
2578 #------------------------------------------------------------------------------
2579 # get_sth
2580 # Private method
2581 #
2582 0 ▶sub get_sth {
Subroutine get_sth: not called CC=7 SCAR=40.3
Pod undocumented
2583 0 my ($sth, %opts) = @_;
2584
2585 # if $sth is a db, create a statement handle
2586 0 ▶ if ( UNIVERSAL::isa($sth, 'DBI::db') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($sth, "DBI::db")) 0 0 0
2587 0 my ($dbh, $sql, @params);
2588 0 $sql = $opts{'sql'};
2589 0 $dbh = $sth;
2590
2591 # must have sql param
2592 0 ▶ if (! $sql)
Branch: 1 branch
BranchTrueFalseTotal
unless ($sql) 0 0 0
2593 0 { croak 'If $sth is a database handle, must have sql param' }
2594
2595 # if any params, put them in @params array
2596 0 ▶ if ($opts{'params'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"params"}) 0 0 0
2597 0 ▶ if (UNIVERSAL::isa $opts{'params'}, 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($opts{'params'}, 'ARRAY')) { } 0 0 0
2598 0 { @params = @{$opts{'params'}} }
2599 else
2600 0 { @params = $opts{'params'} }
2601 }
2602
2603 # create statement handle
2604 0 $sth = $dbh->prepare($sql);
2605 0 ▶ $DBI::err and croak $DBI::errstr;
Branch: 1 branch
BranchTrueFalseTotal
$DBI::err and croak($DBI::errstr) 0 0 0
2606
2607 # execute statement handle
2608 0 $sth->execute(@params);
2609 0 ▶ $DBI::err and croak $DBI::errstr;
Branch: 1 branch
BranchTrueFalseTotal
$DBI::err and croak($DBI::errstr) 0 0 0
2610 }
2611
2612 # return
2613 0 return $sth;
2614 }
2615 #
2616 # get_sth
2617 #------------------------------------------------------------------------------
2618  
2619  
2620 #------------------------------------------------------------------------------
2621 # showsth_text
2622 #
2623 0 ▶sub showsth_text {
Subroutine showsth_text: not called CC=6 SCAR=37.4
Pod undocumented
2624 0 my ($sth, %opts) = @_;
2625 0 my ($table, @fields, @rows, $out);
2626 0 require Text::TabularDisplay;
2627
2628 # get statement handle, in case a database handle was actually sent
2629 # KLUDGE: I got the idea that allowing a dbh and an sql statement
2630 # instead of a statement handle would be a good feature, in case I
2631 # didn't actually want to go to the trouble of creating a statement
2632 # handle just to display the results of an SQL statement. The general
2633 # idea is good, but should have been implemented as a separate method.
2634 # There is now a method called showsql, but it still relies on this
2635 # method to tell if a database or statement handle was sent. The kludge
2636 # here is that for now I'm choosing not to clean up the mess.
2637 0 $sth = get_sth($sth, %opts);
2638
2639 # ensure statement handle is executed
2640 0 ▶ if (! $sth->{'Active'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth->{"Active"}) 0 0 0
2641 0 $sth->execute();
2642 0 ▶ $DBI::err and die $DBI::errstr;
Branch: 1 branch
BranchTrueFalseTotal
$DBI::err and die $DBI::errstr 0 0 0
2643 }
2644
2645 # get array of fields
2646 0 @fields = @{$sth->{'NAME'}};
2647
2648 # create text-table object
2649 0 $table = Text::TabularDisplay->new(@fields);
2650
2651 # loop through rows
2652 0 ▶ while (my $row_hash = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row_hash = $sth->fetchrow_hashref) 0 0 0
2653 0 my (@row_arr);
2654
2655 0 foreach my $field (@fields)
2656 0 { push @row_arr, define($row_hash->{$field}) }
2657
2658 0 push @rows, \@row_arr;
2659 }
2660
2661 # populate text table
2662 0 $table->populate(\@rows);
2663
2664 # output text table
2665 0 $out = $table->render();
2666
2667 # build header
2668 0 ▶ if (hascontent $opts{'title'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (hascontent($opts{"title"})) 0 0 0
2669 0 my ($border, $title);
2670
2671 # build top border
2672 0 $border = $out;
2673 0 $border =~ s|\n.*||s;
2674 0 $border =~ s|\+|-|gs;
2675 0 $border =~ s|^\-|+|gs;
2676 0 $border =~ s|\-$|+|gs;
2677
2678 # build title
2679 0 $title = '| ' . crunch($opts{'title'});
2680 0 $title .= repeat(' ', length($border) - length($title) - 1);
2681 0 $title .= '|';
2682
2683 # add to output
2684 0 $out = $border . "\n" . $title . "\n" . $out;
2685 }
2686
2687 # output
2688 0 println $out;
2689
2690 # success
2691 0 return '';
2692 }
2693 #
2694 # showsth_text
2695 #------------------------------------------------------------------------------
2696  
2697  
2698 #------------------------------------------------------------------------------
2699 # showsth_web
2700 #
2701 0 ▶sub showsth_web {
Subroutine showsth_web: not called CC=17 SCAR=57.2
Pod undocumented
2702 0 my ($sth, %opts) = @_;
2703 0 my ($first_row_done, %field_names, $formats);
2704 0 tie %field_names, 'Tie::IxHash';
2705 0 ▶ $formats = $opts{'formats'} || {};
Condition0%0/2
$opts{'formats'} || {}
l!l
00
MC/DC0%0/1
$opts{'formats'} || {}
$opts{'formats'}
Truth table0%0/2
A
A= $opts{'formats'}
Aresult
11
00
2706
2707 # get statement handle, in case a database handle was actually sent
2708 # KLUDGE: I got the idea that allowing a dbh and an sql statement
2709 # instead of a statement handle would be a good feature, in case I
2710 # didn't actually want to go to the trouble of creating a statement
2711 # handle just to display the results of an SQL statement. The general
2712 # idea is good, but should have been implemented as a separate method.
2713 # There is now a method called showsql, but it still relies on this
2714 # method to tell if a database or statement handle was sent. The kludge
2715 # here is that for now I'm choosing not to clean up the mess.
2716 0 $sth = get_sth($sth, %opts);
2717
2718 # get headers that are to be output first
2719 0 ▶ if ($opts{'headers'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"headers"}) 0 0 0
2720 0 foreach my $field_name (@{$opts{'headers'}})
2721 0 { undef $field_names{$field_name} }
2722 }
2723
2724 # open table
2725 0 print <<'(HTML)';
2726 <table border="1" cellpadding="3">
2727 (HTML)
2728
2729 # output title if available
2730 0 ▶ if (defined $opts{'title'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"title"}) 0 0 0
2731 print
2732 '<tr style="background-color:navy; color:white; text-align:left;">',
2733 '<th colspan="',
2734 0 scalar(@{$sth->{'NAME'}}),
2735 '">',
2736 0 $opts{'title'},
2737 "</th></tr>\n";
2738 }
2739
2740 # loop through records
2741 0 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
2742 0 ▶ if (! $first_row_done) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($first_row_done) 0 0 0
2743 # build list of headers
2744 0 foreach my $field_name (@{$sth->{'NAME'}}) {
2745 0 ▶ if (! exists $field_names{$field_name})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $field_names{$field_name}) 0 0 0
2746 0 { undef $field_names{$field_name} }
2747 }
2748
2749 # open table and header row
2750 0 print <<'(HTML)';
2751 <thead>
2752 <tr valign="top" style="background-color:#99ccff; color:black; text-align:left;">
2753 (HTML)
2754
2755 # output headers
2756 0 foreach my $field_name (keys %field_names) {
2757 0 print '<th>', htmlesc($field_name), "</th>\n";
2758 }
2759
2760 # close header row
2761 0 print <<'(HTML)';
2762 </tr>
2763 </thead>
2764 <tbody>
2765 (HTML)
2766
2767 # note table headers have been output
2768 0 $first_row_done = 1;
2769 }
2770
2771 # open row
2772 0 print '<tr style="background-color:white; color:black; text-align:left; vertical-align:top;">';
2773
2774 # loop through fields
2775 0 foreach my $field_name (keys %field_names) {
2776 0 my ($value, $format);
2777 0 $value = $row->{$field_name};
2778 0 ▶ $format = $formats->{$field_name} || {};
Condition0%0/2
$formats->{$field_name} || {}
l!l
00
MC/DC0%0/1
$formats->{$field_name} || {}
$formats->{$field_name}
Truth table0%0/2
A
A= $formats->{$field_name}
Aresult
11
00
2779
2780 # <td>
2781 0 ▶ if ($format->{'align'})
Branch: 1 branch
BranchTrueFalseTotal
if ($format->{'align'}) { } 0 0 0
2782 0 { print qq|<td align="$format->{'align'}">| }
2783 else
2784 0 { print '<td>' }
2785
2786 0 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 0 0 0
2787 0 ▶ if ($value eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq '') { } 0 0 0
2788 0 print '<i>[empty string]</i>';
2789 }
2790
2791 else {
2792 0 my $output = htmlesc($value);
2793
2794 # pre
2795 0 ▶ if ($format->{'pre'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format->{"pre"}) 0 0 0
2796 0 $output =~ s|\t| |gs;
2797 0 $output = "<pre>$output</pre>";
2798 }
2799
2800 0 print $output;
2801 }
2802 }
2803
2804 else {
2805 0 print '<i>[undef]</i>';
2806 }
2807
2808 0 print "</td>\n";
2809 }
2810
2811 # close row
2812 0 print "</tr>\n";
2813 }
2814
2815 # close table or mention that there were no records
2816 0 ▶ if ($first_row_done) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first_row_done) { } 0 0 0
2817 0 print qq|</tbody>\n|;
2818 }
2819
2820 else {
2821 0 print qq|</thead><tbody><tr><td>no records</td></tr>\n</tbody>\n|;
2822 }
2823
2824 0 print qq|</table>\n|;
2825 }
2826 #
2827 # showsth_web
2828 #------------------------------------------------------------------------------
2829  
2830  
2831  
2832 #------------------------------------------------------------------------------
2833 # tempshowstuff
2834 #
2835  
2836 =head2 tempshowstuff
2837  
2838 Temporarily turn showstuff on or off. Create a variable in the lexical scope
2839 where you want the tempoary change, like this:
2840  
2841 my $temp = tempshowstuff(1)
2842  
2843 When the variable goes out of scope, showstuff will revert back to its previous
2844 state.
2845  
2846 =cut
2847  
2848 0 ▶sub tempshowstuff {
Subroutine tempshowstuff: not called CC=1 SCAR=6.9
Pod documented
2849 0 return Debug::ShowStuff::TempShowStuff->new();
2850 }
2851 #
2852 # tempshowstuff
2853 #------------------------------------------------------------------------------
2854  
2855  
2856 #------------------------------------------------------------------------------
2857 # showisa
2858 #
2859  
2860 =head2 showisa
2861  
2862 Outputs the ISA hierarchy of an object or class. For example, the following
2863 code outputs the ISA hierarchy for a Xapat object (Xapat is a web server
2864 written in Perl and which uses Net::Server).
2865  
2866 $xapat = Xapat->new(%opts);
2867 showisa $xapat;
2868  
2869 which outputs:
2870  
2871 ------------------------------------
2872 Xapat
2873 Net::Server::HTTP
2874 Net::Server::MultiType
2875 Net::Server
2876 ------------------------------------
2877  
2878 Note that showisa loads Class::ISA, which is available on CPAN.
2879  
2880 =cut
2881  
2882 0 ▶sub showisa {
Subroutine showisa: not called CC=2 SCAR=17.9
Pod documented
2883 0 my ($class) = @_;
2884 0 my (@isas);
2885
2886 # load necessary module
2887 0 require Class::ISA;
2888
2889 # if $class is an object, get its class
2890 0 ▶ if (ref $class)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) 0 0 0
2891 0 { $class = ref $class }
2892
2893 # get class hierarchy
2894 0 @isas = Class::ISA::self_and_super_path($class);
2895
2896 # show class hierarchy
2897 0 showarr @isas;
2898 }
2899 #
2900 # showisa
2901 #------------------------------------------------------------------------------
2902  
2903  
2904  
2905 #------------------------------------------------------------------------------
2906 # timer
2907 #
2908  
2909 =head2 timer
2910  
2911 This function is for when you want to display how long it took for your code to
2912 run. Assign the return value of this function to a variable. When the
2913 variable goes out of scope then the difference between the start and end time
2914 is displayed in seconds. For example, the following code:
2915  
2916 do {
2917 my $timer = timer();
2918 sleep 3;
2919 };
2920  
2921 outputs this:
2922  
2923 start timer
2924 duration: 3 seconds
2925  
2926 B<option:> title
2927  
2928 If you send the C<title> option, then that title will be displayed with the
2929 beginning and ending output. For example, this code:
2930  
2931 do {
2932 my $timer = timer(title=>'my block');
2933 sleep 3;
2934 };
2935  
2936 produces this output:
2937  
2938 start timer - my block
2939 duration - my block: 3 seconds
2940  
2941 B<method:> $timer->elapsed
2942  
2943 Returns the difference between when the timer was started and the current
2944 time.
2945  
2946 B<method:> $timer->silence
2947  
2948 Turns off the timer so that it doesn't display anything when it dies.
2949  
2950  
2951 =cut
2952  
2953 0 ▶sub timer {
Subroutine timer: not called CC=1 SCAR=6.9
Pod documented
2954 0 my (%opts) = @_;
2955 0 return Debug::ShowStuff::Timer->new(%opts);
2956 }
2957 #
2958 # timer
2959 #------------------------------------------------------------------------------
2960  
2961  
2962 #------------------------------------------------------------------------------
2963 # backtrace
2964 #
2965 0 ▶sub backtrace {
Subroutine backtrace: not called CC=3 SCAR=24.8
Pod undocumented
2966 0 my (%opts);
2967
2968 # if just one param, assume that's the title
2969 0 ▶ if (@_ == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
2970 0 { %opts = (title=>@_) }
2971 else
2972 0 { %opts = @_ }
2973
2974
2975 0 ▶ if (inweb())
Branch: 1 branch
BranchTrueFalseTotal
if (inweb()) { } 0 0 0
2976 0 { return backtrace_web(%opts) }
2977 else
2978 0 { return backtrace_text(%opts) }
2979 }
2980 #
2981 # backtrace
2982 #------------------------------------------------------------------------------
2983  
2984  
2985 #------------------------------------------------------------------------------
2986 # backtrace_text
2987 # Private subroutine
2988 #
2989 0 ▶sub backtrace_text {
Subroutine backtrace_text: not called CC=3 SCAR=24.8
Pod undocumented
2990 0 my (%opts) = @_;
2991 0 my ($indent, $table, @frames, $render);
2992
2993 # load Devel::Backtrace module
2994 0 require Text::TabularDisplay;
2995
2996 # indent to make a nice clean backtrace
2997 0 $indent = indent();
2998
2999 # get frames
3000 0 @frames = backtrace_frames();
3001
3002 # create text-table object
3003 0 $table = Text::TabularDisplay->new(qw(line package sub));
3004
3005 # populate text table
3006 0 $table->populate(\@frames);
3007
3008 # get rendered table
3009 0 $render = $table->render();
3010
3011 # if there is any message
3012 0 ▶ if (hascontent $opts{'title'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (hascontent($opts{"title"})) 0 0 0
3013 0 my ($first_line, $line_length, $title);
3014 # build first line of table
3015 0 $first_line = $render;
3016 0 $first_line =~ s|\n.*||s;
3017 0 $first_line =~ s|[^-]|-|gs;
3018 0 $first_line =~ s|^.|+|gs;
3019 0 $first_line =~ s|.$|+|gs;
3020 0 $line_length = length($first_line) - 1;
3021
3022 # build title line
3023 0 $title = '| ' . $opts{'title'};
3024 0 ▶ while (length($title) < $line_length) {$title .= ' '}
Branch: 1 branch
BranchTrueFalseTotal
if (length $title < $line_length) 0 0 0
3025 0 $title .= '|';
3026
3027 # build $render
3028 0 $render = $first_line . "\n" . $title. "\n" . $render;
3029 }
3030
3031 # output
3032 0 println $render;
3033 }
3034 #
3035 # backtrace_text
3036 #------------------------------------------------------------------------------
3037  
3038  
3039 #------------------------------------------------------------------------------
3040 # backtrace_web
3041 # Private subroutine
3042 #
3043 0 ▶sub backtrace_web {
Subroutine backtrace_web: not called CC=5 SCAR=34.0
Pod undocumented
3044 0 my (%opts) = @_;
3045 0 my ($last_sub, @frames, $render, $fh, $html, $title);
3046 0 ▶ showstuff() or return '';
Branch: 1 branch
BranchTrueFalseTotal
showstuff() or return "" 0 0 0
3047
3048 # get output handle
3049 0 $fh = getfh(wantarray());
3050
3051 # get frames
3052 0 @frames = backtrace_frames();
3053
3054 # title row
3055 0 ▶ if (defined $opts{'title'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{'title'}) { } 0 0 0
3056 0 $title = htmlesc($opts{'title'});
3057
3058 0 print STDERR 'BACKTRACE: ', $title, "\n";
3059 }
3060 else {
3061 0 $title = 'stack trace';
3062 }
3063
3064 # start table
3065 0 $html = <<"(HTML)";
3066 <table style="background-color:white;color:black" border="1">
3067 <tr>
3068 <th colspan="3" style="background-color:navy;color:white">$title</th>
3069 </tr>
3070 <tr colspan="3" style="background-color:#dddddd;">
3071 <th>line</th>
3072 <th>module</th>
3073 <th>sub</th>
3074 </tr>
3075 (HTML)
3076
3077 # loop through frames
3078 0 foreach my $frame (@frames) {
3079 0 my $mod = htmlesc($frame->[1]);
3080
3081 # break up module name
3082 0 $mod =~ s|::|::&thinsp;|gs;
3083
3084 0 $html .=
3085 qq|<tr valign="top">\n<td align="right">| .
3086 htmlesc($frame->[0]) .
3087 "</td>\n<td>" .
3088 $mod .
3089 "</td>\n<td>" .
3090 htmlesc($frame->[2]) .
3091 "</td>\n</tr>\n\n";
3092 }
3093
3094 # close table
3095 0 $html .= "\n</table>";
3096
3097 # note dying if doing so
3098 0 ▶ if ($opts{'die'})
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"die"}) 0 0 0
3099 0 { println 'die' }
3100
3101 # output
3102 # print $fh '<xmp>', $html, '</xmp>';
3103 0 print $fh $html;
3104 }
3105 #
3106 # backtrace_web
3107 #------------------------------------------------------------------------------
3108  
3109  
3110 #------------------------------------------------------------------------------
3111 # backtrace_frames
3112 #
3113 0 ▶sub backtrace_frames {
Subroutine backtrace_frames: not called CC=4 SCAR=30.0
Pod undocumented
3114 0 my ($trace, @frames, $last_sub);
3115
3116 # create backtrace object
3117 0 require Devel::StackTrace;
3118 0 $trace = Devel::StackTrace->new;
3119
3120 # from bottom (least recent) of stack to top.
3121 0 ▶ while (my $frame = $trace->prev_frame) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $frame = $trace->prev_frame) 0 0 0
3122 0 my ($sub);
3123
3124 # if the subroutine from the previous iteration exists,
3125 # output it, otherwise empty string
3126 0 ▶ if (defined $last_sub)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $last_sub) { } 0 0 0
3127 0 { $sub = $last_sub }
3128 else
3129 0 { $sub = '-' }
3130
3131 # now hold on to last sub
3132 0 $last_sub = $frame->subroutine;
3133 0 $last_sub =~ s|^.*\:||s;
3134
3135 # add frame data
3136 0 push @frames, [
3137 $frame->line,
3138 $frame->package,
3139 $sub];
3140 }
3141
3142 # remove last two frames, which are just this subroutine and the caller
3143 0 pop @frames;
3144 0 pop @frames;
3145 # pop @frames;
3146
3147 # special case: if called from dietrace, don't include that in output
3148 0 ▶ if ($frames[-1]->[2] eq 'dietrace')
Branch: 1 branch
BranchTrueFalseTotal
if ($frames[-1][2] eq "dietrace") 0 0 0
3149 0 { pop @frames }
3150
3151 # return
3152 0 return @frames;
3153 }
3154 #
3155 # backtrace_frames
3156 #------------------------------------------------------------------------------
3157  
3158  
3159 #------------------------------------------------------------------------------
3160 # dietrace
3161 #
3162 0 ▶sub dietrace {
Subroutine dietrace: not called CC=1 SCAR=6.9
Pod undocumented
3163 # showstuff(1);
3164 0 backtrace(@_);
3165
3166 # die
3167 0 die '[die]';
3168 }
3169 #
3170 # dietrace
3171 #------------------------------------------------------------------------------
3172  
3173  
3174  
3175  
3176 ###############################################################################
3177 # ShowStdErr
3178 #
3179 package Debug::ShowStuff::ShowStdErr;
3180 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3181  
3182  
3183 #---------------------------------------------------------------------------
3184 # new
3185 #
3186 0 ▶sub new {
Subroutine new: not called CC=10 SCAR=47.0
3187 0 my ($class, %opts) = @_;
3188 0 my $self = bless({}, $class);
3189
3190 # default to HTML environment if environment appears to indicate so
3191 0 ▶ if ( (! defined $opts{'html'}) && $ENV{'REQUEST_URI'})
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $opts{"html"} and $ENV{"REQUEST_URI"}) 0 0 0
Condition0%0/3
not defined $opts{"html"} and $ENV{"REQUEST_URI"}
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $opts{"html"} and $ENV{"REQUEST_URI"}
not defined $opts{"html"} $ENV{"REQUEST_URI"}
Truth table0%0/3
not(A) and B
A= not defined $opts{"html"}B= $ENV{"REQUEST_URI"}
ABresult
0X0
100
111
3192 0 {$opts{'html'} = 1}
3193
3194 # default certain properties based on html
3195 0 ▶ if ($opts{'html'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"html"}) 0 0 0
3196 0 ▶ defined($opts{'flushtostderr'}) or $opts{'flushtostderr'} = 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{"flushtostderr"} or $opts{"flushtostderr"} = 1 0 0 0
3197 0 ▶ defined($opts{'stderrfirst'}) or $opts{'stderrfirst'} = 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{"stderrfirst"} or $opts{"stderrfirst"} = 1 0 0 0
3198 }
3199
3200 # default to output STDERR first
3201 0 ▶ $opts{'stderrfirst'} = exists($opts{'stderrfirst'}) ? $opts{'stderrfirst'} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'stderrfirst'} ? : 0 0 0
3202
3203 # other properties
3204 0 $self->{'html'} = $opts{'html'};
3205 0 $self->{'flushtostderr'} = $opts{'flushtostderr'};
3206 0 $self->{'stderrfirst'} = $opts{'stderrfirst'};
3207
3208 # capture STDERR
3209 0 $self->{'stderrdata'} = {'str' => []};
3210 0 ▶ open(SAVEERR, ">&STDERR") or warn "Cannot save STDERR: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open SAVEERR, ">&STDERR" or warn "Cannot save STDERR: $!\n" 0 0 0
3211 0 print SAVEERR '';
3212 0 $self->{'saveerr'} = *SAVEERR;
3213 0 $self->{'caperr'} = tie(*STDERR, $class . '::HandleOb', $self->{'stderrdata'}, %opts);
3214
3215 # capture STDOUT
3216 0 ▶ if ($opts{'stderrfirst'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"stderrfirst"}) 0 0 0
3217 0 $self->{'stdoutdata'} = {'str' => []};
3218
3219 0 ▶ open(SAVESTD, ">&STDOUT") or warn "Cannot save STDOUT: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open SAVESTD, ">&STDOUT" or warn "Cannot save STDOUT: $!\n" 0 0 0
3220 0 print SAVESTD '';
3221 0 $self->{'savestd'} = *SAVESTD;
3222 0 $self->{'capstd'} = tie(*STDOUT, $class . '::HandleOb', $self->{'stdoutdata'}, %opts);
3223 }
3224
3225 # warn() doesn't seem to print to STDERR through Perl.
3226 # Catch that manually.
3227 0 $self->{'oldwarn'} = $SIG{__WARN__};
3228 0 ▶ $SIG{__WARN__}= sub {print STDERR "@_"};
Subroutine __ANON__: not called CC=1 SCAR=6.9
3229
3230 0 return $self;
3231 }
3232 #
3233 # new
3234 #---------------------------------------------------------------------------
3235  
3236  
3237 #---------------------------------------------------------------------------
3238 # displaystderr
3239 #
3240 0 ▶sub displaystderr {
Subroutine displaystderr: not called CC=5 SCAR=34.0
3241 0 my ($self) = @_;
3242
3243 # early exit: if there isn't anything in STDERR, do nothing
3244 0 ▶ unless (@{$self->{'stderrdata'}->{'str'}})
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->{"stderrdata"}{"str"};}) 0 0 0
3245 0 {return}
3246
3247 # output to the real STDERR if necessary
3248 0 ▶ if ($self->{'flushtostderr'})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"flushtostderr"}) 0 0 0
3249 0 {print STDERR join("\n",@{$self->{'stderrdata'}->{'str'}})}
3250
3251 # output as HTML if set to do so
3252 0 ▶ if ($self->{'html'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'html'}) { } 0 0 0
3253 0 print
3254 '<DIV STYLE="border-style:solid;border-width:1;padding:5;background-color:CCCCCC;color:black;">',
3255 "<H2 STYLE=\"margin-top:0px;\">STDERR</H2>\n<PRE>\n";
3256
3257 0 foreach my $line (@{$self->{'stderrdata'}->{'str'}}) {
3258 # escape the HTML
3259 0 $line =~ s|&|&#38;|g;
3260 0 $line =~ s|"|&#34;|g;
3261 0 $line =~ s|'|&#39;|g;
3262 0 $line =~ s|<|&#60;|g;
3263 0 $line =~ s|>|&#62;|g;
3264
3265 # output the line
3266 0 print $line;
3267 }
3268
3269 0 print "</PRE>\n</DIV>\n\n";
3270 }
3271
3272 # else output as text
3273 else {
3274 print
3275 "========================================================================\n",
3276 "STDERR\n\n",
3277 0 @{$self->{'stderrdata'}->{'str'}}, "\n",
3278 "========================================================================\n";
3279 }
3280 }
3281 #
3282 # displaystderr
3283 #---------------------------------------------------------------------------
3284  
3285  
3286 #------------------------------------------------------------------------------
3287 # DESTROY
3288 #
3289 0 ▶DESTROY {
Subroutine DESTROY: not called CC=12 SCAR=50.5
3290 0 my ($self) = @_;
3291
3292 #------------------------------------------------
3293 # release handles
3294 #
3295 0 $SIG{__WARN__} = $self->{'oldwarn'};
3296
3297 0 undef $self->{'caperr'}; # as documented in "perltie"
3298 0 ▶ untie(*STDERR) or warn("Cannot untie STDERR: $!\n");
Branch: 1 branch
BranchTrueFalseTotal
untie *STDERR or warn "Cannot untie STDERR: $!\n" 0 0 0
3299
3300 # open(STDERR, ">&SAVEERR") or warn("Cannot restore STDERR: $!\n");
3301 0 *SAVEERR = $self->{'saveerr'};
3302 0 ▶ open(STDERR, ">&SAVEERR") or warn("Cannot restore STDERR: $!\n");
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&SAVEERR" or warn "Cannot restore STDERR: $!\n" 0 0 0
3303
3304 0 ▶ if ($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
3305 0 {die "$@"}
3306
3307 # if also capturing stdout
3308 0 ▶ if ($self->{'stdoutdata'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"stdoutdata"}) 0 0 0
3309 0 undef $self->{'capstd'}; # as documented in "perltie"
3310 0 ▶ untie(*STDOUT) or warn("Cannot untie STDOUT: $!\n");
Branch: 1 branch
BranchTrueFalseTotal
untie *STDOUT or warn "Cannot untie STDOUT: $!\n" 0 0 0
3311
3312 0 *SAVESTD = $self->{'savestd'};
3313 0 ▶ open(STDOUT, ">&SAVESTD") or warn("Cannot restore STDOUT: $!\n");
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&SAVESTD" or warn "Cannot restore STDOUT: $!\n" 0 0 0
3314
3315 0 ▶ if ($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
3316 0 {die "$@"}
3317 }
3318 #
3319 # release handles
3320 #------------------------------------------------
3321
3322
3323 #------------------------------------------------
3324 # display data
3325 #
3326 0 ▶ if ($self->{'stderrfirst'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'stderrfirst'}) { } 0 0 0
3327 # put STDOUT into string
3328 0 my $stdout = join('', @{$self->{'stdoutdata'}->{'str'}});
3329
3330 # if HTML
3331 0 ▶ if ($self->{'html'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"html"}) 0 0 0
3332 0 my ($borders);
3333
3334 # pull out headers
3335 # this part is a little kludgy
3336 # I couldn't get the split on the following line
3337 # to work quite right
3338 # ($borders, $stdout) = split(m/(\r\n\r\n)/s, $stdout, 2);
3339 0 $borders = $stdout;
3340 0 $borders =~ s/((?:\r\n\r\n)|(?:\n\n)|(?:\r\r)).*//s;
3341 0 $stdout = substr($stdout, length($borders) + length($1));
3342
3343 # output headers
3344 0 print $borders, "\n\n";
3345 }
3346
3347 # output stderr
3348 0 $self->displaystderr;
3349
3350 # if stdout has any content, and it doesn't end in
3351 # a newline, add a newline
3352 0 ▶ if ( length($stdout) && ($stdout !~ m|[\r\n]$|) )
Branch: 1 branch
BranchTrueFalseTotal
if (length $stdout and not $stdout =~ /[\r\n]$/) 0 0 0
Condition0%0/3
length $stdout and not $stdout =~ /[\r\n]$/
!ll&&!rl&&r
000
MC/DC0%0/2
length $stdout and not $stdout =~ /[\r\n]$/
length $stdout not $stdout =~ /[\r\n]$/
Truth table0%0/3
A and not(B)
A= length $stdoutB= not $stdout =~ /[\r\n]$/
ABresult
0X0
100
111
3353 0 {$stdout .= "\n"}
3354
3355 # output stdout
3356 0 print $stdout;
3357 }
3358  
3359 # else just output STDERR
3360 else {
3361 0 $self->displaystderr;
3362 }
3363 #
3364 # display data
3365 #------------------------------------------------
3366  
3367 }
3368 #
3369 # DESTROY
3370 #------------------------------------------------------------------------------
3371  
3372  
3373 #
3374 # ShowStdErr
3375 ###############################################################################
3376  
3377  
3378  
3379 ###############################################################################
3380 # HandleOb
3381 #
3382 package Debug::ShowStuff::ShowStdErr::HandleOb;
3383 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3384 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
3385  
3386  
3387 0 ▶sub TIEHANDLE {
Subroutine TIEHANDLE: not called CC=1 SCAR=6.9
3388 0 my($class, $data, %opts) = @_;
3389 0 my $self= bless( {} , $class);
3390
3391 0 $self->{'croakonerr'} = $opts{'croakonerr'};
3392 0 $self->{'data'} = $data;
3393
3394 0 return($self);
3395 }
3396  
3397 0 ▶sub WRITE {
Subroutine WRITE: not called CC=1 SCAR=6.9
3398 0 my($self, $buf, $len, $offset) = @_;
3399 0 push @{$self->{'data'}->{'str'}}, $buf;
3400 0 return 1;
3401 }
3402  
3403 0 ▶sub PRINT {
Subroutine PRINT: not called CC=2 SCAR=17.9
3404 0 my $self = shift;
3405
3406 # croak if necessary
3407 0 ▶ if ($self->{'croakonerr'})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"croakonerr"}) 0 0 0
3408 0 {croak @_}
3409  
3410 0 push @{$self->{'data'}->{'str'}}, @_;
3411  
3412 0 return 1;
3413 }
3414  
3415 0 ▶sub PRINTF {
Subroutine PRINTF: not called CC=1 SCAR=6.9
3416 0 my $self = shift;
3417 0 my $fmt = shift;
3418
3419 # $self->{'data'}->{'str'} .= sprintf($fmt, @_);
3420 0 push @{$self->{'data'}->{'str'}}, sprintf($fmt, @_);
3421  
3422
3423 0 return 1;
3424 }
3425  
3426 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=1 SCAR=0.0
3427 }
3428  
3429 0 ▶sub readwarning {
Subroutine readwarning: not called CC=1 SCAR=6.9
3430 0 carp "Cannot read from specified filehandle.";
3431 }
3432  
3433 #
3434 # HandleOb
3435 ###############################################################################
3436  
3437  
3438  
3439 ###############################################################################
3440 # CloseGlobalHandle
3441 #
3442 # Private function.
3443 # Objects in this class automatically undefs Debug::ShowStuff::out if it is
3444 # a FileHandle object;
3445 #
3446 # Objects in this class are used by the function output_to_file().
3447 #
3448 package Debug::ShowStuff::CloseGlobalHandle;
3449 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3450 # use Carp;
3451  
3452 #------------------------------------------------------------------------------
3453 # new
3454 #
3455 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
3456 0 my ($class, $fh) = @_;
3457 0 my $self = bless {}, $class;
3458
3459 0 $self->{'fh'} = $fh;
3460 0 return $self;
3461 }
3462 #
3463 # new
3464 #------------------------------------------------------------------------------
3465  
3466  
3467 #------------------------------------------------------------------------------
3468 # DESTROY
3469 #
3470 0 ▶DESTROY {
Subroutine DESTROY: not called CC=4 SCAR=30.0
3471 0 my ($self) = @_;
3472 0 my $global = $Debug::ShowStuff::out;
3473
3474 # if the global output filehandle still exists
3475 0 ▶ if ($global) {
Branch: 1 branch
BranchTrueFalseTotal
if ($global) 0 0 0
3476
3477 # if it's a filehandle object
3478 0 ▶ if ( UNIVERSAL::isa($global, 'FileHandle') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($global, "FileHandle")) 0 0 0
3479
3480 # if it's the same filehandle object as this object references
3481 0 ▶ if ($global == $self->{'fh'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($global == $self->{"fh"}) 0 0 0
3482 # print "closing $global\n";
3483
3484 0 undef $global;
3485 0 undef $Debug::ShowStuff::out;
3486 0 Debug::ShowStuff::setoutput('stdout');
3487 }
3488 }
3489 }
3490  
3491 }
3492 #
3493 # DESTROY
3494 #------------------------------------------------------------------------------
3495  
3496  
3497  
3498 #
3499 # CloseGlobalHandle
3500 ###############################################################################
3501  
3502  
3503  
3504 ###############################################################################
3505 # Indent
3506 #
3507 # An object in this class for the purpose of decrementing the current
3508 # indent level when it is destroyed. On destruction is decrements
3509 # $Debug::ShowStuff::indent_level.
3510 #
3511 package Debug::ShowStuff::Indent;
3512 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3513  
3514  
3515 #---------------------------------------------------------------------------
3516 # new
3517 #
3518 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
3519 0 my ($class, %opts) = @_;
3520 0 my ($increment);
3521
3522 0 ▶ if (defined $opts{'title'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{'title'}) { } 0 0 0
3523 0 $Debug::ShowStuff::indent_level++;
3524 0 Debug::ShowStuff::println($opts{'title'});
3525 0 $increment = 2;
3526 }
3527 else {
3528 0 $increment = 1;
3529 }
3530
3531 # increment indent level
3532 0 $Debug::ShowStuff::indent_level++;
3533
3534 0 my $indent = bless({increment=>$increment, %opts}, $class);
3535 0 return $indent;
3536 }
3537 #
3538 # new
3539 #---------------------------------------------------------------------------
3540  
3541  
3542 #------------------------------------------------------------------------------
3543 # DESTROY
3544 #
3545 0 ▶DESTROY {
Subroutine DESTROY: not called CC=3 SCAR=24.8
3546 0 my ($self) = @_;
3547
3548 # add bottom space if necessary
3549 0 ▶ if ($self->{'bottom_space'})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"bottom_space"}) 0 0 0
3550 0 { Debug::ShowStuff::println() }
3551
3552 0 ▶ if ($Debug::ShowStuff::indent_level > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Debug::ShowStuff::indent_level > 0) 0 0 0
3553 # $Debug::ShowStuff::indent_level--;
3554 0 $Debug::ShowStuff::indent_level -= $self->{'increment'};
3555 }
3556 }
3557 #
3558 # DESTROY
3559 #------------------------------------------------------------------------------
3560  
3561  
3562 #
3563 # CloseIndent
3564 ###############################################################################
3565  
3566  
3567  
3568 ###############################################################################
3569 # Timer
3570 #
3571 # An object in this class for the purpose of showing the amount of time
3572 # something took to run. When the object is initialized it notes the current
3573 # time. When it is destroyed it displays the number of seconds between when
3574 # it was created and when it was destroyed.
3575 #
3576 package Debug::ShowStuff::Timer;
3577 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3578  
3579  
3580 #---------------------------------------------------------------------------
3581 # new
3582 #
3583 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
3584 0 my ($class, %opts) = @_;
3585 0 my ($timer, $init_title);
3586
3587 # default options
3588 0 %opts = (init_title=>1, %opts);
3589
3590 # create timer object
3591 0 $timer = bless({%opts}, $class);
3592
3593 # hold on to start time
3594 0 $timer->{'start'} = time();
3595
3596 # silence on destruction
3597 0 $timer->{'silence'} = $opts{'silence'};
3598
3599 # note beginning of duration
3600 0 ▶ if ($opts{'init_title'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"init_title"}) 0 0 0
3601 0 ▶ if (defined $timer->{'title'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $timer->{'title'}) { } 0 0 0
3602 0 Debug::ShowStuff::println('start timer - ', $timer->{'title'});
3603 }
3604 else {
3605 0 Debug::ShowStuff::println('start timer');
3606 }
3607 }
3608
3609 # return
3610 0 return $timer;
3611 }
3612 #
3613 # new
3614 #---------------------------------------------------------------------------
3615  
3616  
3617 #---------------------------------------------------------------------------
3618 # elapsed
3619 #
3620 0 ▶sub elapsed {
Subroutine elapsed: not called CC=1 SCAR=6.9
3621 0 my ($timer) = @_;
3622 0 return time() - $timer->{'start'};
3623 }
3624 #
3625 # elapsed
3626 #---------------------------------------------------------------------------
3627  
3628  
3629 #---------------------------------------------------------------------------
3630 # message
3631 #
3632 0 ▶sub message {
Subroutine message: not called CC=3 SCAR=24.8
3633 0 my ($timer) = @_;
3634 0 my ($duration, $seconds, $rv);
3635
3636 # get duration
3637 0 $duration = time() - $timer->{'start'};
3638
3639 # second or seconds
3640 0 ▶ if ($duration == 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($duration == 1) { } 0 0 0
3641 0 { $seconds = 'second' }
3642 else
3643 0 { $seconds = 'seconds' }
3644
3645 # note beginning of duration
3646 0 ▶ if (defined $timer->{'title'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $timer->{'title'}) { } 0 0 0
3647 $rv =
3648 'duration - ' .
3649 0 $timer->{'title'} .
3650 ': ' .
3651 $duration .
3652 ' seconds';
3653 }
3654 else {
3655 0 $rv =
3656 'duration : ' .
3657 $duration .
3658 ' ' .
3659 $seconds;
3660 }
3661
3662 # return
3663 0 return $rv;
3664 }
3665 #
3666 # message
3667 #---------------------------------------------------------------------------
3668  
3669  
3670 #---------------------------------------------------------------------------
3671 # silence
3672 #
3673 0 ▶sub silence {
Subroutine silence: not called CC=1 SCAR=6.9
3674 0 my ($timer) = @_;
3675 0 $timer->{'silence'} = 1;
3676 }
3677 #
3678 # silence
3679 #---------------------------------------------------------------------------
3680  
3681  
3682 #------------------------------------------------------------------------------
3683 # DESTROY
3684 #
3685 0 ▶DESTROY {
Subroutine DESTROY: not called CC=2 SCAR=17.9
3686 0 my ($timer) = @_;
3687
3688 # only output info if the 'silence' property is false
3689 0 ▶ if (! $timer->{'silence'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $timer->{"silence"}) 0 0 0
3690 0 Debug::ShowStuff::println($timer->message());
3691 }
3692 }
3693 #
3694 # DESTROY
3695 #------------------------------------------------------------------------------
3696  
3697  
3698 #
3699 # Timer
3700 ###############################################################################
3701  
3702  
3703 ###############################################################################
3704 # TempShowStuff
3705 #
3706 package Debug::ShowStuff::TempShowStuff;
3707 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3708  
3709  
3710 #------------------------------------------------------------------------------
3711 # new
3712 #
3713 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
3714 0 my ($class, $set) = @_;
3715 0 my $self = bless {}, $class;
3716
3717 # get current state
3718 0 $self->{'org'} = $Debug::ShowStuff::active;
3719
3720 # turn showstuff off
3721 0 Debug::ShowStuff::showstuff($set);
3722
3723 # return
3724 0 return $self;
3725 }
3726 #
3727 # new
3728 #------------------------------------------------------------------------------
3729  
3730  
3731 #------------------------------------------------------------------------------
3732 # DESTROY
3733 #
3734 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=6.9
3735 0 my ($self) = @_;
3736
3737 # turn showstuff off
3738 0 Debug::ShowStuff::showstuff($self->{'org'});
3739 }
3740 #
3741 # DESTROY
3742 #------------------------------------------------------------------------------
3743  
3744  
3745 #
3746 # TempShowStuff
3747 ###############################################################################
3748  
3749  
3750 ###############################################################################
3751 # Debug::ShowStuff::SeparatePrint
3752 #
3753 package Debug::ShowStuff::SeparatePrint;
3754 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3755 1 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
3756 1 ▶use IO::Seekable;
Subroutine BEGIN: called CC=1 SCAR=0.0
3757 1 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
3758 1 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
3759  
3760  
3761 #--------------------------------------------------------------------------------------
3762 # new
3763 #
3764 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
3765 0 my($class, $path, %opts) = @_;
3766 0 my $fh = gensym;
3767
3768 # must have path
3769 0 ▶ if (! defined $path)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $path) 0 0 0
3770 0 { croak "Debug::ShowStuff::SeparatePrint->new must have file path" }
3771
3772 # tie handle
3773 0 ${*$fh} = tie *$fh, "${class}::Tie", $path;
3774 0 bless $fh, $class;
3775
3776 # if "new" parameter is sent and is true, wipe out any existing contents
3777 # in the output path
3778 0 ▶ if ($opts{'new'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"new"}) 0 0 0
3779 0 ▶ FileHandle->new("> $path") or die $!;
Branch: 1 branch
BranchTrueFalseTotal
"FileHandle"->new("> $path") or die $! 0 0 0
3780 }
3781
3782 # return
3783 0 return $fh;
3784 }
3785 #
3786 # new
3787 #--------------------------------------------------------------------------------------
3788  
3789  
3790 #
3791 # Debug::ShowStuff::SeparatePrint
3792 ###############################################################################
3793  
3794  
3795  
3796 ###############################################################################
3797 # Debug::ShowStuff::SeparatePrint::Tie
3798 #
3799 package Debug::ShowStuff::SeparatePrint::Tie;
3800 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3801 1 ▶use IO::Seekable;
Subroutine BEGIN: called CC=1 SCAR=0.0
3802 1 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
3803 1 ▶use Fcntl ':mode', ':flock';
Subroutine BEGIN: called CC=1 SCAR=0.0
3804  
3805  
3806 #------------------------------------------------------------------------------
3807 # TIEHANDLE
3808 #
3809 0 ▶sub TIEHANDLE {
Subroutine TIEHANDLE: not called CC=1 SCAR=6.9
3810 0 my($class, $path) = @_;
3811 0 my $handle = bless({}, $class);
3812
3813 # hold on to path
3814 0 $handle->{'path'} = $path;
3815
3816 # return
3817 0 return $handle;
3818 }
3819 #
3820 # TIEHANDLE
3821 #------------------------------------------------------------------------------
3822  
3823  
3824 #------------------------------------------------------------------------------
3825 # don't use these methods
3826 # This type of file handle is for printing only, so none of the following
3827 # methods make sense to use and should notify the programming as such.
3828 #
3829 0 ▶sub READLINE { die "FileHandle::Separate::Tie is for printing only" }
Subroutine READLINE: not called CC=1 SCAR=6.9
3830 0 ▶sub READ { die "FileHandle::Separate::Tie is for printing only" }
Subroutine READ: not called CC=1 SCAR=6.9
3831 0 ▶sub GETC { die "FileHandle::Separate::Tie is for printing only" }
Subroutine GETC: not called CC=1 SCAR=6.9
3832 0 ▶sub WRITE { die "FileHandle::Separate::Tie is for printing only" }
Subroutine WRITE: not called CC=1 SCAR=6.9
3833 0 ▶sub TELL { die "FileHandle::Separate::Tie is for printing only" }
Subroutine TELL: not called CC=1 SCAR=6.9
3834 0 ▶sub SEEK { die "FileHandle::Separate::Tie is for printing only" }
Subroutine SEEK: not called CC=1 SCAR=6.9
3835 #
3836 # don't use these methods
3837 #------------------------------------------------------------------------------
3838  
3839  
3840 #------------------------------------------------------------------------------
3841 # these methods have no effect, but aren't harmful to use
3842 #
3843 0 ▶sub FLOCK {}
Subroutine FLOCK: not called CC=1 SCAR=0.0
3844 0 ▶sub BINMODE {}
Subroutine BINMODE: not called CC=1 SCAR=0.0
3845 0 ▶sub CLOSE {}
Subroutine CLOSE: not called CC=1 SCAR=0.0
3846 #
3847 # these methods have no effect, but aren't harmful to use
3848 #------------------------------------------------------------------------------
3849  
3850  
3851 #------------------------------------------------------------------------------
3852 # PRINT
3853 #
3854 0 ▶sub PRINT {
Subroutine PRINT: not called CC=3 SCAR=24.8
3855 0 my ($handle, @data) = @_;
3856 0 my ($out);
3857
3858 # open file handle
3859 0 ▶ $out = FileHandle->new(">> $handle->{'path'}") or
Branch: 1 branch
BranchTrueFalseTotal
$out = "FileHandle"->new(qq[>> $handle->{"path"}]) or die "unable to open filehandle: $!" 0 0 0
3860 die "unable to open filehandle: $!";
3861
3862 # get exclusive lock
3863 0 ▶ flock($out, LOCK_EX) or
Branch: 1 branch
BranchTrueFalseTotal
flock $out, 2 or die "unable to lock file: $!" 0 0 0
3864 die "unable to lock file: $!";
3865
3866 # seek end of file
3867 0 $out->seek(0, SEEK_END);
3868
3869 # output
3870 0 print $out @data;
3871 }
3872 #
3873 # PRINT
3874 #------------------------------------------------------------------------------
3875  
3876  
3877 #------------------------------------------------
3878 # PRINTF
3879 #
3880 0 ▶sub PRINTF {
Subroutine PRINTF: not called CC=1 SCAR=6.9
3881 0 my $self = shift;
3882 0 return $self->PRINT(sprintf( shift, @_ ));
3883 }
3884 #
3885 # PRINTF
3886 #------------------------------------------------
3887  
3888  
3889 #
3890 # Debug::ShowStuff::SeparatePrint::Tie
3891 ###############################################################################
3892  
3893  
3894 # return true
3895 1 1;
3896 __END__
↑ Summary ShowStuff/ShowVar.pm »