Login
Fixing errors in POD that were exposed in the last
[gknop/Perl-Critic.git] / lib / Perl / Critic / Policy / NamingConventions / Capitalization.pm
CommitLineData
04e1ddd9
JRT
1##############################################################################
2# $URL$
3# $Date$
4# $Author$
5# $Revision$
6##############################################################################
7
8package Perl::Critic::Policy::NamingConventions::Capitalization;
9
10use 5.006001;
11use strict;
12use warnings;
13
14use Readonly;
15
16use Perl::Critic::Utils qw{ :severities };
17
18use base 'Perl::Critic::Policy';
19
20our $VERSION = '1.091';
21
22#-----------------------------------------------------------------------------
23
24Readonly::Scalar my $LOWER_RX => qr/ [[:lower:]] /xms;
25Readonly::Scalar my $UPPER_RX => qr/ [[:upper:]] /xms;
26Readonly::Scalar my $PACKAGE_RX => qr/ :: /xms;
27Readonly::Scalar my $DESC => 'Capitalization';
28Readonly::Scalar my $EXPL => [ 45 ];
29
30#-----------------------------------------------------------------------------
31
32sub supported_parameters { return () }
33sub default_severity { return $SEVERITY_LOWEST }
34sub default_themes { return qw( core pbp cosmetic ) }
35
36sub applies_to {
37 return 'PPI::Statement::Variable',
38 'PPI::Statement::Package',
39 'PPI::Statement::Sub';
40}
41
42#-----------------------------------------------------------------------------
43
44sub violates {
45 my ( $self, $elem, undef ) = @_;
46
47 my $violates
48 = $elem->isa("PPI::Statement::Variable") ? _variable_capitalization($elem)
49 : $elem->isa("PPI::Statement::Package") ? _package_capitalization($elem)
50 : $elem->isa("PPI::Statement::Sub") ? _sub_capitalization($elem)
51 : die "Should never reach this point"
52 ;
53
54 return $self->violation( $DESC, $EXPL, $elem ) if $violates;
55 return;
56}
57
58sub _variable_capitalization {
59 my $elem = shift;
60
61 for my $name ( $elem->variables() ) {
62 # Fully qualified names are exempt because we can't be responsible for
63 # other people's sybols.
64 next if $elem->type() eq 'local' && $name =~ m/$PACKAGE_RX/xms;
65
66 # Allow CONSTANTS
67 next if $name !~ $LOWER_RX;
68
69 # Words in variable names cannot be capitalized unless
70 # camelCase is in use
71 return 1 if $name =~ m{ [^[:alpha:]] $UPPER_RX }xmso;
72 }
73
74 return;
75}
76
77sub _package_capitalization {
78 my $elem = shift;
79 my @names = split /::/, $elem->namespace;
80
81 for my $name (@names) {
82 # Each word should be capitalized.
83 return 1 unless $name =~ m{ ^ $UPPER_RX }xmso;
84 }
85
86 return;
87}
88
89sub _sub_capitalization {
90 my $elem = shift;
91 my $name = $elem->name;
92
93 # Words in subroutine names cannot be capitalized
94 # unless camelCase is in use.
95 return 1 if $name =~ m{
96 (?: ^ | [^[:alpha:]] )
97 $UPPER_RX
98 }xmso;
99 return;
100}
101
1021;
103
104__END__
105
106#-----------------------------------------------------------------------------
107
108=head1 NAME
109
110Perl::Critic::Policy::NamingConventions::Capitalization - Distinguish different program components by case.
111
112
113=head1 AFFILIATION
114
115This Policy is part of the core L<Perl::Critic|Perl::Critic> distribution.
116
117=head1 DESCRIPTION
118
119Conway recommends to distinguish different program components by case.
120
121Normal subroutines, methods and variables are all in lower case.
122
123 my $foo; # ok
124 my $foo_bar; # ok
125 sub foo {} # ok
126 sub foo_bar {} # ok
127
128 my $Foo; # not ok
129 my $foo_Bar; # not ok
130 sub Foo {} # not ok
131 sub foo_Bar {} # not ok
132
133Package and class names are capitalized.
134
135 package IO::Thing; # ok
136 package Web::FooBar # ok
137
138 package foo; # not ok
139 package foo::Bar; # not ok
140
141Constants are in all-caps.
142
143 Readonly::Scalar my $FOO = 42; # ok
144
145 Readonly::Scalar my $foo = 42; # not ok
146
147=head1 CONFIGURATION
148
149This Policy is not configurable except for the standard options.
150
151
152=head1 BUGS
153
154The policy cannot currently tell that a variable is being declared as
155a constant, thus any variable may be made all-caps.
156
157
158=head1 SEE ALSO
159
160To control use of camelCase see L<Perl::Critic::Policy::NamingConventions::ProhibitMixedCaseSubs|Perl::Critic::Policy::NamingConventions::ProhibitMixedCaseSubs> and L<Perl::Critic::Policy::NamingConventions::ProhibitMixedCaseVars|Perl::Critic::Policy::NamingConventions::ProhibitMixedCaseVars>.
161
162=cut
163
164# Local Variables:
165# mode: cperl
166# cperl-indent-level: 4
167# fill-column: 78
168# indent-tabs-mode: nil
169# c-indentation-style: bsd
170# End:
171# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :