| 1 | package B::CutNPaste; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use vars qw( @ISA $VERSION ); | ||
| 5 | use B qw( main_cv main_root main_start ); | ||
| 6 | use B::Deparse; | ||
| 7 | |||
| 8 | BEGIN { | ||
| 9 | @ISA = 'B::Deparse'; | ||
| 10 | $VERSION = '0.31'; | ||
| 11 | |||
| 12 | for my $func (qw( begin_av init_av check_av end_av )) { | ||
| 13 | |||
| 14 | ## no critic | ||
| 15 | no strict 'refs'; | ||
| 16 | if ( defined &{"B::$func"} ) { | ||
| 17 | B->import($func); | ||
| 18 | } | ||
| 19 | else { | ||
| 20 | |||
| 21 | # If I couldn't create it, I'll just declare it to keep lint happy. | ||
| 22 | eval "sub $func;"; | ||
| 23 | } | ||
| 24 | } | ||
| 25 | |||
| 26 | # B::perlstring was added in 5.8.0 | ||
| 27 | if ( defined &B::perlstring ) { | ||
| 28 | B->import('perlstring'); | ||
| 29 | } | ||
| 30 | else { | ||
| 31 | *perlstring = sub { '"' . quotemeta( shift @_ ) . '"' }; | ||
| 32 | } | ||
| 33 | |||
| 34 | } | ||
| 35 | |||
| 36 | use Carp 'confess'; | ||
| 37 | use IO::Handle (); | ||
| 38 | |||
| 39 | # use Data::Postponed 'postpone_forever'; | ||
| 40 | sub postpone_forever { return shift @_ } | ||
| 41 | |||
| 42 | #_# OVERRIDE METHODS FROM B::Deparse | ||
| 43 | sub new { | ||
| 44 | my $class = shift @_; | ||
| 45 | my $self = $class->SUPER::new(@_); | ||
| 46 | $self->{__rename_vars} = $ENV{RENAME_VARS}; | ||
| 47 | $self->{__rename_subs} = $ENV{RENAME_SUBS}; | ||
| 48 | $self->{linenums} = 1; | ||
| 49 | return $self; | ||
| 50 | } | ||
| 51 | |||
| 52 | sub compile { ## no critic Complex | ||
| 53 | my (@args) = @_; | ||
| 54 | |||
| 55 | return sub { | ||
| 56 | my $source = ''; | ||
| 57 | my $self = __PACKAGE__->new(@args); | ||
| 58 | |||
| 59 | # First deparse command-line args | ||
| 60 | if ( defined $^I ) { # deparse -i | ||
| 61 | $source .= q(BEGIN { $^I = ) . perlstring($^I) . qq(; }\n); | ||
| 62 | } | ||
| 63 | if ($^W) { # deparse -w | ||
| 64 | $source .= qq(BEGIN { \$^W = $^W; }\n); | ||
| 65 | } | ||
| 66 | ## no critic PackageVar | ||
| 67 | if ( $/ ne "\n" or defined $O::savebackslash ) { # deparse -l -0 | ||
| 68 | my $fs = perlstring($/) || 'undef'; | ||
| 69 | my $bs = perlstring($O::savebackslash) || 'undef'; | ||
| 70 | $source .= qq(BEGIN { \$/ = $fs; \$\\ = $bs; }\n); | ||
| 71 | } | ||
| 72 | |||
| 73 | # I need to do things differently depending on the perl | ||
| 74 | # version. | ||
| 75 | if ( $] >= 5.008 ) { | ||
| 76 | if ( defined &begin_av | ||
| 77 | and begin_av->isa('B::AV') ) | ||
| 78 | { | ||
| 79 | $self->todo( $_, 0 ) for begin_av->ARRAY; | ||
| 80 | } | ||
| 81 | if ( defined &check_av | ||
| 82 | and check_av->isa('B::AV') ) | ||
| 83 | { | ||
| 84 | $self->todo( $_, 0 ) for check_av->ARRAY; | ||
| 85 | } | ||
| 86 | if ( defined &init_av | ||
| 87 | and init_av->isa('B::AV') ) | ||
| 88 | { | ||
| 89 | $self->todo( $_, 0 ) for init_av->ARRAY; | ||
| 90 | } | ||
| 91 | if ( defined &end_av | ||
| 92 | and end_av->isa('B::AV') ) | ||
| 93 | { | ||
| 94 | $self->todo( $_, 0 ) for end_av->ARRAY; | ||
| 95 | } | ||
| 96 | |||
| 97 | $self->stash_subs; | ||
| 98 | $self->{curcv} = main_cv; | ||
| 99 | $self->{curcvlex} = undef; | ||
| 100 | } | ||
| 101 | else { | ||
| 102 | |||
| 103 | # 5.6.x | ||
| 104 | $self->stash_subs('main'); | ||
| 105 | $self->{curcv} = main_cv; | ||
| 106 | $self->walk_sub( main_cv, main_start ); | ||
| 107 | } | ||
| 108 | |||
| 109 | $source .= join "\n", $self->print_protos; | ||
| 110 | @{ $self->{subs_todo} } | ||
| 111 | = sort { $a->[0] <=> $b->[0] } @{ $self->{subs_todo} }; | ||
| 112 | $source .= join "\n", $self->indent( $self->deparse( main_root, 0 ) ), | ||
| 113 | "\n" | ||
| 114 | unless B::Deparse::null main_root; | ||
| 115 | # B::Deparse | ||
| 116 | my @text; | ||
| 117 | while ( scalar @{ $self->{subs_todo} } ) { | ||
| 118 | push @text, $self->next_todo; | ||
| 119 | } | ||
| 120 | $source .= join "\n", $self->indent( join "", @text ), "\n" | ||
| 121 | if @text; | ||
| 122 | |||
| 123 | # Print __DATA__ section, if necessary | ||
| 124 | my $laststash | ||
| 125 | = defined $self->{curcop} | ||
| 126 | ? $self->{curcop}->stash->NAME | ||
| 127 | : $self->{curstash}; | ||
| 128 | { | ||
| 129 | ## no critic | ||
| 130 | no strict 'refs'; | ||
| 131 | ## use critic | ||
| 132 | if ( defined *{ $laststash . "::DATA" } ) { | ||
| 133 | if ( eof *{ $laststash . "::DATA" } ) { | ||
| 134 | |||
| 135 | # I think this only happens when using B::Deobfuscate | ||
| 136 | # on itself. | ||
| 137 | { | ||
| 138 | local $/ = "__DATA__\n"; | ||
| 139 | seek *{ $laststash . "::DATA" }, 0, 0; | ||
| 140 | readline *{ $laststash . "::DATA" }; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | $source .= "__DATA__\n"; | ||
| 145 | $source .= join '', readline *{ $laststash . "::DATA" }; | ||
| 146 | } | ||
| 147 | } | ||
| 148 | |||
| 149 | print($source); | ||
| 150 | |||
| 151 | return; | ||
| 152 | }; | ||
| 153 | } | ||
| 154 | |||
| 155 | # get rid of %^H data | ||
| 156 | { | ||
| 157 | no warnings 'redefine'; | ||
| 158 | sub B::Deparse::declare_hinthash {} | ||
| 159 | } | ||
| 160 | |||
| 161 | sub padname { | ||
| 162 | my $self = shift @_; | ||
| 163 | my $padname = $self->SUPER::padname(@_); | ||
| 164 | $padname =~ s/\w+/XXX/g if $self->{__rename_vars}; | ||
| 165 | return $padname; | ||
| 166 | } | ||
| 167 | |||
| 168 | sub gv_name { | ||
| 169 | my $self = shift; | ||
| 170 | my $gv_name = $self->SUPER::gv_name(@_); | ||
| 171 | |||
| 172 | # XXX Somes modules break if we s/_/YYYY/ due to the | ||
| 173 | # following: | ||
| 174 | # | ||
| 175 | # $body = $kid->first->first->sibling; # skip OP_AND and OP_ITER | ||
| 176 | # if (!is_state $body->first and $body->first->name ne "stub") { | ||
| 177 | # confess unless $var eq '$_'; # XXX here's where we get an empty confess | ||
| 178 | # $body = $body->first; | ||
| 179 | # return $self->deparse($body, 2) . " foreach ($ary)"; | ||
| 180 | # } | ||
| 181 | # | ||
| 182 | # Also, it's probably only the BEGIN block, but if you rename that, your | ||
| 183 | # code parses differently. | ||
| 184 | if ( $gv_name ne '_' and $gv_name !~ /BEGIN|CHECK|INIT|END/ ) { | ||
| 185 | $gv_name =~ s/\w+/YYYY/g if $self->{__rename_subs}; | ||
| 186 | } | ||
| 187 | return $gv_name; | ||
| 188 | } | ||
| 189 | |||
| 190 | 1; | ||
| 191 | |||
| 192 | __END__ |