1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/Documentation/RequirePackageMatchesPodName.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::Policy::Documentation::RequirePackageMatchesPodName;
15 use Perl::Critic::Utils qw{ :severities :classification };
16 use base 'Perl::Critic::Policy';
18 our $VERSION = '1.088';
20 #-----------------------------------------------------------------------------
22 Readonly::Scalar my $PKG_RX => qr{ [[:alpha:]](?:[\w:\']*\w)? }mx;
23 Readonly::Scalar my $DESC => q{Pod NAME does not match the package declaration};
24 Readonly::Scalar my $EXPL => q{};
26 #-----------------------------------------------------------------------------
28 sub supported_parameters { return () }
29 sub default_severity { return $SEVERITY_LOWEST }
30 sub default_themes { return qw( core cosmetic ) }
31 sub applies_to { return 'PPI::Document' }
33 #-----------------------------------------------------------------------------
36 my ( $self, $elem, $doc ) = @_;
38 # No POD means no violation
39 my $pods_ref = $doc->find('PPI::Token::Pod');
42 for my $pod (@{$pods_ref}) {
43 my $content = $pod->content;
45 next if $content !~ m{^=head1 [ \t]+ NAME [ \t]*$ \s*}cgxms;
47 my ($pod_pkg) = $content =~ m{\G (\S+) }cgxms;
50 return $self->violation( $DESC, q{Empty name declaration}, $elem );
53 # idea: force NAME to match the file name in scripts?
54 return if is_script($doc); # mismatch is normal in program entry points
56 # idea: worry about POD escapes?
57 $pod_pkg =~ s{\A [CL]<(.*)>\z}{$1}gxms; # unwrap
58 $pod_pkg =~ s{\'}{::}gxms; # perl4 -> perl5
60 my $pkgs = $doc->find('PPI::Statement::Package');
61 # no package statement means no possible match
62 my $pkg = $pkgs ? $pkgs->[0]->namespace : q{};
63 $pkg =~ s{\'}{::}gxms;
65 return if $pkg eq $pod_pkg;
66 return $self->violation( $DESC, $EXPL, $pod );
68 return; # no NAME section found
75 #-----------------------------------------------------------------------------
81 Perl::Critic::Policy::Documentation::RequirePackageMatchesPodName - The C<=head1 NAME> section should match the package.
85 This Policy is part of the core L<Perl::Critic> distribution.
92 This Policy is not configurable except for the standard options.
96 Chris Dolan <cdolan@cpan.org>
100 Copyright (c) 2008 Chris Dolan
102 This program is free software; you can redistribute it and/or modify
103 it under the same terms as Perl itself. The full text of this license
104 can be found in the LICENSE file included with this module
110 # cperl-indent-level: 4
112 # indent-tabs-mode: nil
113 # c-indentation-style: bsd
115 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :