File Coverage

blib/lib/Perl/Critic/Policy/Modules/PerlMinimumVersion.pm
Criterion Covered Total %
statement 44 46 95.6
branch 10 14 71.4
condition n/a
subroutine 13 13 100.0
pod 4 5 80.0
total 71 78 91.0


line stmt bran cond sub pod time code
1             #######################################################################
2             # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/distributions/Perl-Critic-More/lib/Perl/Critic/Policy/Modules/PerlMinimumVersion.pm $
3             # $Date: 2013-10-29 09:39:11 -0700 (Tue, 29 Oct 2013) $
4             # $Author: thaljef $
5             # $Revision: 4222 $
6             ########################################################################
7              
8             package Perl::Critic::Policy::Modules::PerlMinimumVersion;
9              
10 6     6   5817 use 5.006001;
  6         20  
  6         236  
11              
12 6     6   33 use strict;
  6         9  
  6         172  
13 6     6   30 use warnings;
  6         11  
  6         168  
14              
15 6     6   36 use English qw(-no_match_vars);
  6         10  
  6         46  
16 6     6   3169 use Readonly;
  6         13  
  6         282  
17              
18 6     6   33 use Perl::Critic::Utils qw{ :severities };
  6         9  
  6         341  
19              
20 6     6   767 use base 'Perl::Critic::Policy';
  6         11  
  6         2957  
21              
22             our $VERSION = '1.003';
23              
24             #---------------------------------------------------------------------------
25              
26             Readonly::Scalar my $DESC =>
27             'Avoid Perl features newer than specified version';
28             Readonly::Scalar my $EXPL => 'Improve your backward compatibility';
29              
30             #---------------------------------------------------------------------------
31              
32 2     2 1 238 sub default_severity { return $SEVERITY_LOWEST }
33 1     1 1 87 sub default_themes { return qw< more compatibility > }
34 3     3 1 10919 sub applies_to { return 'PPI::Document' }
35              
36             sub supported_parameters {
37             return (
38 5     5 0 16324 { name => 'version',
39             description => 'Version of perl to be compatible with.',
40             behavior => 'string',
41             parser => \&_parse_version,
42             },
43             );
44             }
45              
46             #---------------------------------------------------------------------------
47              
48             sub _parse_version {
49 5     5   3858 my ( $self, $parameter, $config_string ) = @_;
50              
51 5         29 my $version;
52 5 100       24 if ($config_string) {
    50          
53 4 100       25 if ( $config_string =~ m<\A \s* (5 [.] [\d.]+) \s* \z>xms ) {
54 3         9 $version = $1;
55             } else {
56 1         14 $self->throw_parameter_value_exception( 'version', $config_string,
57             undef, "doesn't look like a perl version number.\n",
58             );
59             }
60             } elsif ( ref $PERL_VERSION ) {
61 1         15 $version = $PERL_VERSION->numify(); # It's an object as of 5.10.
62             } else {
63 0         0 $version = 0 + $PERL_VERSION; # numify to get away from version.pm
64             }
65              
66 4         13 $self->{_version} = $version;
67              
68 4         14 return;
69             }
70              
71             #---------------------------------------------------------------------------
72              
73             sub violates {
74 3     3 1 31 my ( $self, $elem, $doc ) = @_;
75              
76 3 50       41 return if not eval { require Perl::MinimumVersion; 1; };
  3         30  
  3         17  
77              
78 3         21 my $checker = Perl::MinimumVersion->new($doc);
79              
80             # Workaround for Perl::Critic::Document instance in older P::C versions
81             # (pre-v0.22) that didn't have a custom isa() to masquerade as a
82             # PPI::Document
83 3 50       182 if ( !$checker ) {
84 0         0 $checker = Perl::MinimumVersion->new( $doc->{_doc} );
85             }
86 3 50       65 return if !$checker; # bail out!
87              
88             # this returns a version.pm instance
89 3         14 my $doc_version = $checker->minimum_version();
90              
91             #print "v$doc_version vs. $self->{_version}\n";
92 3 100       16081 return if $doc_version <= $self->{_version};
93              
94 1         16 return $self->violation( $DESC, $EXPL, $doc );
95             }
96              
97             1;
98              
99             __END__
100              
101             #---------------------------------------------------------------------------
102              
103             =pod
104              
105             =for stopwords
106              
107             =head1 NAME
108              
109             Perl::Critic::Policy::Modules::PerlMinimumVersion - Enforce backward compatible code.
110              
111             =head1 AFFILIATION
112              
113             This policy is part of L<Perl::Critic::More|Perl::Critic::More>, a bleeding
114             edge supplement to L<Perl::Critic|Perl::Critic>.
115              
116             =head1 DESCRIPTION
117              
118             As Perl evolves, new desirable features get added. The best ones seem to
119             break backward compatibility, unfortunately. This policy allows you to
120             specify a mandatory compatibility version for your code.
121              
122             For example, if you add the following to your F<.perlcriticrc> file:
123              
124             [Modules::PerlMinimumVersion]
125             version = 5.005
126              
127             then any code that employs C<our> will fail this policy, for example. By
128             default, this policy enforces the current Perl version, which is a pretty weak
129             statement.
130              
131             This policy relies on L<Perl::MinimumVersion|Perl::MinimumVersion> to do the
132             heavy lifting. If that module is not installed, then this policy always
133             passes.
134              
135             =head1 AUTHOR
136              
137             Chris Dolan <cdolan@cpan.org>
138              
139             =head1 COPYRIGHT
140              
141             Copyright (c) 2006-2008 Chris Dolan
142              
143             This program is free software; you can redistribute it and/or modify
144             it under the same terms as Perl itself. The full text of this license
145             can be found in the LICENSE file included with this module.
146              
147             =cut
148              
149             # Local Variables:
150             # mode: cperl
151             # cperl-indent-level: 4
152             # fill-column: 78
153             # indent-tabs-mode: nil
154             # c-indentation-style: bsd
155             # End:
156             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :