1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/Modules/RequireVersionVar.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::Policy::Modules::RequireVersionVar;
15 use List::MoreUtils qw(any);
17 use Perl::Critic::Utils qw{ :severities };
18 use base 'Perl::Critic::Policy';
20 our $VERSION = '1.088';
22 #-----------------------------------------------------------------------------
24 Readonly::Scalar my $DESC => q{No "VERSION" variable found};
25 Readonly::Scalar my $EXPL => [ 404 ];
27 #-----------------------------------------------------------------------------
29 sub supported_parameters { return () }
30 sub default_severity { return $SEVERITY_LOW }
31 sub default_themes { return qw(core pbp readability) }
32 sub applies_to { return 'PPI::Document' }
34 #-----------------------------------------------------------------------------
37 my ( $self, $elem, $doc ) = @_;
39 return if $doc->find_first( \&_is_VERSION_declaration );
41 #If we get here, then no $VERSION was found
42 return $self->violation( $DESC, $EXPL, $doc );
45 #-----------------------------------------------------------------------------
47 sub _is_VERSION_declaration { ##no critic(ArgUnpacking)
48 return 1 if _is_our_VERSION(@_);
49 return 1 if _is_vars_VERSION(@_);
50 return 1 if _is_package_VERSION(@_);
51 return 1 if _is_readonly_VERSION(@_);
55 #-----------------------------------------------------------------------------
58 my (undef, $elem) = @_;
59 $elem->isa('PPI::Statement::Variable') || return 0;
60 $elem->type() eq 'our' || return 0;
61 return any { $_ eq '$VERSION' } $elem->variables(); ## no critic
64 #-----------------------------------------------------------------------------
66 sub _is_vars_VERSION {
67 my (undef, $elem) = @_;
68 $elem->isa('PPI::Statement::Include') || return 0;
69 $elem->pragma() eq 'vars' || return 0;
70 return $elem =~ m{ \$VERSION }mx; #Crude, but usually works
73 #-----------------------------------------------------------------------------
75 sub _is_package_VERSION {
76 my (undef, $elem) = @_;
77 $elem->isa('PPI::Token::Symbol') || return 0;
78 return $elem =~ m{ \A \$ \S+ ::VERSION \z }mx;
79 #TODO: ensure that it is in _this_ package!
82 #-----------------------------------------------------------------------------
84 sub _is_readonly_VERSION {
86 #---------------------------------------------------------------
87 # Readonly VERSION statements usually come in one of two forms:
89 # Readonly our $VERSION = 1.0;
90 # Readonly::Scalar our $VERSION = 1.0;
91 #---------------------------------------------------------------
93 my (undef, $elem) = @_;
94 $elem->isa('PPI::Token::Symbol') || return 0;
95 return 0 if $elem !~ m{ \A \$VERSION \z }mx;
97 my $psib = $elem->sprevious_sibling() || return 0;
98 return 0 if $psib ne 'our';
100 my $ppsib = $psib->sprevious_sibling() || return 0;
101 return $ppsib eq 'Readonly' || $ppsib eq 'Readonly::Scalar';
108 #-----------------------------------------------------------------------------
114 Perl::Critic::Policy::Modules::RequireVersionVar - Give every module a C<$VERSION> number.
118 This Policy is part of the core L<Perl::Critic> distribution.
123 Every Perl file (modules, libraries, and programs) should have a
124 C<$VERSION> variable. The C<$VERSION> allows clients to insist on a
125 particular revision of your file like this:
127 use SomeModule 2.4; #Only loads version 2.4
129 This Policy scans your file for any package variable named
130 C<$VERSION>. I'm assuming that you are using C<strict>, so you'll
131 have to declare it like one of these:
133 our $VERSION = 1.0611;
134 $MyPackage::VERSION = 1.061;
135 use vars qw($VERSION);
137 A common practice is to use the C<$Revision: 2489 $> keyword to automatically
138 define the C<$VERSION> variable like this:
140 our ($VERSION) = '$Revision: 2489 $' =~ m{ \$Revision: \s+ (\S+) }x;
145 This Policy is not configurable except for the standard options.
150 Conway recommends using the C<version> pragma instead of raw numbers
151 or 'v-strings.' However, this Policy only insists that the
152 C<$VERSION> be defined somehow. I may try to extend this in the
157 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
161 Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer. All rights reserved.
163 This program is free software; you can redistribute it and/or modify
164 it under the same terms as Perl itself. The full text of this license
165 can be found in the LICENSE file included with this module.
171 # cperl-indent-level: 4
173 # indent-tabs-mode: nil
174 # c-indentation-style: bsd
176 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :