]> git.vpit.fr Git - perl/modules/B-RecDeparse.git/blobdiff - lib/B/RecDeparse.pm
This is 0.10
[perl/modules/B-RecDeparse.git] / lib / B / RecDeparse.pm
index 12df138ed059273de20251c0a0818499bc07ccfa..286b28920ba06f29f1650448ac788b9b81d89f94 100644 (file)
@@ -1,6 +1,6 @@
 package B::RecDeparse;
 
-use 5.008;
+use 5.008_001;
 
 use strict;
 use warnings;
@@ -17,20 +17,27 @@ B::RecDeparse - Deparse recursively into subroutines.
 
 =head1 VERSION
 
-Version 0.04
+Version 0.10
 
 =cut
 
-our $VERSION = '0.04';
+our $VERSION = '0.10';
 
 =head1 SYNOPSIS
 
-    perl -MO=RecDeparse,deparse,[@B__Deparse_opts],level,-1 [ -e '...' | bleh.pl ]
+    # Deparse recursively a Perl one-liner :
+    $ perl -MO=RecDeparse,deparse,@B__Deparse_opts,level,-1 -e '...'
 
-    # Or as a module :
+    # Or a complete Perl script :
+    $ perl -MO=RecDeparse,deparse,@B__Deparse_opts,level,-1 x.pl
+
+    # Or a single code reference :
     use B::RecDeparse;
 
-    my $brd = B::RecDeparse->new(deparse => [ @b__deparse_opts ], level => $level);
+    my $brd = B::RecDeparse->new(
+     deparse => \@B__Deparse_opts,
+     level   => $level,
+    );
     my $code = $brd->coderef2text(sub { ... });
 
 =head1 DESCRIPTION
@@ -42,7 +49,12 @@ Besides the constructor syntax, everything should work the same for the two modu
 
 =head1 METHODS
 
-=head2 C<< new < deparse => [ @B__Deparse_opts ], level => $level > >>
+=head2 C<new>
+
+    my $brd = B::RecDeparse->new(
+     deparse => \@B__Deparse_opts,
+     level   => $level,
+    );
 
 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.
@@ -53,8 +65,8 @@ The C<level> option expects an integer that specifies how many levels of recursi
 use constant {
  # p31268 made pp_entersub call single_delim
  FOOL_SINGLE_DELIM =>
-     ($^V ge v5.9.5)
-  || ($^V lt v5.9.0 and $^V ge v5.8.9)
+     ("$]" >= 5.009_005)
+  || ("$]" <  5.009 and "$]" >= 5.008_009)
   || ($Config{perl_patchlevel} && $Config{perl_patchlevel} >= 31268)
 };
 
@@ -178,22 +190,33 @@ sub pp_refgen {
  }
 }
 
+sub pp_srefgen {
+ my $self = shift;
+
+ return do {
+  local $self->{brd_sub} = 0;
+  $self->SUPER::pp_srefgen(@_);
+ }
+}
+
 sub pp_gv {
  my $self = shift;
 
  my $gv   = $self->gv_or_padgv($_[0]);
- my $name = $gv->NAME;
- my $cv   = $gv->CV;
+ my $cv   = $gv->FLAGS & B::SVf_ROK ? $gv->RV : undef;
+ my $name = $cv ? $cv->NAME_HEK || $cv->GV->NAME : $gv->NAME;
+ $cv    ||= $gv->CV;
  my $seen = $self->{brd_seen};
 
  my $body;
- if (!$self->{brd_sub} or !$self->_recurse or $seen->{$name} or !$$cv) {
+ if (!$self->{brd_sub} or !$self->_recurse or $seen->{$name} or !$$cv
+     or !$cv->isa('B::CV') or $cv->ROOT->isa('B::NULL')) {
   $body = $self->SUPER::pp_gv(@_);
  } else {
   $body = do {
    local @{$self}{qw<brd_sub brd_cur>} = (0, $self->{brd_cur} + 1);
    local $seen->{$name} = 1;
-   'sub ' . $self->indent($self->deparse_sub($gv->CV));
+   'sub ' . $self->indent($self->deparse_sub($cv));
   };
 
   if (FOOL_SINGLE_DELIM) {
@@ -206,20 +229,41 @@ sub pp_gv {
  return $body;
 }
 
-=head2 C<compile>
+=pod
+
+The following functions and methods from L<B::Deparse> are reimplemented by this module :
+
+=over 4
 
-=head2 C<init>
+=item *
 
-=head2 C<deparse_sub>
+C<compile>
 
-=head2 C<pp_entersub>
+=item *
 
-=head2 C<pp_refgen>
+C<init>
 
-=head2 C<pp_gv>
+=item *
 
-Functions and methods from L<B::Deparse> reimplemented by this module.
-Never call them directly.
+C<deparse_sub>
+
+=item *
+
+C<pp_entersub>
+
+=item *
+
+C<pp_refgen>
+
+=item *
+
+C<pp_srefgen>
+
+=item *
+
+C<pp_gv>
+
+=back
 
 Otherwise, L<B::RecDeparse> inherits all methods from L<B::Deparse>.
 
@@ -229,6 +273,8 @@ An object-oriented module shouldn't export any function, and so does this one.
 
 =head1 DEPENDENCIES
 
+L<perl> 5.8.1.
+
 L<Carp> (standard since perl 5), L<Config> (since perl 5.00307) and L<B::Deparse> (since perl 5.005).
 
 =head1 AUTHOR
@@ -252,7 +298,7 @@ Tests code coverage report is available at L<http://www.profvince.com/perl/cover
 
 =head1 COPYRIGHT & LICENSE
 
-Copyright 2008,2009,2010,2011 Vincent Pit, all rights reserved.
+Copyright 2008,2009,2010,2011,2013,2014,2015 Vincent Pit, all rights reserved.
 
 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.