A variable is a name for a container that holds one or more values. [1] The name of the variable is constant throughout the program, but the value or values contained in that variable typically change over and over again throughout the execution of the program.
A scalar variable holds a single scalar value (representing a number, a string, or a reference). Scalar variable names begin with a dollar sign followed by what we'll call a Perl identifier: a letter or underscore, and then possibly more letters, or digits, or underscores. Another way to think of it is that it's made up of alphanumerics and underscores, but can't start with a digit. Upper- and lowercase letters are distinct: the variable $A is a different variable from $a. And all of the letters, digits, and underscores are significant, so:
$a_very_long_variable_that_ends_in_1is different from:
$a_very_long_variable_that_ends_in_2
Scalar variables in Perl are always referenced with the leading $. In the UNIX shell, you use $ to get the value, but leave the $ off to assign a new value. In awk or C, you leave the $ off entirely. If you bounce back and forth a lot, you'll find yourself typing the wrong things occassionally. This is expected. (Most Perl programmers would recommend that you stop writing shell, awk, and C programs, but that may not work for you.)
You should generally select variable names that mean something regarding the purpose of the variable. For example, $r is probably not very descriptive but $line_length is. A variable used for only two or three lines close together may be called something simple, like $n, but a variable used throughout a program should probably have a more descriptive name.
Similarly, properly placed underscores can make a name easier to read and understand, especially if your maintenance programmer has a different spoken language background than you have. For example, $super_bowl is a better name than $superbowl, since that last one might look like $superb_owl. Does $stopid mean $sto_pid (storing a process-ID of some kind?) or $s_to_pid (converting something to a process-ID?) or $stop_id (the ID for some kind of "stop" object?) or is it just a stopid mispelling?
Most variable names in our Perl programs are all lowercase, like most of the ones we'll see in these lessons. In a few special cases, capitalization is used. Using all-caps (like $ARGV) generally means that there's something special about that variable. (But you can get into an all-out brawl if you choose sides on the $underscores_are_cool versus the $giveMeInitialCaps argument. So be careful.)
Of course, choosing good or poor names makes no difference to Perl. You could name your program's three most-important variables $OOO000OOO, $OO00OO00, and $O0O0O0O0O and Perl wouldn't be bothered - but in that case, please, don't ask us to maintain your code.
Next: Interpolation of Scalar Variables into Strings
[1]
As we'll see, a scalar variable can hold only one value. But other types of
variables, such as arrays and hashes, may hold many values.
Return to the page from whence you came