NAME

Perl::Critic::Policy::NamingConventions::Capitalization - Distinguish different program components by case.

AFFILIATION

This Policy is part of the core Perl::Critic distribution.

DESCRIPTION

Conway recommends to distinguish different program components by case.

Normal subroutines, methods and variables are all in lower case.

my $foo;            # ok
my $foo_bar;        # ok
sub foo {}          # ok
sub foo_bar {}      # ok

my $Foo;            # not ok
my $foo_Bar;        # not ok
sub Foo     {}      # not ok
sub foo_Bar {}      # not ok

Package and class names are capitalized.

package IO::Thing;     # ok
package Web::FooBar    # ok

package foo;           # not ok
package foo::Bar;      # not ok

Constants are in all-caps.

Readonly::Scalar my $FOO = 42;  # ok

Readonly::Scalar my $foo = 42;  # not ok

There are other opinions on the specifics, for example, in perlstyle. This policy can be configured to match almost any style that you can think of.

CONFIGURATION

You can specify capitalization rules for the following things: packages, subroutines, local_lexical_variables, scoped_lexical_variables, file_lexical_variables, global_variables, constants, and labels.

constants are things declared via constant or Readonly.

use constant FOO => 193;
Readonly::Array my @BAR => qw< a b c >;

global_variables are anything declared using local, our, or vars. file_lexical_variables are variables declared at the file scope.

scoped_lexical_variables are variables declared inside bare blocks that are outside of any subroutines or other control structures; these are usually created to limit scope of variables to a given subset of subroutines. E.g.

sub foo { ... }

{
    my $thingy;

    sub bar { ... $thingy ... }
    sub baz { ... $thingy ... }
}

All other variable declarations are considered local_lexical_variables.

Each of the packages, subroutines, local_lexical_variables, scoped_lexical_variables, file_lexical_variables, global_variables, constants, and labels options can be specified as one of :single_case, :all_lower, :all_upper:, :starts_with_lower, :starts_with_upper, or :no_restriction or a regular expression; any value that does not start with a colon, :, is considered to be a regular expression. The :single_case tag means a name can be all lower case or all upper case. If a regular expression is specified, it is surrounded by \A and \z.

packages defaults to :starts_with_upper. subroutines, local_lexical_variables, scoped_lexical_variables, file_lexical_variables, and global_variables default to :single_case. And constants and labels default to :all_upper.

There are corresponding package_exemptions, subroutine_exemptions, local_lexical_variable_exemptions, scoped_lexical_variable_exemptions, file_lexical_variable_exemptions, global_variable_exemptions, constant_exemptions, and label_exemptions options that are lists of regular expressions to exempt from the corresponding capitalization rule. These values also end up being surrounded by \A and \z.

package_exemptions defaults to main. global_variable_exemptions defaults to \$VERSION @ISA @EXPORT(?:_OK)? %EXPORT_TAGS \$AUTOLOAD %ENV %SIG \$TODO. subroutine_exemptions defaults to AUTOLOAD BUILD BUILDARGS CLEAR CLOSE DELETE DEMOLISH DESTROY EXISTS EXTEND FETCH FETCHSIZE FIRSTKEY GETC NEXTKEY POP PRINT PRINTF PUSH READ READLINE SCALAR SHIFT SPLICE STORE STORESIZE TIEARRAY TIEHANDLE TIEHASH TIESCALAR UNSHIFT UNTIE WRITE which should cover all the standard Perl subroutines plus those from Moose.

For example, if you want all local variables to be in all lower-case and global variables to start with "G_" and otherwise not contain underscores, but exempt any variable with a name that contains "THINGY", you could put the following in your .perlcriticrc:

[NamingConventions::Capitalization]
local_lexical_variables = :all_lower
global_variables = G_(?:(?!_)\w)+
global_variable_exemptions = .*THINGY.*

TODO

Handle use vars. Treat constant subroutines like constant variables. Handle bareword file handles. There needs to be "schemes" or ways of specifying "perlstyle" or "pbp". Differentiate lexical Readonly constants in scopes.

BUGS

This policy won't catch problems with the declaration of $y below:

for (my $x = 3, my $y = 5; $x < 57; $x += 3) {
    ...
}

AUTHOR

Multiple people

COPYRIGHT

Copyright (c) 2008-2023 Michael G Schwern.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The full text of this license can be found in the LICENSE file included with this module.