]> git.vpit.fr Git - perl/modules/B-RecDeparse.git/blob - lib/B/RecDeparse.pm
Add some linefeeds
[perl/modules/B-RecDeparse.git] / lib / B / RecDeparse.pm
1 package B::RecDeparse;
2
3 use 5.008;
4
5 use strict;
6 use warnings;
7
8 use Config;
9
10 use base qw/B::Deparse/;
11
12 =head1 NAME
13
14 B::RecDeparse - Deparse recursively into subroutines.
15
16 =head1 VERSION
17
18 Version 0.04
19
20 =cut
21
22 our $VERSION = '0.04';
23
24 =head1 SYNOPSIS
25
26     perl -MO=RecDeparse,deparse,[@B__Deparse_opts],level,-1 [ -e '...' | bleh.pl ]
27
28     # Or as a module :
29     use B::RecDeparse;
30
31     my $brd = B::RecDeparse->new(deparse => [ @b__deparse_opts ], level => $level);
32     my $code = $brd->coderef2text(sub { ... });
33
34 =head1 DESCRIPTION
35
36 This module extends L<B::Deparse> by making it recursively replace subroutine calls encountered when deparsing.
37
38 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.
39
40 =head1 METHODS
41
42 =head2 C<< new < deparse => [ @B__Deparse_opts ], level => $level > >>
43
44 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.
45
46 =cut
47
48 use constant {
49  # p31268 made pp_entersub call single_delim
50  FOOL_SINGLE_DELIM =>
51      ($^V ge v5.9.5)
52   || ($^V lt v5.9.0 and $^V ge v5.8.9)
53   || ($Config{perl_patchlevel} && $Config{perl_patchlevel} >= 31268)
54 };
55
56 sub _parse_args {
57  if (@_ % 2) {
58   require Carp;
59   Carp::croak('Optional arguments must be passed as key/value pairs');
60  }
61  my %args = @_;
62
63  my $deparse = $args{deparse};
64  if (defined $deparse) {
65   if (!ref $deparse) {
66    $deparse = [ $deparse ];
67   } elsif (ref $deparse ne 'ARRAY') {
68    $deparse = [ ];
69   }
70  } else {
71   $deparse = [ ];
72  }
73
74  my $level = $args{level};
75  $level    = -1  unless defined $level;
76  $level    = int $level;
77
78  return $deparse, $level;
79 }
80
81 sub new {
82  my $class = shift;
83  $class = ref($class) || $class || __PACKAGE__;
84
85  my ($deparse, $level) = _parse_args(@_);
86
87  my $self = bless $class->SUPER::new(@$deparse), $class;
88
89  $self->{brd_level} = $level;
90
91  return $self;
92 }
93
94 sub _recurse {
95  return $_[0]->{brd_level} < 0 || $_[0]->{brd_cur} < $_[0]->{brd_level}
96 }
97
98 sub compile {
99  my @args = @_;
100
101  my $bd = B::Deparse->new();
102  my ($deparse, $level) = _parse_args(@args);
103
104  my $compiler = $bd->coderef2text(B::Deparse::compile(@$deparse));
105  $compiler =~ s/
106   ['"]? B::Deparse ['"]? \s* -> \s* (new) \s* \( ([^\)]*) \)
107  /B::RecDeparse->$1(deparse => [ $2 ], level => $level)/gx;
108  $compiler = eval 'sub ' . $compiler;
109  die if $@;
110
111  return $compiler;
112 }
113
114 sub init {
115  my $self = shift;
116
117  $self->{brd_cur} = 0;
118  $self->{brd_sub} = 0;
119
120  $self->SUPER::init(@_);
121 }
122
123 my $key = $; . __PACKAGE__ . $;;
124
125 if (FOOL_SINGLE_DELIM) {
126  my $oldsd = *B::Deparse::single_delim{CODE};
127
128  no warnings 'redefine';
129  *B::Deparse::single_delim = sub {
130   my $body = $_[2];
131
132   if ((caller 1)[0] eq __PACKAGE__ and $body =~ s/^$key//) {
133    return $body;
134   } else {
135    $oldsd->(@_);
136   }
137  }
138 }
139
140 sub pp_entersub {
141  my $self = shift;
142
143  my $body = do {
144   local $self->{brd_sub} = 1;
145   $self->SUPER::pp_entersub(@_);
146  };
147
148  $body =~ s/^&\s*(\w)/$1/ if $self->_recurse;
149
150  return $body;
151 }
152
153 sub pp_refgen {
154  my $self = shift;
155
156  return do {
157   local $self->{brd_sub} = 0;
158   $self->SUPER::pp_refgen(@_);
159  }
160 }
161
162 sub pp_gv {
163  my $self = shift;
164
165  my $body;
166  if ($self->{brd_sub} <= 0 || !$self->_recurse) {
167   $body = $self->SUPER::pp_gv(@_);
168  } else {
169   my $gv = $self->gv_or_padgv($_[0]);
170
171   $body = do {
172    local @{$self}{qw/brd_sub brd_cur/} = (0, $self->{brd_cur} + 1);
173    'sub ' . $self->indent($self->deparse_sub($gv->CV));
174   };
175
176   if (FOOL_SINGLE_DELIM) {
177    $body = $key . $body;
178   } else {
179    $body .= '->';
180   }
181  }
182
183  return $body;
184 }
185
186 =head2 C<compile>
187
188 =head2 C<init>
189
190 =head2 C<pp_entersub>
191
192 =head2 C<pp_refgen>
193
194 =head2 C<pp_gv>
195
196 Functions and methods from L<B::Deparse> reimplemented by this module. Never call them directly.
197
198 Otherwise, L<B::RecDeparse> inherits all methods from L<B::Deparse>.
199
200 =head1 EXPORT
201
202 An object-oriented module shouldn't export any function, and so does this one.
203
204 =head1 DEPENDENCIES
205
206 L<Carp> (standard since perl 5), L<Config> (since perl 5.00307) and L<B::Deparse> (since perl 5.005).
207
208 =head1 AUTHOR
209
210 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
211
212 You can contact me by mail or on C<irc.perl.org> (vincent).
213
214 =head1 BUGS
215
216 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.
217
218 =head1 SUPPORT
219
220 You can find documentation for this module with the perldoc command.
221
222     perldoc B::RecDeparse
223
224 Tests code coverage report is available at L<http://www.profvince.com/perl/cover/B-RecDeparse>.
225
226 =head1 COPYRIGHT & LICENSE
227
228 Copyright 2008,2009,2010 Vincent Pit, all rights reserved.
229
230 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
231
232 =cut
233
234 1; # End of B::RecDeparse