| 1 | package PPI::Transform; | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | =pod | ||||||||||||||||||
| 4 | |||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||
| 6 | |||||||||||||||||||
| 7 | PPI::Transform - Abstract base class for document transformation classes | ||||||||||||||||||
| 8 | |||||||||||||||||||
| 9 | =head1 DESCRIPTION | ||||||||||||||||||
| 10 | |||||||||||||||||||
| 11 | C<PPI::Transform> provides an API for the creation of classes and objects | ||||||||||||||||||
| 12 | that modify or transform PPI documents. | ||||||||||||||||||
| 13 | |||||||||||||||||||
| 14 | =head1 METHODS | ||||||||||||||||||
| 15 | |||||||||||||||||||
| 16 | =cut | ||||||||||||||||||
| 17 | |||||||||||||||||||
| 18 | 1 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 19 | 1 | ▶ | use Carp (); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 20 | 1 | ▶ | use List::Util (); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 21 | 1 | ▶ | use PPI::Document (); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 22 | 1 | ▶ | use Params::Util qw{_INSTANCE _CLASS _CODE _SCALAR0}; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 23 | |||||||||||||||||||
| 24 | 1 | our $VERSION = '1.260'; # VERSION | |||||||||||||||||
| 25 | |||||||||||||||||||
| 26 | |||||||||||||||||||
| 27 | |||||||||||||||||||
| 28 | |||||||||||||||||||
| 29 | |||||||||||||||||||
| 30 | ##################################################################### | ||||||||||||||||||
| 31 | # Apply Handler Registration | ||||||||||||||||||
| 32 | |||||||||||||||||||
| 33 | 1 | my %HANDLER; | |||||||||||||||||
| 34 | my @ORDER; | ||||||||||||||||||
| 35 | |||||||||||||||||||
| 36 | # Yes, you can use this yourself. | ||||||||||||||||||
| 37 | # I'm just leaving it undocumented for now. | ||||||||||||||||||
| 38 | sub register_apply_handler { | ||||||||||||||||||
| 39 | 3 | ▶ | my $class = shift; | ||||||||||||||||
|
Subroutine
register_apply_handler: called CC=5 SCAR=17.5
Pod
undocumented
| |||||||||||||||||||
| 40 | 3 | ▶ | my $handler = _CLASS(shift) or Carp::croak("Invalid PPI::Transform->register_apply_handler param"); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 41 | 3 | ▶ | my $get = _CODE(shift) or Carp::croak("Invalid PPI::Transform->register_apply_handler param"); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 42 | 3 | ▶ | my $set = _CODE(shift) or Carp::croak("Invalid PPI::Transform->register_apply_handler param"); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 43 | 3 | ▶ | if ( $HANDLER{$handler} ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 44 | 0 | Carp::croak("PPI::Transform->apply handler '$handler' already exists"); | |||||||||||||||||
| 45 | } | ||||||||||||||||||
| 46 | |||||||||||||||||||
| 47 | # Register the handler | ||||||||||||||||||
| 48 | 3 | $HANDLER{$handler} = [ $get, $set ]; | |||||||||||||||||
| 49 | 3 | unshift @ORDER, $handler; | |||||||||||||||||
| 50 | } | ||||||||||||||||||
| 51 | |||||||||||||||||||
| 52 | # Register the default handlers | ||||||||||||||||||
| 53 | 1 | __PACKAGE__->register_apply_handler( 'SCALAR', \&_SCALAR_get, \&_SCALAR_set ); | |||||||||||||||||
| 54 | 0 | ▶ | __PACKAGE__->register_apply_handler( 'PPI::Document', sub { $_[0] }, sub() { 1 } ); | ||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||
| 55 | |||||||||||||||||||
| 56 | |||||||||||||||||||
| 57 | |||||||||||||||||||
| 58 | |||||||||||||||||||
| 59 | |||||||||||||||||||
| 60 | ##################################################################### | ||||||||||||||||||
| 61 | # Constructor | ||||||||||||||||||
| 62 | |||||||||||||||||||
| 63 | =pod | ||||||||||||||||||
| 64 | |||||||||||||||||||
| 65 | =head2 new | ||||||||||||||||||
| 66 | |||||||||||||||||||
| 67 | my $transform = PPI::Transform->new( | ||||||||||||||||||
| 68 | param1 => 'value1', | ||||||||||||||||||
| 69 | param2 => 'value2', | ||||||||||||||||||
| 70 | ); | ||||||||||||||||||
| 71 | |||||||||||||||||||
| 72 | The C<new> constructor creates a new object for your C<PPI::Transform> | ||||||||||||||||||
| 73 | subclass. A default constructor is provided for you which takes no params | ||||||||||||||||||
| 74 | and creates a basic, empty, object. | ||||||||||||||||||
| 75 | |||||||||||||||||||
| 76 | If you wish to have your transform constructor take params, these B<must> | ||||||||||||||||||
| 77 | be in the form of a list of key/value pairs. | ||||||||||||||||||
| 78 | |||||||||||||||||||
| 79 | Returns a new C<PPI::Transform>-compatible object, or returns | ||||||||||||||||||
| 80 | C<undef> on error. | ||||||||||||||||||
| 81 | |||||||||||||||||||
| 82 | =cut | ||||||||||||||||||
| 83 | |||||||||||||||||||
| 84 | sub new { | ||||||||||||||||||
| 85 | 4 | ▶ | my $class = shift; | ||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 86 | 4 | bless { @_ }, $class; | |||||||||||||||||
| 87 | } | ||||||||||||||||||
| 88 | |||||||||||||||||||
| 89 | =pod | ||||||||||||||||||
| 90 | |||||||||||||||||||
| 91 | =head2 document | ||||||||||||||||||
| 92 | |||||||||||||||||||
| 93 | The C<document> method should be implemented by each subclass, and | ||||||||||||||||||
| 94 | takes a single argument of a L<PPI::Document> object, modifying it | ||||||||||||||||||
| 95 | B<in place> as appropriate for the particular transform class. | ||||||||||||||||||
| 96 | |||||||||||||||||||
| 97 | That's right, this method B<will not clone> and B<should not clone> | ||||||||||||||||||
| 98 | the document object. If you do not want the original to be modified, | ||||||||||||||||||
| 99 | you need to clone it yourself before passing it in. | ||||||||||||||||||
| 100 | |||||||||||||||||||
| 101 | Returns the numbers of changes made to the document. If the transform | ||||||||||||||||||
| 102 | is unable to track the quantity (including the situation where it cannot | ||||||||||||||||||
| 103 | tell B<IF> it made a change) it should return 1. Returns zero if no | ||||||||||||||||||
| 104 | changes were made to the document, or C<undef> if an error occurs. | ||||||||||||||||||
| 105 | |||||||||||||||||||
| 106 | By default this error is likely to only mean that you passed in something | ||||||||||||||||||
| 107 | that wasn't a L<PPI::Document>, but may include additional errors | ||||||||||||||||||
| 108 | depending on the subclass. | ||||||||||||||||||
| 109 | |||||||||||||||||||
| 110 | =cut | ||||||||||||||||||
| 111 | |||||||||||||||||||
| 112 | sub document { | ||||||||||||||||||
| 113 | 1 | ▶ | my $class = shift; | ||||||||||||||||
|
Subroutine
document: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 114 | 1 | die "$class does not implement the required ->document method"; | |||||||||||||||||
| 115 | } | ||||||||||||||||||
| 116 | |||||||||||||||||||
| 117 | =pod | ||||||||||||||||||
| 118 | |||||||||||||||||||
| 119 | =head2 apply | ||||||||||||||||||
| 120 | |||||||||||||||||||
| 121 | The C<apply> method is used to apply the transform to something. The | ||||||||||||||||||
| 122 | argument must be a L<PPI::Document>, or something which can be turned | ||||||||||||||||||
| 123 | into one and then be written back to again. | ||||||||||||||||||
| 124 | |||||||||||||||||||
| 125 | Currently, this list is limited to a C<SCALAR> reference, although a | ||||||||||||||||||
| 126 | handler registration process is available for you to add support for | ||||||||||||||||||
| 127 | additional types of object should you wish (see the source for this module). | ||||||||||||||||||
| 128 | |||||||||||||||||||
| 129 | Returns true if the transform was applied, false if there is an error in the | ||||||||||||||||||
| 130 | transform process, or may die if there is a critical error in the apply | ||||||||||||||||||
| 131 | handler. | ||||||||||||||||||
| 132 | |||||||||||||||||||
| 133 | =cut | ||||||||||||||||||
| 134 | |||||||||||||||||||
| 135 | sub apply { | ||||||||||||||||||
| 136 | 2 | ▶ | my $self = _SELF(shift); | ||||||||||||||||
|
Subroutine
apply: called CC=8 SCAR=22.1
Pod
documented
| |||||||||||||||||||
| 137 | 2 | ▶ | my $it = defined $_[0] ? shift : return undef; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 138 | |||||||||||||||||||
| 139 | # Try to find an apply handler | ||||||||||||||||||
| 140 | my $class = _SCALAR0($it) ? 'SCALAR' | ||||||||||||||||||
| 141 | 1 | ▶ | : List::Util::first { _INSTANCE($it, $_) } @ORDER | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 142 | 2 | ▶ | or return undef; | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 143 | 2 | ▶ | my $handler = $HANDLER{$class} | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 144 | or die("->apply handler for $class missing! Panic"); | ||||||||||||||||||
| 145 | |||||||||||||||||||
| 146 | # Get, change, set | ||||||||||||||||||
| 147 | 2 | ▶ | my $Document = _INSTANCE($handler->[0]->($it), 'PPI::Document') | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 148 | or Carp::croak("->apply handler for $class failed to get a PPI::Document"); | ||||||||||||||||||
| 149 | 2 | ▶ | $self->document( $Document ) or return undef; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 150 | 2 | ▶ | $handler->[1]->($it, $Document) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 151 | or Carp::croak("->apply handler for $class failed to save the changed document"); | ||||||||||||||||||
| 152 | 2 | 1; | |||||||||||||||||
| 153 | } | ||||||||||||||||||
| 154 | |||||||||||||||||||
| 155 | =pod | ||||||||||||||||||
| 156 | |||||||||||||||||||
| 157 | =head2 file | ||||||||||||||||||
| 158 | |||||||||||||||||||
| 159 | # Read from one file and write to another | ||||||||||||||||||
| 160 | $transform->file( 'Input.pm' => 'Output.pm' ); | ||||||||||||||||||
| 161 | |||||||||||||||||||
| 162 | # Change a file in place | ||||||||||||||||||
| 163 | $transform->file( 'Change.pm' ); | ||||||||||||||||||
| 164 | |||||||||||||||||||
| 165 | The C<file> method modifies a Perl document by filename. If passed a single | ||||||||||||||||||
| 166 | parameter, it modifies the file in-place. If provided a second parameter, | ||||||||||||||||||
| 167 | it will attempt to save the modified file to the alternative filename. | ||||||||||||||||||
| 168 | |||||||||||||||||||
| 169 | Returns true on success, or C<undef> on error. | ||||||||||||||||||
| 170 | |||||||||||||||||||
| 171 | =cut | ||||||||||||||||||
| 172 | |||||||||||||||||||
| 173 | sub file { | ||||||||||||||||||
| 174 | 2 | ▶ | my $self = _SELF(shift); | ||||||||||||||||
|
Subroutine
file: called CC=7 SCAR=20.9
Pod
documented
| |||||||||||||||||||
| 175 | |||||||||||||||||||
| 176 | # Where do we read from and write to | ||||||||||||||||||
| 177 | 2 | ▶ | my $input = defined $_[0] ? shift : return undef; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 178 | 2 | ▶ | my $output = @_ ? defined $_[0] ? "$_[0]" : undef : $input or return undef; | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 179 | |||||||||||||||||||
| 180 | # Process the file | ||||||||||||||||||
| 181 | 2 | ▶ | my $Document = PPI::Document->new( "$input" ) or return undef; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 182 | 2 | ▶ | $self->document( $Document ) or return undef; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 183 | 2 | $Document->save( $output ); | |||||||||||||||||
| 184 | } | ||||||||||||||||||
| 185 | |||||||||||||||||||
| 186 | |||||||||||||||||||
| 187 | |||||||||||||||||||
| 188 | |||||||||||||||||||
| 189 | |||||||||||||||||||
| 190 | ##################################################################### | ||||||||||||||||||
| 191 | # Apply Hander Methods | ||||||||||||||||||
| 192 | |||||||||||||||||||
| 193 | sub _SCALAR_get { | ||||||||||||||||||
| 194 | 1 | ▶ | PPI::Document->new( $_[0] ); | ||||||||||||||||
|
Subroutine
_SCALAR_get: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 195 | } | ||||||||||||||||||
| 196 | |||||||||||||||||||
| 197 | sub _SCALAR_set { | ||||||||||||||||||
| 198 | 1 | ▶ | my $it = shift; | ||||||||||||||||
|
Subroutine
_SCALAR_set: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 199 | 1 | $$it = $_[0]->serialize; | |||||||||||||||||
| 200 | 1 | 1; | |||||||||||||||||
| 201 | } | ||||||||||||||||||
| 202 | |||||||||||||||||||
| 203 | |||||||||||||||||||
| 204 | |||||||||||||||||||
| 205 | |||||||||||||||||||
| 206 | |||||||||||||||||||
| 207 | ##################################################################### | ||||||||||||||||||
| 208 | # Support Functions | ||||||||||||||||||
| 209 | |||||||||||||||||||
| 210 | sub _SELF { | ||||||||||||||||||
| 211 | 4 | ▶ | return shift if ref $_[0]; | ||||||||||||||||
|
Subroutine
_SELF: called CC=3 SCAR=11.7
Branch: 1 branch
| |||||||||||||||||||
| 212 | 4 | ▶ | my $self = $_[0]->new or Carp::croak( | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 213 | "Failed to auto-instantiate new $_[0] object" | ||||||||||||||||||
| 214 | ); | ||||||||||||||||||
| 215 | 4 | $self; | |||||||||||||||||
| 216 | } | ||||||||||||||||||
| 217 | |||||||||||||||||||
| 218 | 1 | 1; | |||||||||||||||||
| 219 | |||||||||||||||||||
| 220 | =pod | ||||||||||||||||||
| 221 | |||||||||||||||||||
| 222 | =head1 SUPPORT | ||||||||||||||||||
| 223 | |||||||||||||||||||
| 224 | See the L<support section|PPI/SUPPORT> in the main module. | ||||||||||||||||||
| 225 | |||||||||||||||||||
| 226 | =head1 AUTHOR | ||||||||||||||||||
| 227 | |||||||||||||||||||
| 228 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||||||||||||||||||
| 229 | |||||||||||||||||||
| 230 | =head1 COPYRIGHT | ||||||||||||||||||
| 231 | |||||||||||||||||||
| 232 | Copyright 2001 - 2011 Adam Kennedy. | ||||||||||||||||||
| 233 | |||||||||||||||||||
| 234 | This program is free software; you can redistribute | ||||||||||||||||||
| 235 | it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||
| 236 | |||||||||||||||||||
| 237 | The full text of the license can be found in the | ||||||||||||||||||
| 238 | LICENSE file included with this module. | ||||||||||||||||||
| 239 | |||||||||||||||||||
| 240 | =cut | ||||||||||||||||||