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