Perl Variable Concepts
A variable is like a pigeon hole for data.
Listing 1 is a short Perl program that shows how a simple program may look.
It's where the data lives when it's not busy being data.
Most programming languages have different types of variables for different types of data. But, Perl is a little different in this respect.
In Perl, all data types are based on one simple type called a scalar. Scalar data can be either text or numeric and sometimes both.
This simplification makes its possible for you to write your programs without worrying about what data type a particular variable is.
More complex data is built by aggregating scalars into other structures like arrays and hashes.
These aggregations are convenient at times, and allow you to write more complex programs without writing more complex programs.
LISTING 1: Hello, World!
# this is a comment
print "Hello, World!\n"; # so is this
Scalar variables have a leading $ for example, these are all valid Perl scalar variables:
Case matters in Perl, so the first two examples are different variables. The final variable ''$'' is special,
one of many that the Perl interpreter will use by default in various operations if the programmer does not indicate otherwise.
Any particular valid identifier can be used to designate a scalar, an array, and a hash.
The leading character
determines which of the three types the variable has. For example, here the identifier name is used to denote three
$name @name %name
All three can be used concurrently, as they denote different storage areas. A variable of the type scalar can contain any scalar value:
- The first is a scalar;
- the second is an array;
- the last is a hash.
$v1 = 'good morning';
$v1 = 127;
The first assignment places a string value in the variable. The second replaces the string with an integer value.
This is different from many strongly typed languages (such as C++ and Java), where types are finely divided
into categories such as integer, real, string, and boolean. In Perl these are values, but not types; Perl uses type distinction
mainly to separate singular entities (scalar) from collective entities (arrays and hashes).
String values are delimited with either single or double quotes. Single-quoted literals are used exactly as written,
whereas double-quoted literals are subject to escape character and variable interpolation before the final value is obtained. For example:
$numer = 2;
$st1 = 'one fine $numer day';
$st2 = "$numer fine day \n";
The output from this script is
2 fine day
one fine $numer day
Four interpolations have taken place. In the second line, the $numer appears to be a use of a variable, but
because the string is in single quotes the characters are included as they appear. In the third line the string literal
is in double quotes, so the variable name is replaced with its current value (2) to produce the final string value;
the escape sequence ''\n" also puts in a newline character.
The first print statement shows the result. The second print shows two interpolations as well, as the string being printed is in double quotes.
The value in $st1 is interpolated into the output string and then a newline is added to the end. Even though $st1 has ''$numer" in its value,
this is not recursively interpolated when those characters are put into the output string.