| 1 | package PAR::Filter; | ||
| 2 | use 5.006; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | our $VERSION = '0.03'; | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | PAR::Filter - Input filter for PAR | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | $code = 'use strict; print "Hello, World!\n";'; | ||
| 14 | $ref = PAR::Filter->new('PodStrip', 'Bleach')->apply(\$code); | ||
| 15 | print $code; # pod-stripped and obfuscated code | ||
| 16 | print $$ref; # same thing | ||
| 17 | |||
| 18 | $ref = PAR::Filter->new('PodStrip', 'Bleach')->apply('file.pl'); | ||
| 19 | print $$ref; # same thing, applied to file.pl | ||
| 20 | |||
| 21 | =head1 DESCRIPTION | ||
| 22 | |||
| 23 | Starting with PAR 0.76, C<pp -f> takes a filter name, like C<Bleach>, and | ||
| 24 | invokes this module to transform the programs with L<PAR::Filter::Bleach>. | ||
| 25 | Similarly, C<pp -F Bleach> applies the B<Bleach> filter to all included | ||
| 26 | modules. | ||
| 27 | |||
| 28 | It is possible to pass in multiple such filters, which are applied in turn. | ||
| 29 | |||
| 30 | The output of each such filter is expected be semantically equivalent to the | ||
| 31 | input, although possibly obfuscated. | ||
| 32 | |||
| 33 | The default list of filters include: | ||
| 34 | |||
| 35 | =over 4 | ||
| 36 | |||
| 37 | =item * L<PAR::Filter::Bleach> | ||
| 38 | |||
| 39 | The archetypical obfuscating filter. | ||
| 40 | |||
| 41 | =item * L<PAR::Filter::Obfuscate> | ||
| 42 | |||
| 43 | Use L<B::Deobfuscate> to strip away indents and comments, as well as mangling | ||
| 44 | variable names. | ||
| 45 | |||
| 46 | =item * L<PAR::Filter::PatchContent> | ||
| 47 | |||
| 48 | Fix PAR-incompatible modules, applied to modules by default. | ||
| 49 | |||
| 50 | =item * L<PAR::Filter::PodStrip> | ||
| 51 | |||
| 52 | Strip away POD sections, applied to modules by default. | ||
| 53 | |||
| 54 | =item * L<PAR::Filter::Bytecode> | ||
| 55 | |||
| 56 | Use L<B::Bytecode> to strip away indents and comments. | ||
| 57 | B<This filter is deprecated!> For source hiding, | ||
| 58 | have a look at L<PAR::Filter::Crypto> instead and make | ||
| 59 | sure to read about its caveats. | ||
| 60 | |||
| 61 | =back | ||
| 62 | |||
| 63 | =cut | ||
| 64 | |||
| 65 | sub new { | ||
| 66 | my $class = shift; | ||
| 67 | require "PAR/Filter/$_.pm" foreach @_; | ||
| 68 | bless(\@_, $class); | ||
| 69 | } | ||
| 70 | |||
| 71 | sub apply { | ||
| 72 | my ($self, $ref, $name) = @_; | ||
| 73 | my $filename = $name || '-e'; | ||
| 74 | |||
| 75 | if (!ref $ref) { | ||
| 76 | $name ||= $filename = $ref; | ||
| 77 | local $/; | ||
| 78 | open my $fh, "<", $ref or die $!; | ||
| 79 | binmode($fh); | ||
| 80 | my $content = <$fh>; | ||
| 81 | $ref = \$content; | ||
| 82 | return $ref unless length($content); | ||
| 83 | } | ||
| 84 | |||
| 85 | "PAR::Filter::$_"->new->apply( $ref, $filename, $name ) foreach @$self; | ||
| 86 | |||
| 87 | return $ref; | ||
| 88 | } | ||
| 89 | |||
| 90 | 1; | ||
| 91 | |||
| 92 | =head1 AUTHORS | ||
| 93 | |||
| 94 | Audrey Tang E<lt>cpan@audreyt.orgE<gt> | ||
| 95 | |||
| 96 | =head1 COPYRIGHT | ||
| 97 | |||
| 98 | Copyright 2003-2009 by Audrey Tang E<lt>cpan@audreyt.orgE<gt>. | ||
| 99 | |||
| 100 | This program is free software; you can redistribute it and/or | ||
| 101 | modify it under the same terms as Perl itself. | ||
| 102 | |||
| 103 | See F<LICENSE>. | ||
| 104 | |||
| 105 | =cut |