12 use base qw<B::Deparse>;
16 B::RecDeparse - Deparse recursively into subroutines.
24 our $VERSION = '0.04';
28 perl -MO=RecDeparse,deparse,[@B__Deparse_opts],level,-1 [ -e '...' | bleh.pl ]
33 my $brd = B::RecDeparse->new(deparse => [ @b__deparse_opts ], level => $level);
34 my $code = $brd->coderef2text(sub { ... });
38 This module extends L<B::Deparse> by making it recursively replace subroutine calls encountered when deparsing.
40 Please refer to L<B::Deparse> documentation for what to do and how to do it. Besides the constructor syntax, everything should work the same for the two modules.
44 =head2 C<< new < deparse => [ @B__Deparse_opts ], level => $level > >>
46 The L<B::RecDeparse> object constructor. 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. 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.
51 # p31268 made pp_entersub call single_delim
54 || ($^V lt v5.9.0 and $^V ge v5.8.9)
55 || ($Config{perl_patchlevel} && $Config{perl_patchlevel} >= 31268)
61 Carp::croak('Optional arguments must be passed as key/value pairs');
65 my $deparse = $args{deparse};
66 if (defined $deparse) {
68 $deparse = [ $deparse ];
69 } elsif (ref $deparse ne 'ARRAY') {
76 my $level = $args{level};
77 $level = -1 unless defined $level;
80 return $deparse, $level;
85 $class = ref($class) || $class || __PACKAGE__;
87 my ($deparse, $level) = _parse_args(@_);
89 my $self = bless $class->SUPER::new(@$deparse), $class;
91 $self->{brd_level} = $level;
97 return $_[0]->{brd_level} < 0 || $_[0]->{brd_cur} < $_[0]->{brd_level}
103 my $bd = B::Deparse->new();
104 my ($deparse, $level) = _parse_args(@args);
106 my $compiler = $bd->coderef2text(B::Deparse::compile(@$deparse));
108 ['"]? B::Deparse ['"]? \s* -> \s* (new) \s* \( ([^\)]*) \)
109 /B::RecDeparse->$1(deparse => [ $2 ], level => $level)/gx;
110 $compiler = eval 'sub ' . $compiler;
119 $self->{brd_cur} = 0;
120 $self->{brd_sub} = 0;
121 $self->{brd_seen} = { };
123 $self->SUPER::init(@_);
126 my $key = $; . __PACKAGE__ . $;;
128 if (FOOL_SINGLE_DELIM) {
129 my $oldsd = *B::Deparse::single_delim{CODE};
131 no warnings 'redefine';
132 *B::Deparse::single_delim = sub {
135 if ((caller 1)[0] eq __PACKAGE__ and $body =~ s/^$key//) {
148 unless ($cv->CvFLAGS & B::CVf_ANON()) {
149 $name = $cv->GV->SAFENAME;
152 local $self->{brd_seen}->{$name} = 1 if defined $name;
153 return $self->SUPER::deparse_sub(@_);
160 local $self->{brd_sub} = 1;
161 $self->SUPER::pp_entersub(@_);
164 $body =~ s/^&\s*(\w)/$1/ if $self->_recurse;
173 local $self->{brd_sub} = 0;
174 $self->SUPER::pp_refgen(@_);
181 my $gv = $self->gv_or_padgv($_[0]);
182 my $name = $gv->NAME;
183 my $seen = $self->{brd_seen};
186 if ($self->{brd_sub} <= 0 || !$self->_recurse || $seen->{$name}) {
187 $body = $self->SUPER::pp_gv(@_);
190 local @{$self}{qw<brd_sub brd_cur>} = (0, $self->{brd_cur} + 1);
191 local $seen->{$name} = 1;
192 'sub ' . $self->indent($self->deparse_sub($gv->CV));
195 if (FOOL_SINGLE_DELIM) {
196 $body = $key . $body;
209 =head2 C<deparse_sub>
211 =head2 C<pp_entersub>
217 Functions and methods from L<B::Deparse> reimplemented by this module. Never call them directly.
219 Otherwise, L<B::RecDeparse> inherits all methods from L<B::Deparse>.
223 An object-oriented module shouldn't export any function, and so does this one.
227 L<Carp> (standard since perl 5), L<Config> (since perl 5.00307) and L<B::Deparse> (since perl 5.005).
231 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
233 You can contact me by mail or on C<irc.perl.org> (vincent).
237 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>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
241 You can find documentation for this module with the perldoc command.
243 perldoc B::RecDeparse
245 Tests code coverage report is available at L<http://www.profvince.com/perl/cover/B-RecDeparse>.
247 =head1 COPYRIGHT & LICENSE
249 Copyright 2008,2009,2010,2011 Vincent Pit, all rights reserved.
251 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
255 1; # End of B::RecDeparse