| 1 | package App::unbelievable::Util; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | App::unbelievable::Util - common utilities | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 VARIABLES | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =cut | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use 5.010001; # For say(), stacked file tests | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use feature ':5.10'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use IPC::System::Simple; # for autodie ':all' | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use autodie ':all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use Import::Into; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head2 $VERBOSE | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | When truthy, L</_diag> produces output. | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =cut | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 1 | ▶ | use vars::i '$VERBOSE' => 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 1 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 1 | $Data::Dumper::Indent = 1; | |||||||||||||||||||||||||||
| 31 | 1 | $Data::Dumper::Sortkeys = 1; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | 1 | require File::Spec; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 1 | ▶ | use parent 'Exporter'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | use vars::i | ||||||||||||||||||||||||||||
| 37 | 1 | ▶ | '@EXPORT' => [qw($VERBOSE _croak _diag _line_mark_string)]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =head2 import | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | Load strict, warnings, &c. into the caller. | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =cut | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 2 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 48 | 2 | my $target = caller; | |||||||||||||||||||||||||||
| 49 | 2 | __PACKAGE__->export_to_level(1, @_); | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 2 | $_->import::into($target) foreach qw(strict warnings Data::Dumper File::Spec); | |||||||||||||||||||||||||||
| 52 | 2 | feature->import::into($target, ':5.10'); | |||||||||||||||||||||||||||
| 53 | 2 | IPC::System::Simple->import::into($target); | |||||||||||||||||||||||||||
| 54 | 2 | autodie->import::into($target, ':all'); | |||||||||||||||||||||||||||
| 55 | } #import() | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =head2 _croak | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | Lazy L<Carp/croak>. | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =cut | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | ▶ | sub _croak { | ||||||||||||||||||||||||||
|
Subroutine
_croak: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 64 | 0 | require Carp; | |||||||||||||||||||||||||||
| 65 | 0 | goto &Carp::croak; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =head2 _diag | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | Lazy L<Test::More/diag>, conditioned on L</$VERBOSE>. | ||||||||||||||||||||||||||||
| 71 | If the first argument is a scalar reference, output is suppressed | ||||||||||||||||||||||||||||
| 72 | unless L</$VERBOSE> is at least the referenced value. | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =cut | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 1 | ▶ | sub _diag { | ||||||||||||||||||||||||||
|
Subroutine
_diag: called CC=4 SCAR=26.2
| |||||||||||||||||||||||||||||
| 77 | 1 | ▶ | return unless $VERBOSE; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | if(ref $_[0] eq 'SCALAR') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | ▶ | return unless $VERBOSE >= ${$_[0]}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 0 | shift; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | 0 | require Test::More; # for diag() | |||||||||||||||||||||||||||
| 83 | 0 | goto &Test::More::diag; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =head2 _line_mark_string | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | Add a C<#line> directive to a string. Usage: | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | my $str = _line_mark_string <<EOT ; | ||||||||||||||||||||||||||||
| 91 | $contents | ||||||||||||||||||||||||||||
| 92 | EOT | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | or | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | my $str = _line_mark_string __FILE__, __LINE__, <<EOT ; | ||||||||||||||||||||||||||||
| 97 | $contents | ||||||||||||||||||||||||||||
| 98 | EOT | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | In the first form, information from C<caller> will be used for the filename | ||||||||||||||||||||||||||||
| 101 | and line number. | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | The C<#line> directive will point to the line after the C<_line_mark_string> | ||||||||||||||||||||||||||||
| 104 | invocation, i.e., the first line of <C$contents>. Generally, C<$contents> will | ||||||||||||||||||||||||||||
| 105 | be source code, although this is not required. | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | C<$contents> must be defined, but can be empty. | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =cut | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | ▶ | sub _line_mark_string { | ||||||||||||||||||||||||||
|
Subroutine
_line_mark_string: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 112 | 0 | my ($contents, $filename, $line); | |||||||||||||||||||||||||||
| 113 | 0 | ▶ | if(@_ == 1) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 114 | 0 | $contents = $_[0]; | |||||||||||||||||||||||||||
| 115 | 0 | (undef, $filename, $line) = caller; | |||||||||||||||||||||||||||
| 116 | } elsif(@_ == 3) { | ||||||||||||||||||||||||||||
| 117 | 0 | ($filename, $line, $contents) = @_; | |||||||||||||||||||||||||||
| 118 | } else { | ||||||||||||||||||||||||||||
| 119 | 0 | _croak("Invalid invocation"); | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | _croak("Need text") unless defined $contents; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | ▶ | die "Couldn't get location information" unless $filename && $line; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$filename and $line
MC/DC0%0/2
$filename and $line
$filename
$line
Truth table0%0/3
A and B
A= $filenameB= $line
| |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | $filename =~ s/"/-/g; | |||||||||||||||||||||||||||
| 126 | 0 | ++$line; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | return <<EOT; | |||||||||||||||||||||||||||
| 129 | #line $line "$filename" | ||||||||||||||||||||||||||||
| 130 | $contents | ||||||||||||||||||||||||||||
| 131 | EOT | ||||||||||||||||||||||||||||
| 132 | } #_line_mark_string() | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 1 | 1; | |||||||||||||||||||||||||||