Previous Page
Next Page

3.6. Capitalization

Distinguish different program components by case.

In a Perl program, an identifier might refer to a variable, a subroutine, a class or package name, an I/O stream, a format, or a typeglob. More importantly, sometimes the same identifier can refer to two or more of those in the same scope:

    # Print command line files, prefixing each line with the filename...
    if (@ARGV) {
        while (my $line = <ARGV>) {
            print "$ARGV: $line";

To help make it clear what kind of referent an identifier is naming:

  • Use lowercase only for the names of subroutines, methods, variables, and labeled arguments ($controller, new( ), src=>$fh).

  • Use mixed-case for package and class names (IO::Controller).

  • Use uppercase for constants ($SRC, $NODE)[*].

    [*] Yes, constants with sigils. See "Constants" in Chapter 4.

For example:

    my $controller
        = IO::Controller->new(src=>$fh,  mode=>$SRC|$NODE);

These case distinctions can then serve as useful clues to the purpose and role of each identifier, with visual differences reinforcing semantic differences. In contrast, it's much harder to distinguish between the variables, constants, methods, and classes in any of the following:

    my $controller
        = io::controller->new(src=>$fh,  mode=>$src|$node);

    my $Controller
        = Io::Controller->New(Src=>$Fh,  Mode=>$Src|$Node);


Of course, the approach suggested here is by no means the only possible set of conventions. But they are the same conventions (adapted for Perl's unique syntax) that are already applied in many languages and software libraries. In addition, they are already widely used throughout the Perl community, and therefore familiar to many programmers.

Note that the only exception to this guideline should be in identifiers that include a proper name, a standard abbreviation, or a unit of measurement. These should keep their familiar capitalizations, regardless of the construct they're used in. For example, write:

    my $expended_MJoules
        = $LaTeX_FUDGE_FACTOR * HTTP_transfer_rate() * $W3C::XHTML->entropy_factor( );


    my $expended_mjoules
        = $LATEX_FUDGE_FACTOR * http_transfer_rate() * $W3c::Xhtml->entropy_factor( );

    Previous Page
    Next Page