1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/TestingAndDebugging/RequireUseStrict.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::Policy::TestingAndDebugging::RequireUseStrict;
15 use Perl::Critic::Utils qw{ :severities };
16 use base 'Perl::Critic::Policy';
18 our $VERSION = '1.088';
20 #-----------------------------------------------------------------------------
22 Readonly::Scalar my $DESC => q{Code before strictures are enabled};
23 Readonly::Scalar my $EXPL => [ 429 ];
25 #-----------------------------------------------------------------------------
27 sub supported_parameters { return () }
28 sub default_severity { return $SEVERITY_HIGHEST }
29 sub default_themes { return qw( core pbp bugs ) }
30 sub applies_to { return 'PPI::Document' }
32 sub default_maximum_violations_per_document { return 1; }
34 #-----------------------------------------------------------------------------
37 my ( $self, undef, $doc ) = @_;
39 # Find the first 'use strict' statement
40 my $strict_stmnt = $doc->find_first( \&_is_use_strict );
41 my $strict_line = $strict_stmnt ? $strict_stmnt->location()->[0] : undef;
43 # Find all statements that aren't 'use', 'require', or 'package'
44 my $stmnts_ref = $doc->find( \&_isnt_include_or_package );
45 return if !$stmnts_ref;
47 # If the 'use strict' statement is not defined, or the other
48 # statement appears before the 'use strict', then it violates.
51 for my $stmnt ( @{ $stmnts_ref } ) {
52 last if $stmnt->isa('PPI::Statement::End');
53 last if $stmnt->isa('PPI::Statement::Data');
55 my $stmnt_line = $stmnt->location()->[0];
56 if ( (! defined $strict_line) || ($stmnt_line < $strict_line) ) {
57 push @viols, $self->violation( $DESC, $EXPL, $stmnt );
64 my (undef, $elem) = @_;
66 return 0 if !$elem->isa('PPI::Statement::Include');
67 return 0 if $elem->type() ne 'use';
70 $elem->pragma() ne 'strict'
71 and $elem->module() ne 'Moose'
72 and $elem->module() ne 'Moose::Role'
80 sub _isnt_include_or_package {
81 my (undef, $elem) = @_;
83 return 0 if ! $elem->isa('PPI::Statement');
84 return 0 if $elem->isa('PPI::Statement::Package');
85 return 0 if $elem->isa('PPI::Statement::Include');
93 #-----------------------------------------------------------------------------
99 Perl::Critic::Policy::TestingAndDebugging::RequireUseStrict - Always C<use strict>.
104 This Policy is part of the core L<Perl::Critic> distribution.
109 Using strictures is probably the single most effective way to improve the
110 quality of your code. This policy requires that the C<'use strict'> statement
111 must come before any other statements except C<package>, C<require>, and other
112 C<use> statements. Thus, all the code in the entire package will be affected.
114 There are special exemptions for L<Moose> and L<Moose::Role> because they
115 enforces strictness; e.g. C<'use Moose'> is treated as equivalent to
118 The maximum number of violations per document for this policy defaults to 1.
124 This Policy is not configurable except for the standard options.
129 L<Perl::Critic::Policy::TestingAndDebugging::ProhibitNoStrict>
134 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
139 Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer. All rights reserved.
141 This program is free software; you can redistribute it and/or modify it under
142 the same terms as Perl itself. The full text of this license can be found in
143 the LICENSE file included with this module
147 ##############################################################################
150 # cperl-indent-level: 4
152 # indent-tabs-mode: nil
153 # c-indentation-style: bsd
155 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :