2.1.1 Literals
The simplest kind of expression is a literal. It is a fixed and manifest or explicit value of some kind. In other words, it is an instance of a data value that appears in a program. Since Perl has scalars, lists and hashes as primitive data types, literals can be of any of these types. We start with scalar data types because they are simple. Perl allows numbers, and strings as scalars. Perl does not distinguish between a number and a string and uses them interchangeably, coercing one into the other as need arises. Lists and hashes are discussed in Chapter 3.
A numeric literal in Perl can be an integer, a floating point number in decimal or scientific notation, a number in hexadecimal or octal notation. Examples of numeric literals are given in Table 2.1. The table shows that numeric literals can have a sign in front of them. Also, an underscore can be inserted to improve readability. Perl also accepts numeric values in binary, octal and hexadecimal bases. A binary literal must have 0b in front. The b indicates it is binary. A binary number can have only 0s and 1s in it. An octal literal starts with 0 and contains digits between 0 and 7. A hexadecimal number has 0x in front of it and contains digits in the range 0 through 9, a,b,c,d,e and f.
Table 2.1: Numeric Literals in Perl
String literals can be singly-quoted or doubly-quoted. A string can have characters that are printable but that cannot be transcribed in a straight-forward manner. Such a character cannot be written as one single viewable character. A sequence of two or more characters is sometimes used to represent just one printable character like a linebreak or a tab space. The linebreak is represented as \n and a tab space as \t. These are called escape sequences. Singly-quoted string literals allow the use of only one back-slashed character or so-called escape sequence. It is \n. Thus,
'It is bright and sunny.\nMuch better than yesterday!'
is a singly-quoted string literal in Perl that contains a linebreak after the first sentence. Doubly quoted strings allow the use of \n as well as other escape sequences such as \t for a tab space. An example of a doubly-quoted string literal is the following.
"Name\tRank\nJ. Kalita\tAssociate Professor\n"
This doubly-quoted string contains two newline characters and two tab space characters. There is another major difference between a singly-quoted string and a doubly-quoted string. A singly-quoted string does not interpolate variables whereas a doubly-quoted string does. This means that if we have a scalar variable name or string variable name inside a doubly quoted string, the name is replaced by the value of the variable.
Assume that we have a scalar variable $friend with the value "Justin O'Malley", and have the following two strings—one singly-quoted and the other doubly-quoted.
'$friend lives in Colorado Springs\n' "$friend lives in Colorado Springs\n"
If we now print the two string using Perl’s print built-in function, we see the following, respectively.
$friend lives in Colorado Springs Justin O'Malley lives in Colorado Springs
When the singly-quoted string is printed, $friend is printed literally. However, when we print the doubly-quoted string, $friend is replaced by its value Justin O'Malley. This is called variable interpolation inside a string. List variable names are interpolated as well inside a doubly-quoted string. However, hash variable names are not interpolated.
