blib/lib/B/RecDeparse.pm

filter: Statementstmt 80.1% 89 / 111 Branchbran 69.2% 18 / 26 Conditioncond 25.0% 11 / 44 MC/DCmcdc 19.0% 4 / 21 Subroutinesub 90.4% 19 / 21 Podpod 12.5% 1 / 8 totaltotal 61.4% 142 / 231 CC 29 SCAR 34.9
CC
29
Cov
65%
pp_gv
132.0
BEGIN
12.4
_parse_args
6.0
CRAP 32.7
↑ Summary
1 package B::RecDeparse;
2  
3 12 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 12 ▶use B ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 12 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 12 ▶use base qw<B::Deparse>;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 B::RecDeparse - Deparse recursively into subroutines.
17  
18 =head1 VERSION
19  
20 Version 0.10
21  
22 =cut
23  
24 12 our $VERSION = '0.10';
25  
26 =head1 SYNOPSIS
27  
28 # Deparse recursively a Perl one-liner :
29 $ perl -MO=RecDeparse,deparse,@B__Deparse_opts,level,-1 -e '...'
30  
31 # Or a complete Perl script :
32 $ perl -MO=RecDeparse,deparse,@B__Deparse_opts,level,-1 x.pl
33  
34 # Or a single code reference :
35 use B::RecDeparse;
36  
37 my $brd = B::RecDeparse->new(
38 deparse => \@B__Deparse_opts,
39 level => $level,
40 );
41 my $code = $brd->coderef2text(sub { ... });
42  
43 =head1 DESCRIPTION
44  
45 This module extends L<B::Deparse> by making it recursively replace subroutine calls encountered when deparsing.
46  
47 Please refer to L<B::Deparse> documentation for what to do and how to do it.
48 Besides the constructor syntax, everything should work the same for the two modules.
49  
50 =head1 METHODS
51  
52 =head2 C<new>
53  
54 my $brd = B::RecDeparse->new(
55 deparse => \@B__Deparse_opts,
56 level => $level,
57 );
58  
59 The L<B::RecDeparse> object constructor.
60 You can specify the underlying L<B::Deparse> constructor arguments by passing a string or an array reference as the value of the C<deparse> key.
61 The C<level> option expects an integer that specifies how many levels of recursions are allowed : C<-1> means infinite while C<0> means none and match L<B::Deparse> behaviour.
62  
63 =cut
64  
65 12 ▶use constant {
Subroutine BEGIN: called CC=5 SCAR=25.2
66 # p31268 made pp_entersub call single_delim
67 FOOL_SINGLE_DELIM =>
68 ("$]" >= 5.009_005)
69 || ("$]" < 5.009 and "$]" >= 5.008_009)
70 12 ▶ || ($Config{perl_patchlevel} && $Config{perl_patchlevel} >= 31268)
Condition16%2/12
"$]" >= 5.009005 || "$]" < 5.009 && "$]" >= 5.008009 || $B::RecDeparse::Config{'perl_patchlevel'} && $B::RecDeparse::Config{'perl_patchlevel'} >= 31268
l!l&&r!l&&!r
1200
"$]" >= "5.009005" or "$]" < "5.009" and "$]" >= "5.008009"
l!l&&r!l&&!r
1200
"$]" < "5.009" and "$]" >= "5.008009"
!ll&&!rl&&r
000
$B::RecDeparse::Config{"perl_patchlevel"} and $B::RecDeparse::Config{"perl_patchlevel"} >= 31268
!ll&&!rl&&r
000
MC/DC0%0/5
"$]" >= 5.009005 || "$]" < 5.009 && "$]" >= 5.008009 || $B::RecDeparse::Config{'perl_patchlevel'} && $B::RecDeparse::Config{'perl_patchlevel'} >= 31268
"$]" >= "5.009005" "$]" < "5.009" "$]" >= "5.008009" $B::RecDeparse::Config{"perl_patchlevel"} $B::RecDeparse::Config{"perl_patchlevel"} >= 31268
Truth table12%1/8
A or B and C || D and E
A= "$]" >= "5.009005"B= "$]" < "5.009"C= "$]" >= "5.008009"D= $B::RecDeparse::Config{"perl_patchlevel"}E= $B::RecDeparse::Config{"perl_patchlevel"} >= 31268
ABCDEresult
1XXXX1
011XX1
00X111
010111
00X0X0
00X100
0100X0
010100
71 12 };
72  
73 36 ▶sub _parse_args {
Subroutine _parse_args: called CC=6 SCAR=17.9
74 36 ▶ if (@_ % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2) 1 35 36
75 1 require Carp;
76 1 Carp::croak('Optional arguments must be passed as key/value pairs');
77 }
78 35 my %args = @_;
79  
80 35 my $deparse = $args{deparse};
81 35 ▶ if (defined $deparse) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $deparse) { } 24 11 35
82 24 ▶ if (!ref $deparse) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $deparse) { } 6 18 24
elsif (ref $deparse ne 'ARRAY') { } 1 17 18
83 6 $deparse = [ $deparse ];
84 } elsif (ref $deparse ne 'ARRAY') {
85 1 $deparse = [ ];
86 }
87 } else {
88 11 $deparse = [ ];
89 }
90  
91 35 my $level = $args{level};
92 35 ▶ $level = -1 unless defined $level;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $level 3 32 35
93 35 $level = int $level;
94  
95 35 return $deparse, $level;
96 }
97  
98 35 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
99 35 my $class = shift;
100 35 ▶ $class = ref($class) || $class || __PACKAGE__;
Condition100%5/5
ref $class || $class || __PACKAGE__
l!l
341
ref $class or $class
l!l&&r!l&&!r
1331
MC/DC100%2/2
ref $class || $class || __PACKAGE__
ref $class $class
Truth table100%3/3
A or B
A= ref $classB= $class
ABresult
1X1
011
000
101  
102 35 my ($deparse, $level) = _parse_args(@_);
103  
104 34 my $self = bless $class->SUPER::new(@$deparse), $class;
105  
106 34 $self->{brd_level} = $level;
107  
108 34 return $self;
109 }
110  
111 77 ▶sub _recurse {
Subroutine _recurse: called CC=2 SCAR=6.9
112 return $_[0]->{brd_level} < 0 || $_[0]->{brd_cur} < $_[0]->{brd_level}
113 77 ▶}
Condition100%3/3
$_[0]{'brd_level'} < 0 || $_[0]{'brd_cur'} < $_[0]{'brd_level'}
l!l&&r!l&&!r
402314
MC/DC100%2/2
$_[0]{'brd_level'} < 0 || $_[0]{'brd_cur'} < $_[0]{'brd_level'}
$_[0]{'brd_level'} < 0 $_[0]{'brd_cur'} < $_[0]{'brd_level'}
Truth table100%3/3
A || B
A= $_[0]{'brd_level'} < 0B= $_[0]{'brd_cur'} < $_[0]{'brd_level'}
ABresult
1X1
011
000
114  
115 1 ▶sub compile {
Subroutine compile: called CC=2 SCAR=6.9
Pod undocumented
116 1 my @args = @_;
117  
118 1 my $bd = B::Deparse->new();
119 1 my ($deparse, $level) = _parse_args(@args);
120  
121 1 my $compiler = $bd->coderef2text(B::Deparse::compile(@$deparse));
122 1 $compiler =~ s/
123 ['"]? B::Deparse ['"]? \s* -> \s* (new) \s* \( ([^\)]*) \)
124 /B::RecDeparse->$1(deparse => [ $2 ], level => $level)/gx;
125 1 ▶ $compiler = eval 'sub ' . $compiler;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
126 1 ▶ die if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
127  
128 1 return $compiler;
129 }
130  
131 111 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
132 111 my $self = shift;
133  
134 111 $self->{brd_cur} = 0;
135 111 $self->{brd_sub} = 0;
136 111 $self->{brd_seen} = { };
137  
138 111 $self->SUPER::init(@_);
139 }
140  
141 12 my $key = $; . __PACKAGE__ . $;;
142  
143 12 if (FOOL_SINGLE_DELIM) {
144 12 my $oldsd = *B::Deparse::single_delim{CODE};
145  
146 12 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
147 53 ▶ *B::Deparse::single_delim = sub {
Subroutine __ANON__: called CC=3 SCAR=13.3
148 53 my $body = $_[2];
149  
150 53 ▶ if ((caller 1)[0] eq __PACKAGE__ and $body =~ s/^$key//) {
Branch: 1 branch
BranchTrueFalseTotal
if ((caller 1)[0] eq __PACKAGE__ and $body =~ s/^$key//) { } 0 53 53
Condition33%1/3
(caller 1)[0] eq __PACKAGE__ and $body =~ s/^$key//
!ll&&!rl&&r
5300
MC/DC0%0/2
(caller 1)[0] eq __PACKAGE__ and $body =~ s/^$key//
(caller 1)[0] eq __PACKAGE__ $body =~ s/^$key//
Truth table33%1/3
A and B
A= (caller 1)[0] eq __PACKAGE__B= $body =~ s/^$key//
ABresult
0X0
100
111
151 0 return $body;
152 } else {
153 53 $oldsd->(@_);
154 }
155 }
156 12 }
157  
158 77 ▶sub deparse_sub {
Subroutine deparse_sub: called CC=3 SCAR=11.0
Pod undocumented
159 77 my $self = shift;
160 77 my $cv = $_[0];
161  
162 77 my $name;
163 77 ▶ unless ($cv->CvFLAGS & B::CVf_ANON()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cv->CvFLAGS & 128) 44 33 77
164 44 $name = $cv->GV->SAFENAME;
165 }
166  
167 77 ▶ local $self->{brd_seen}->{$name} = 1 if defined $name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $name 44 33 77
168 77 return $self->SUPER::deparse_sub(@_);
169 }
170  
171 77 ▶sub pp_entersub {
Subroutine pp_entersub: called CC=2 SCAR=6.9
Pod undocumented
172 77 my $self = shift;
173  
174 77 my $body = do {
175 77 local $self->{brd_sub} = 1;
176 77 $self->SUPER::pp_entersub(@_);
177 };
178  
179 77 ▶ $body =~ s/^&\s*(\w)/$1/ if $self->_recurse;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_recurse 63 14 77
180  
181 77 return $body;
182 }
183  
184 0 ▶sub pp_refgen {
Subroutine pp_refgen: not called CC=1 SCAR=6.9
Pod undocumented
185 0 my $self = shift;
186  
187 0 return do {
188 0 local $self->{brd_sub} = 0;
189 0 $self->SUPER::pp_refgen(@_);
190 }
191 }
192  
193 12 ▶sub pp_srefgen {
Subroutine pp_srefgen: called CC=1 SCAR=0.0
Pod undocumented
194 12 my $self = shift;
195  
196 12 return do {
197 12 local $self->{brd_sub} = 0;
198 12 $self->SUPER::pp_srefgen(@_);
199 }
200 }
201  
202 0 ▶sub pp_gv {
Subroutine pp_gv: not called CC=11 SCAR=48.8
Pod undocumented
203 0 my $self = shift;
204  
205 0 my $gv = $self->gv_or_padgv($_[0]);
206 0 ▶ my $cv = $gv->FLAGS & B::SVf_ROK ? $gv->RV : undef;
Branch: 1 branch
BranchTrueFalseTotal
$gv->FLAGS & 2048 ? : 0 0 0
207 0 ▶ my $name = $cv ? $cv->NAME_HEK || $cv->GV->NAME : $gv->NAME;
Branch: 1 branch
BranchTrueFalseTotal
$cv ? : 0 0 0
Condition0%0/3
$cv->NAME_HEK or $cv->GV->NAME
l!l&&r!l&&!r
000
MC/DC0%0/2
$cv->NAME_HEK or $cv->GV->NAME
$cv->NAME_HEK $cv->GV->NAME
Truth table0%0/3
A or B
A= $cv->NAME_HEKB= $cv->GV->NAME
ABresult
1X1
011
000
208 0 ▶ $cv ||= $gv->CV;
Condition0%0/3
$cv ||= $gv->CV
l!l&&r!l&&!r
000
MC/DC0%0/2
$cv ||= $gv->CV
$cv $gv->CV
Truth table0%0/3
A ||= B
A= $cvB= $gv->CV
ABresult
1X1
011
000
209 0 my $seen = $self->{brd_seen};
210  
211 0 my $body;
212 0 ▶ if (!$self->{brd_sub} or !$self->_recurse or $seen->{$name} or !$$cv
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'brd_sub'} && $self->_recurse or $seen->{$name} or not $$cv or not $cv->isa('B::CV') or $cv->ROOT->isa('B::NULL')) { } 0 0 0
Condition0%0/15
not $self->{"brd_sub"} && $self->_recurse or $seen->{$name} or not $$cv or not $cv->isa("B::CV") or $cv->ROOT->isa("B::NULL")
l!l&&r!l&&!r
000
not $self->{"brd_sub"} && $self->_recurse or $seen->{$name} or not $$cv or not $cv->isa("B::CV")
l!l&&r!l&&!r
000
not $self->{"brd_sub"} && $self->_recurse or $seen->{$name} or not $$cv
l!l&&r!l&&!r
000
not $self->{"brd_sub"} && $self->_recurse or $seen->{$name}
l!l&&r!l&&!r
000
$self->{'brd_sub'} && $self->_recurse
!ll&&!rl&&r
000
MC/DC0%0/6
not $self->{"brd_sub"} && $self->_recurse or $seen->{$name} or not $$cv or not $cv->isa("B::CV") or $cv->ROOT->isa("B::NULL")
$self->{'brd_sub'} $self->_recurse $seen->{$name} not $$cv not $cv->isa("B::CV") $cv->ROOT->isa("B::NULL")
Truth table0%0/7
not(A && B) or C or not(D) or not(E) or F
A= $self->{'brd_sub'}B= $self->_recurseC= $seen->{$name}D= not $$cvE= not $cv->isa("B::CV")F= $cv->ROOT->isa("B::NULL")
ABCDEFresult
0XXXXX1
10XXXX1
111XXX1
1101XX1
11001X1
1100011
1100000
213 or !$cv->isa('B::CV') or $cv->ROOT->isa('B::NULL')) {
214 0 $body = $self->SUPER::pp_gv(@_);
215 } else {
216 0 $body = do {
217 0 local @{$self}{qw<brd_sub brd_cur>} = (0, $self->{brd_cur} + 1);
218 0 local $seen->{$name} = 1;
219 0 'sub ' . $self->indent($self->deparse_sub($cv));
220 };
221  
222 0 if (FOOL_SINGLE_DELIM) {
223 0 $body = $key . $body;
224 } else {
225 $body .= '->';
226 }
227 }
228  
229 0 return $body;
230 }
231  
232 =pod
233  
234 The following functions and methods from L<B::Deparse> are reimplemented by this module :
235  
236 =over 4
237  
238 =item *
239  
240 C<compile>
241  
242 =item *
243  
244 C<init>
245  
246 =item *
247  
248 C<deparse_sub>
249  
250 =item *
251  
252 C<pp_entersub>
253  
254 =item *
255  
256 C<pp_refgen>
257  
258 =item *
259  
260 C<pp_srefgen>
261  
262 =item *
263  
264 C<pp_gv>
265  
266 =back
267  
268 Otherwise, L<B::RecDeparse> inherits all methods from L<B::Deparse>.
269  
270 =head1 EXPORT
271  
272 An object-oriented module shouldn't export any function, and so does this one.
273  
274 =head1 DEPENDENCIES
275  
276 L<perl> 5.8.1.
277  
278 L<Carp> (standard since perl 5), L<Config> (since perl 5.00307) and L<B::Deparse> (since perl 5.005).
279  
280 =head1 AUTHOR
281  
282 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
283  
284 You can contact me by mail or on C<irc.perl.org> (vincent).
285  
286 =head1 BUGS
287  
288 Please report any bugs or feature requests to C<bug-b-recdeparse at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=B-RecDeparse>.
289 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
290  
291 =head1 SUPPORT
292  
293 You can find documentation for this module with the perldoc command.
294  
295 perldoc B::RecDeparse
296  
297 Tests code coverage report is available at L<http://www.profvince.com/perl/cover/B-RecDeparse>.
298  
299 =head1 COPYRIGHT & LICENSE
300  
301 Copyright 2008,2009,2010,2011,2013,2014,2015 Vincent Pit, all rights reserved.
302  
303 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
304  
305 =cut
306  
307 12 1; # End of B::RecDeparse
↑ Summary