File Coverage

blib/lib/Pod/CopyrightYears.pm
Criterion Covered Total %
statement 70 71 98.5
branch 16 20 80.0
condition n/a
subroutine 11 11 100.0
pod 4 4 100.0
total 101 106 95.2


line stmt bran cond sub pod time code
1             package Pod::CopyrightYears;
2              
3 5     5   135573 use strict;
  5         38  
  5         142  
4 5     5   26 use warnings;
  5         11  
  5         141  
5              
6 5     5   989 use Class::Utils qw(set_params);
  5         26321  
  5         182  
7 5     5   191 use Error::Pure qw(err);
  5         9  
  5         189  
8 5     5   2687 use Pod::Abstract;
  5         137749  
  5         3839  
9              
10             our $VERSION = 0.01;
11              
12             # Constructor.
13             sub new {
14 14     14 1 10245 my ($class, @params) = @_;
15              
16             # Create object.
17 14         39 my $self = bless {}, $class;
18              
19             # Pod file to update.
20 14         52 $self->{'pod_file'} = undef;
21              
22             # Section names to update.
23 14         39 $self->{'section_names'} = [
24             'LICENSE AND COPYRIGHT',
25             ];
26              
27             # Process parameters.
28 14         46 set_params($self, @params);
29              
30 13 100       195 if (! defined $self->{'pod_file'}) {
31 1         5 err "Parameter 'pod_file' is required.";
32             }
33 12         56 $self->{'pod_abstract'} = Pod::Abstract->load_file($self->{'pod_file'});
34              
35 12         46136 return $self;
36             }
37              
38             sub change_years {
39 4     4 1 24 my ($self, $year) = @_;
40              
41 4 50       12 if (! defined $year) {
42 0         0 $year = (localtime(time))[5] + 1900;
43             }
44              
45 4         12 foreach my $pod_node ($self->license_sections) {
46 2         6 $self->_iterate_node($pod_node, $year);
47             }
48              
49 4         8 return;
50             }
51              
52             sub license_sections {
53 7     7 1 33 my $self = shift;
54              
55 7         14 my @pod_nodes;
56 7         13 foreach my $section (@{$self->{'section_names'}}) {
  7         21  
57 7         46 my ($pod_node) = $self->{'pod_abstract'}->select('/head1[@heading =~ {'.$section.'}]');
58 7 100       4350 if (defined $pod_node) {
59 4         11 push @pod_nodes, $pod_node;
60             }
61             }
62              
63 7         31 return @pod_nodes;
64             }
65              
66             sub pod {
67 8     8 1 35 my $self = shift;
68              
69 8         24 my $pod = $self->{'pod_abstract'}->pod;
70 8         3369 chomp $pod;
71 8         16 my $ret = $pod;
72 8 100       40 if ($pod =~ m/=cut\s+$/ms) {
73 6         20 $ret = substr $pod, 0, -2;
74 6         12 $ret .= "\n";
75             } else {
76 2         5 $ret .= "\n";
77             }
78              
79 8         21 return $ret;
80             }
81              
82             sub _iterate_node {
83 8     8   16 my ($self, $pod_node, $year) = @_;
84              
85 8 50       18 if (defined $pod_node->children) {
86 8         88 foreach my $child ($pod_node->children) {
87 10 100       70 if ($child->type eq ':text') {
88 4         27 $self->_change_years($child, $year);
89             } else {
90 6         43 $self->_iterate_node($child, $year);
91             }
92             }
93             }
94              
95 8         31 return;
96             }
97              
98             sub _change_years {
99 4     4   8 my ($self, $pod_node, $year) = @_;
100              
101 4         9 my $text = $pod_node->pod;
102 4 100       211 if ($text =~ m/^(.*)(\d{4})-(\d{4})(.*)$/ms) {
    100          
103 1         3 my $pre = $1;
104 1         3 my $first_year = $2;
105 1         2 my $last_year = $3;
106 1         3 my $post = $4;
107 1 50       4 if ($last_year != $year) {
108 1         4 my $new_text = $pre.$first_year.'-'.$year.$post;
109 1         4 $pod_node->body($new_text);
110             }
111             } elsif ($text =~ m/^(.*)(\d{4})(.*)$/ms) {
112 1         4 my $pre = $1;
113 1         3 my $first_year = $2;
114 1         3 my $post = $3;
115 1 50       8 if ($first_year != $year) {
116 1         5 my $new_text = $pre.$first_year.'-'.$year.$post;
117 1         4 $pod_node->body($new_text);
118             }
119             }
120              
121 4         25 return;
122             }
123              
124             1;
125              
126             __END__