1 package PPI::Transform::UpdateCopyright;
7 PPI::Transform::UpdateCopyright - Demonstration PPI::Transform class
11 my $transform = PPI::Transform::UpdateCopyright->new(
12 name => 'Adam Kennedy'
15 $transform->file('Module.pm');
19 B<PPI::Transform::UpdateCopyright> provides a demonstration of a typical
20 L<PPI::Transform> class.
22 This class implements a document transform that will take the name of an
23 author and update the copyright statement to refer to the current year,
24 if it does not already do so.
31 use Params::Util qw{_STRING};
32 use PPI::Transform ();
34 use vars qw{$VERSION};
43 #####################################################################
44 # Constructor and Accessors
50 my $transform = PPI::Transform::UpdateCopyright->new(
51 name => 'Adam Kennedy'
54 The C<new> constructor creates a new transform object for a specific
55 author. It takes a single C<name> parameter that should be the name
56 (or longer string) for the author.
58 Specifying the name is required to allow the changing of a subset of
59 copyright statements that refer to you from a larger set in a file.
64 my $self = shift->SUPER::new(@_);
67 unless ( defined _STRING($self->name) ) {
68 PPI::Exception->throw("Did not provide a valid name param");
78 The C<name> accessor returns the author name that the transform will be
79 searching for copyright statements of.
91 #####################################################################
96 my $document = _INSTANCE(shift, 'PPI::Document') or return undef;
98 # Find things to transform
99 my $name = quotemeta $self->name;
100 my $regexp = qr/\bcopyright\b.*$name/m;
101 my $elements = $document->find( sub {
102 $_[1]->isa('PPI::Token::Pod') or return '';
103 $_[1]->content =~ $regexp or return '';
106 return undef unless defined $elements;
107 return 0 unless $elements;
109 # Try to transform any elements
112 my $copyright = shift;
113 my $thisyear = (localtime time)[5] + 1900;
114 my @year = $copyright =~ m/(\d{4})/g;
117 # Handle the single year format
118 if ( $year[0] == $thisyear ) {
122 # Convert from single year to multiple year
124 $copyright =~ s/(\d{4})/$1 - $thisyear/;
130 # Handle the range format
131 if ( $year[1] == $thisyear ) {
135 # Change the second year to the current one
137 $copyright =~ s/$year[1]/$thisyear/;
143 die "Invalid or unknown copyright line '$copyright'";
146 # Attempt to transform each element
147 my $pattern = qr/\b(copyright.*\d)({4}(?:\s*-\s*\d{4})?)(.*$name)/mi;
148 foreach my $element ( @$elements ) {
149 $element =~ s/$pattern/$1 . $change->($2) . $2/eg;
161 - May need to overload some methods to forcefully prevent Document
162 objects becoming children of another Node.
166 See the L<support section|PPI/SUPPORT> in the main module.
170 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
174 Copyright 2009 Adam Kennedy.
176 This program is free software; you can redistribute
177 it and/or modify it under the same terms as Perl itself.
179 The full text of the license can be found in the
180 LICENSE file included with this module.