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