| 1 | package Filter::PPI; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use Filter::Util::Call; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use PPI::Document; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Scalar::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | our $VERSION = '0.00_01'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 3 | my $caller = caller; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | { | ||||||||||||||||||||||||||||
| 17 | 2 | ▶ | no warnings qw/redefine/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 2 | ▶ | no strict qw/refs/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 3 | ▶ | *{$caller.'::import'} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 2 | my ($class,@args) = @_; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 3 | ▶ | filter_add (sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=18.2
| |||||||||||||||||||||||||||||
| 27 | 3 | my $status; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 3 | ▶ | Carp::confess "Filter error ($status) while reading source" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | if ($status = filter_read (4096)) < 0; | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 3 | ▶ | return $status unless $status > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 2 | ▶ | my $doc = PPI::Document->new (\$_) or Carp::confess "Source could not be understood by PPI"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 2 | $doc = $class->ppi_filter ($doc,@args); | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 2 | ▶ | Carp::confess "$class->ppi_filter did not return a document" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($doc) and $doc->isa("PPI::Document")
MC/DC50%1/2
builtin::blessed($doc) and $doc->isa("PPI::Document")
builtin::blessed($doc)
$doc->isa("PPI::Document")
Truth table66%2/3
A and B
A= builtin::blessed($doc)B= $doc->isa("PPI::Document")
| |||||||||||||||||||||||||||||
| 39 | unless Scalar::Util::blessed $doc && $doc->isa ('PPI::Document'); | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 1 | $_ = $doc->serialize; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 1 | return $status; | |||||||||||||||||||||||||||
| 44 | 2 | }); | |||||||||||||||||||||||||||
| 45 | 3 | }; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 3 | return; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 2 | 1; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =pod | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | =head1 NAME | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | Filter::PPI - PPI based source filtering | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | package MyFilter; | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | use Filter::PPI; | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | sub ppi_filter { | ||||||||||||||||||||||||||||
| 66 | my ($class,$doc) = @_; | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # Do funny things to $doc here | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | return $doc; | ||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | package MyModule; | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | use MyFilter; | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # This will get filtered | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | This module lets you write perl source filters using PPI to process | ||||||||||||||||||||||||||||
| 82 | the source. Compared to other modules used for writing source filters, | ||||||||||||||||||||||||||||
| 83 | it is quite simple. You only need to have one method in your class, | ||||||||||||||||||||||||||||
| 84 | called ppi_filter. This gets passed the class name, a L<PPI::Document> | ||||||||||||||||||||||||||||
| 85 | object, and any arguments given to import. The method is expected to | ||||||||||||||||||||||||||||
| 86 | return a L<PPI::Document> but not neccesarily the same one. The | ||||||||||||||||||||||||||||
| 87 | document returned will be what's used by perl to compile the program. | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =over 4 | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =item L<Filter::Util::Call> | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =item L<PPI> | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =back | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =head1 BUGS | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | Most software has bugs. This module probably isn't an exception. | ||||||||||||||||||||||||||||
| 102 | If you find a bug please either email me, or add the bug to cpan-RT. | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | Anders Nor Berle E<lt>berle@cpan.orgE<gt> | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | Copyright 2008 by Anders Nor Berle. | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 113 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =cut | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||