icon Top 9 categories map      RocketAware > Perl >

Variable names

Tips: Browse or Search all pages for efficient awareness of Perl functions, operators, and FAQs.



Home

Search Perl pages


Subjects

By activity
Professions, Sciences, Humanities, Business, ...

User Interface
Text-based, GUI, Audio, Video, Keyboards, Mouse, Images,...

Text Strings
Conversions, tests, processing, manipulation,...

Math
Integer, Floating point, Matrix, Statistics, Boolean, ...

Processing
Algorithms, Memory, Process control, Debugging, ...

Stored Data
Data storage, Integrity, Encryption, Compression, ...

Communications
Networks, protocols, Interprocess, Remote, Client Server, ...

Hard World
Timing, Calendar and Clock, Audio, Video, Printer, Controls...

File System
Management, Filtering, File & Directory access, Viewers, ...

    

Variable names

Perl has three data structures: scalars, arrays of scalars, and associative arrays of scalars, known as ``hashes''. Normal arrays are indexed by number, starting with 0. (Negative subscripts count from the end.) Hash arrays are indexed by string.

Values are usually referred to by name (or through a named reference). The first character of the name tells you to what sort of data structure it refers. The rest of the name tells you the particular value to which it refers. Most often, it consists of a single identifier, that is, a string beginning with a letter or underscore, and containing letters, underscores, and digits. In some cases, it may be a chain of identifiers, separated by :: (or by ', but that's deprecated); all but the last are interpreted as names of packages, to locate the namespace in which to look up the final identifier (see Packages for details). It's possible to substitute for a simple identifier an expression which produces a reference to the value at runtime; this is described in more detail below, and in the perlref manpage.

There are also special variables whose names don't follow these rules, so that they don't accidentally collide with one of your normal variables. Strings which match parenthesized parts of a regular expression are saved under names containing only digits after the $ (see the perlop manpage and the perlre manpage). In addition, several special variables which provide windows into the inner working of Perl have names containing punctuation characters (see the perlvar manpage).

Scalar values are always named with '$', even when referring to a scalar that is part of an array. It works like the English word ``the''. Thus we have:

    $days               # the simple scalar value "days"
    $days[28]           # the 29th element of array @days
    $days{'Feb'}        # the 'Feb' value from hash %days
    $#days              # the last index of array @days

but entire arrays or array slices are denoted by '@', which works much like the word ``these'' or ``those'':

    @days               # ($days[0], $days[1],... $days[n])
    @days[3,4,5]        # same as @days[3..5]
    @days{'a','c'}      # same as ($days{'a'},$days{'c'})

and entire hashes are denoted by '%':

    %days               # (key1, val1, key2, val2 ...)

In addition, subroutines are named with an initial '&', though this is optional when it's otherwise unambiguous (just as ``do'' is often redundant in English). Symbol table entries can be named with an initial '*', but you don't really care about that yet.

Every variable type has its own namespace. You can, without fear of conflict, use the same name for a scalar variable, an array, or a hash (or, for that matter, a filehandle, a subroutine name, or a label). This means that $foo and @foo are two different variables. It also means that $foo[1] is a part of @foo, not a part of $foo. This may seem a bit weird, but that's okay, because it is weird.

Because variable and array references always start with '$', '@', or '%', the ``reserved'' words aren't in fact reserved with respect to variable names. (They ARE reserved with respect to labels and filehandles, however, which don't have an initial special character. You can't have a filehandle named ``log'', for instance. Hint: you could say open(LOG,'logfile') rather than open(log,'logfile'). Using uppercase filehandles also improves readability and protects you from conflict with future reserved words.) Case IS significant--``FOO'', ``Foo'', and ``foo'' are all different names. Names that start with a letter or underscore may also contain digits and underscores.

It is possible to replace such an alphanumeric name with an expression that returns a reference to an object of that type. For a description of this, see the perlref manpage.

Names that start with a digit may contain only more digits. Names which do not start with a letter, underscore, or digit are limited to one character, e.g., $% or $$. (Most of these one character names have a predefined significance to Perl. For instance, $$ is the current process id.)


Source: Perl data types
Copyright: Larry Wall, et al.
Next: Context

Previous: Floating-point Arithmetic



(Corrections, notes, and links courtesy of RocketAware.com)


[Overview Topics]

Up to: PERL




Rapid-Links: Search | About | Comments | Submit Path: RocketAware > Perl > perldata/Variable_names.htm
RocketAware.com is a service of Mib Software
Copyright 2000, Forrest J. Cavalier III. All Rights Reserved.
We welcome submissions and comments