11 use base qw/B::Deparse/;
15 B::RecDeparse - Deparse recursively into subroutines.
23 our $VERSION = '0.03';
27 perl -MO=RecDeparse,deparse,[@B__Deparse_opts],level,-1 [ -e '...' | bleh.pl ]
32 my $brd = B::RecDeparse->new(deparse => [ @b__deparse_opts ], level => $level);
33 my $code = $brd->coderef2text(sub { ... });
37 This module extends L<B::Deparse> by making it recursively replace subroutine calls encountered when deparsing.
39 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.
43 =head2 C<< new < deparse => [ @B__Deparse_opts ], level => $level > >>
45 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.
50 # p31268 made pp_entersub call single_delim
53 || ($Config{perl_patchlevel} && $Config{perl_patchlevel} >= 31268)
57 croak 'Optional arguments must be passed as key/value pairs' if @_ % 2;
59 my $deparse = $args{deparse};
60 if (defined $deparse) {
62 $deparse = [ $deparse ];
63 } elsif (ref $deparse ne 'ARRAY') {
69 my $level = $args{level};
70 $level = -1 unless defined $level;
72 return $deparse, $level;
77 $class = ref($class) || $class || __PACKAGE__;
78 my ($deparse, $level) = _parse_args(@_);
79 my $self = bless $class->SUPER::new(@$deparse), $class;
80 $self->{brd_level} = $level;
85 return $_[0]->{brd_level} < 0 || $_[0]->{brd_cur} < $_[0]->{brd_level}
89 my $bd = B::Deparse->new();
91 my ($deparse, $level) = _parse_args(@args);
92 my $compiler = $bd->coderef2text(B::Deparse::compile(@$deparse));
94 ['"]? B::Deparse ['"]? \s* -> \s* (new) \s* \( ([^\)]*) \)
95 /B::RecDeparse->$1(deparse => [ $2 ], level => $level)/gx;
96 $compiler = eval 'sub ' . $compiler;
103 $self->{brd_cur} = 0;
104 $self->{brd_sub} = 0;
105 $self->SUPER::init(@_);
108 my $key = $; . __PACKAGE__ . $;;
110 if (FOOL_SINGLE_DELIM) {
111 my $oldsd = *B::Deparse::single_delim{CODE};
112 no warnings 'redefine';
113 *B::Deparse::single_delim = sub {
115 if ((caller 1)[0] eq __PACKAGE__ and $body =~ s/^$key//) {
126 local $self->{brd_sub} = 1;
127 $self->SUPER::pp_entersub(@_);
129 $body =~ s/^&\s*(\w)/$1/ if $self->_recurse;
136 local $self->{brd_sub} = 0;
137 $self->SUPER::pp_refgen(@_);
144 if ($self->{brd_sub} <= 0 || !$self->_recurse) {
145 $body = $self->SUPER::pp_gv(@_);
147 my $gv = $self->gv_or_padgv($_[0]);
149 local @{$self}{qw/brd_sub brd_cur/} = (0, $self->{brd_cur} + 1);
150 'sub ' . $self->indent($self->deparse_sub($gv->CV));
152 if (FOOL_SINGLE_DELIM) {
153 $body = $key . $body;
165 =head2 C<pp_entersub>
171 Functions and methods from L<B::Deparse> reimplemented by this module. Never call them directly.
173 Otherwise, L<B::RecDeparse> inherits all methods from L<B::Deparse>.
177 An object-oriented module shouldn't export any function, and so does this one.
181 L<Carp> (standard since perl 5), L<Config> (since perl 5.00307) and L<B::Deparse> (since perl 5.005).
185 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
187 You can contact me by mail or on C<irc.perl.org> (vincent).
191 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.
195 You can find documentation for this module with the perldoc command.
197 perldoc B::RecDeparse
199 Tests code coverage report is available at L<http://www.profvince.com/perl/cover/B-RecDeparse>.
201 =head1 COPYRIGHT & LICENSE
203 Copyright 2008 Vincent Pit, all rights reserved.
205 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
209 1; # End of B::RecDeparse