1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/Modules/RequireFilenameMatchesPackage.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::Policy::Modules::RequireFilenameMatchesPackage;
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{Package declaration must match filename};
25 Readonly::Scalar my $EXPL => q{Correct the filename or package statement};
27 #-----------------------------------------------------------------------------
29 sub supported_parameters { return () }
30 sub default_severity { return $SEVERITY_HIGHEST }
31 sub default_themes { return qw(core bugs) }
32 sub applies_to { return 'PPI::Document' }
34 #-----------------------------------------------------------------------------
37 my ($self, $elem, $doc) = @_;
39 my $filename = $doc->filename;
42 # 'lib/Foo/Bar.pm' -> ('lib', 'Foo', 'Bar')
43 my @path = File::Spec->splitpath($filename);
45 $filename =~ s/[.]\w+\z//mx;
46 my @path_parts = grep {$_ ne q{}} File::Spec->splitdir($path[1]), $filename;
48 # 'Foo::Bar' -> ('Foo', 'Bar')
49 my $pkg_node = $doc->find_first('PPI::Statement::Package');
51 my $pkg = $pkg_node->namespace;
52 return if $pkg eq 'main';
53 my @pkg_parts = split m/(?:\'|::)/mx, $pkg;
55 # To succeed, at least the lastmost must match
56 # Beyond that, the search terminates if a dirname is an impossible package name
58 while (@pkg_parts && @path_parts) {
59 my $pkg_part = pop @pkg_parts;
60 my $path_part = pop @path_parts;
61 if ($pkg_part eq $path_part) {
66 # if it's a path that's not a possible package (like 'Foo-Bar-1.00'), that's OK
67 last if ($path_part =~ m/\W/mx);
70 return $self->violation( $DESC, $EXPL, $pkg_node );
73 return if $matched_any;
74 return $self->violation( $DESC, $EXPL, $pkg_node );
79 #-----------------------------------------------------------------------------
87 Perl::Critic::Policy::Modules::RequireFilenameMatchesPackage - Package declaration must match filename.
91 This Policy is part of the core L<Perl::Critic> distribution.
96 The package declaration should always match the name of the file that
97 contains it. For example, C<package Foo::Bar;> should be in a file
103 This Policy is not configurable except for the standard options.
108 Chris Dolan <cdolan@cpan.org>
112 Copyright (C) 2006 Chris Dolan. All rights reserved.
114 This program is free software; you can redistribute it and/or modify
115 it under the same terms as Perl itself.
121 # cperl-indent-level: 4
123 # indent-tabs-mode: nil
124 # c-indentation-style: bsd
126 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :