blib/lib/Primeval.pm

filter: Statementstmt 89.8% 53 / 59 Branchbran 61.1% 11 / 18 Conditioncond 71.4% 5 / 7 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 85.7% 12 / 14 Podpod 0.0% 0 / 2 totaltotal 79.8% 83 / 104 CC 14 SCAR 17.8
CC
14
Cov
82%
prim
12.1
quote
3.2
__ANON__
2.0
CRAP 5.9
↑ Summary
1 package Primeval;
2 2 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶ use Scalar::Util qw(reftype blessed looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶ use Data::Dumper 'Dumper';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 our $DUMP = 0;
8 2 our $RETURN = 0;
9
10 2 ▶ sub import {
Subroutine import: called CC=1 SCAR=0.0
11 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 *{caller().'::prim'} = \&prim
13 }
14
15 12 ▶ sub quote {
Subroutine quote: called CC=3 SCAR=11.6
Pod undocumented
16 2 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 12 ▶ local $_ = @_ ? "$_[0]" : "$_";
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 6 6 12
18 0 ▶ looks_like_number $_ ? $_ : do {s/\n/\\n/g; "'$_'"}
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($_) ? : 12 0 12
19 }
20
21 my %string = (
22 0 ▶ CODE => sub {&quote},
Subroutine __ANON__: not called CC=1 SCAR=6.9
23 0 ▶ SCALAR => sub {quote ${$_[0]}},
Subroutine __ANON__: not called CC=1 SCAR=6.9
24 3 ▶ ARRAY => sub {'['.(join ', ' => map quote, @{$_[0]}).']'},
Subroutine __ANON__: called CC=1 SCAR=0.0
25 3 ▶ HASH => sub {'{'.(join ', ' => map {
Subroutine __ANON__: called CC=2 SCAR=7.1
26 3 ▶ (/^\w+$/i ? $_ : quote).' => '.quote $_[0]{$_}
Branch: 1 branch
BranchTrueFalseTotal
/^\w+$/i ? : 3 0 3
27 3 } keys %{$_[0]}).'}'},
28 2 );
29
30 3 ▶ sub prim (&@) {
Subroutine prim: called CC=11 SCAR=25.0
Pod undocumented
31 3 my $eval = shift;
32 3 local $Data::Dumper::Terse = 1;
33 3 local $@;
34 3 my @msg;
35 3 for my $name (map {split /\s+/} @_) {
36 9 ▶ $name =~ /^
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /^ [\$\@%&*] (?: [a-zA-Z_] | (?: '|:: ) (?= \w ) ) (?: \w | (?: '|:: ) (?= \w ) )* $/x or croak("not a variable name '${name}'") 9 0 9
37 [\$@%&*]
38 (?: [a-zA-Z_] | (?: '|:: ) (?= \w ) )
39 (?: \w | (?: '|:: ) (?= \w ) )*
40 $/x or croak "not a variable name '$name'";
41
42 9 local *_ = \('\\'.$name);
43
44 9 ▶ my $ref = $eval->()
Branch: 1 branch
BranchTrueFalseTotal
my $ref = &$eval() or croak("error accessing variable '${name}':\n$@") 9 0 9
45 or croak "error accessing variable '$name':\n$@";
46
47 9 ▶ $ref = $$ref if $name =~ /\$/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /\$/ 3 6 9
48
49 9 ▶ my $type = reftype($ref) || '';
Condition100%2/2
builtin::reftype($ref) || ''
l!l
63
MC/DC100%1/1
builtin::reftype($ref) || ''
builtin::reftype($ref)
Truth table100%2/2
A
A= builtin::reftype($ref)
Aresult
11
00
50 9 my $class = blessed $ref;
51
52 my $msg = "$name: ".($class ? "$class=" : '').
53 ($DUMP && $type =~ /ARRAY|HASH/
54 ? Dumper($ref)
55 9 ▶ : ($string{$type} or \&quote)->($ref));
Branch: 2 branches
BranchTrueFalseTotal
$class ? : 0 9 9
$Primeval::DUMP && $type =~ /ARRAY|HASH/ ? : 0 9 9
Condition60%3/5
$Primeval::DUMP and $type =~ /ARRAY|HASH/
!ll&&!rl&&r
900
$string{$type} || \&Primeval::quote
l!l
63
MC/DC0%0/2
$Primeval::DUMP and $type =~ /ARRAY|HASH/
$Primeval::DUMP $type =~ /ARRAY|HASH/
MC/DC100%1/1
$string{$type} || \&Primeval::quote
$string{$type}
Truth table33%1/3
A and B
A= $Primeval::DUMPB= $type =~ /ARRAY|HASH/
ABresult
0X0
100
111
Truth table100%2/2
A
A= $string{$type}
Aresult
11
00
56
57 9 $msg =~ s/((?:^..|).{1,78}(?:\s|$))/$1\n /g;
58 9 $msg =~ s/\s*$/\n/;
59 9 ▶ if ($RETURN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Primeval::RETURN) { } 9 0 9
60 9 push @msg, $msg
61 }
62 else {
63 0 print $msg
64 }
65 }
66 "@msg"
67 3 }
68
69 2 our $VERSION = '0.02';
70
71 =head1 NAME
72
73 Primeval - terse variable dumping
74
75 =head1 VERSION
76
77 version 0.02
78
79 =head1 SYNOPSIS
80
81 use Primeval;
82
83 my $foo = 5;
84 my @bar = 1..10;
85 our %baz = (a => 1, b => 2);
86
87 prim{eval} '$foo @bar %baz';
88
89 prints:
90
91 $foo: 5
92 @bar: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
93 %baz: {a => 1, b => 2}
94
95 =head1 EXPORT
96
97 prim # always called as prim{eval}
98
99 =head1 SUBROUTINES
100
101 =head2 C< prim{eval} LIST >
102
103 takes a list of variable names, prints out the names along with their values.
104 each element of the argument list is split on white-space.
105
106 while actually a subroutine named C< prim > the block C< {eval} > must always
107 be passed to C< prim > as the first argument. this code block is used to peek
108 into the calling scope to fetch the values for lexical variables. using this
109 code block to access the caller's scope allows this module to have no external
110 dependencies (normally PadWalker would be required to peek into a lexical scope)
111
112 the arguments are checked to make sure they look like perl variable names, so
113 you don't have to worry about anything accidentally making it into an eval that
114 you wouldn't want to.
115
116 C< prim{eval} > will normally only print the first level of an array or hash
117 using a simple internal serialization routine. for full recursive printing,
118 arrays and hashes can be passed to L<Data::Dumper> by setting
119 C< $Primeval::DUMP = 1 >
120
121 C< prim{eval} > will return a string instead of printing if
122 C< $Primeval::RETURN > is set to a true value.
123
124 if you use C< prim{eval} > in a subroutine with closed over variables, just make
125 sure that you use every variable passed to C< prim{eval} > somewhere else in the
126 subroutine. otherwise, perl's garbage collector will sweep up the variables too
127 early.
128
129 =head1 AUTHOR
130
131 Eric Strom, C<< <asg at cpan.org> >>
132
133 =head1 BUGS
134
135 C<prim{eval}> only works correctly with closures in perl 5.10+
136
137 please report any bugs or feature requests to C<bug-primeval at rt.cpan.org>,
138 or through the web interface at
139 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Primeval>. I will be notified,
140 and then you'll automatically be notified of progress on your bug as I make
141 changes.
142
143 =head1 LICENSE AND COPYRIGHT
144
145 copyright 2011 Eric Strom.
146
147 this program is free software; you can redistribute it and/or modify it under
148 the terms of either: the GNU General Public License as published by the Free
149 Software Foundation; or the Artistic License.
150
151 see http://dev.perl.org/licenses/ for more information.
152
153 =cut
154
155 2 __PACKAGE__ if 'first require';
↑ Summary