File Coverage

blib/lib/Primeval.pm
Criterion Covered Total %
statement 43 46 93.4
branch 10 16 62.5
condition 5 7 71.4
subroutine 10 10 100.0
pod 0 2 0.0
total 68 81 83.9


line stmt bran cond sub pod time code
1             package Primeval;
2 2     2   41588 use warnings;
  2         6  
  2         70  
3 2     2   13 use strict;
  2         4  
  2         71  
4 2     2   12 use Carp;
  2         6  
  2         153  
5 2     2   9 use Scalar::Util qw(reftype blessed looks_like_number);
  2         4  
  2         189  
6 2     2   2065 use Data::Dumper 'Dumper';
  2         19439  
  2         214  
7             our $DUMP = 0;
8             our $RETURN = 0;
9            
10             sub import {
11 2     2   18 no strict 'refs';
  2         2  
  2         138  
12 2     2   17 *{caller().'::prim'} = \&prim
  2         2311  
13             }
14            
15             sub quote {
16 2     2   11 no warnings 'uninitialized';
  2         8  
  2         1664  
17 12 100   12 0 37 local $_ = @_ ? "$_[0]" : "$_";
18 12 50       74 looks_like_number $_ ? $_ : do {s/\n/\\n/g; "'$_'"}
  0         0  
  0         0  
19             }
20            
21             my %string = (
22             CODE => sub {"e},
23             SCALAR => sub {quote ${$_[0]}},
24             ARRAY => sub {'['.(join ', ' => map quote, @{$_[0]}).']'},
25             HASH => sub {'{'.(join ', ' => map {
26             (/^\w+$/i ? $_ : quote).' => '.quote $_[0]{$_}
27             } keys %{$_[0]}).'}'},
28             );
29            
30             sub prim (&@) {
31 3     3 0 4827 my $eval = shift;
32 3         8 local $Data::Dumper::Terse = 1;
33 3         5 local $@;
34 3         6 my @msg;
35 3         8 for my $name (map {split /\s+/} @_) {
  5         20  
36 9 50       55 $name =~ /^
37             [\$@%&*]
38             (?: [a-zA-Z_] | (?: '|:: ) (?= \w ) )
39             (?: \w | (?: '|:: ) (?= \w ) )*
40             $/x or croak "not a variable name '$name'";
41            
42 9         26 local *_ = \('\\'.$name);
43            
44 9 50       25 my $ref = $eval->()
45             or croak "error accessing variable '$name':\n$@";
46            
47 9 100       496 $ref = $$ref if $name =~ /\$/;
48            
49 9   100     50 my $type = reftype($ref) || '';
50 9         19 my $class = blessed $ref;
51            
52 9 50 33     76 my $msg = "$name: ".($class ? "$class=" : '').
    50 100        
53             ($DUMP && $type =~ /ARRAY|HASH/
54             ? Dumper($ref)
55             : ($string{$type} or \"e)->($ref));
56            
57 9         134 $msg =~ s/((?:^..|).{1,78}(?:\s|$))/$1\n /g;
58 9         115 $msg =~ s/\s*$/\n/;
59 9 50       18 if ($RETURN) {
60 9         38 push @msg, $msg
61             }
62             else {
63 0         0 print $msg
64             }
65             }
66             "@msg"
67 3         19 }
68            
69             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 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<< >>
132            
133             =head1 BUGS
134            
135             C only works correctly with closures in perl 5.10+
136            
137             please report any bugs or feature requests to C,
138             or through the web interface at
139             L. 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             __PACKAGE__ if 'first require';