Search⌘ K

Identifiers

Explore Perl identifiers and variables to understand how names are formed and used in Perl programs. Learn about the role of sigils, variant sigils, context in variable usage, and the significance of namespaces in organizing code logically and efficiently.

Names

Names (or identifiers) are everywhere in Perl programs. They exist primarily for our benefit as programmers; we choose them for variables, functions, packages, classes, and even filehandles. All valid Perl names begin with a letter or an underscore and may optionally include any combination of letters, numbers, and underscores. When the utf8 pragma is in effect, we may use any UTF-8UTF-8 is a commonly used variable-length encoding of unicode characters. It uses only one byte to represent ASCII characters. word characters in identifiers.

These are valid Perl identifiers:

Perl
my $name;
my @_private_names;
my %Names_to_Addresses;
sub anAwkwardName3;

This works with utf8 pragma enabled:

Perl
use utf8;
package Ingy::Döt::Net;

These are invalid Perl identifiers:

Perl
my $invalid name; # space is invalid
my @3; # cannot start with number my
%~flags; # symbols invalid in name
package a-lisp-style-name; # use underscore instead

Note: Names exist primarily for our benefit as a programmer.

These rules apply only to literal names that appear in our source code, such as sub fetch_pie or my $waffleiron.

Only Perl’s parser enforces the rules about identifier names. We may also refer to entities with names generated at runtime or provided as ...