A literal is viewed as a constant data. It has the following data types.
Let's discuss each of them one by one.
For those of you who are not familiar with non-decimal numbering systems, here is a short explanation. In decimal notation-or base ten- when you see the value 15 it signifies (1 * 10) + 5 or 1510. The subscript indicates which base is being used.
In octal notation-or base eight-when you see the value 15 it signifies (1 * 8) + 5 or 1310.
In hexadecimal notation-or base 16-when you see the value 15 it signifies (1 * 16) + 5 or 2110. Base 16 needs an 6 characters in addition to 0 to 9 so that each position can have a total of 16 values. The letters A-F are used to represent 11-16. So the value BD16 is equal to (B16 * 16) * D16 or (1110 * 16) + 1310 which is 17610.
If you will be using very large or very small numbers, you might also find scientific notation to be of use. Scientific notation looks like 10.23E+4, which is equivalent to 1,023,000. You can also represent small numbers if you use a negative sign. For example, 10.23E-4 is .001023. Simply move the decimal point to the right if the exponent is positive and to the left if the exponent is negative.
First, here are some integers.
123 043 0x23Now, some numbers and fractions-also called floating point values. You will frequently see these values referred to as a float value. A float with a value in the tenths place. You can also say 100 and 5/10. A float with a fraction value out to the thousandths place. You can also say 54 and 534/1000.
100.5 54.534Here's a very small number.
.000034
'Gene Bank' 'Searching Result'Strings are pretty simple. What if you wanted to use a single quote inside the literal? If you did this, Perl would think you wanted to end the string early and a compiler error would result. Perl uses the backslash (\) character to indicate that the normal function of the single quote-ending a literal-should be ignored for a moment. The backslash character is also called an escape character. For example,
'WasWaldo can\'t hit the broad side of a barn.' 'Morganna said, "WasWaldo can\'t hit anything."'The single-quotes are used here specifically so that the double-quotes can be used to surround the spoken words. Later in the section on double-quoted literals, you'll see that the single-quotes can be replaced by double-quotes if you'd like.
You must know only one more thing about single-quoted strings. You can add a line break to a single-quoted string simply by adding the line break to your source code-as demonstrated by Listing 2.1.
print 'Bill of Goods
Bread: $34.45
Fruit: $45.00
======
$79.45';
You can see that with single-quoted literals, even the line breaks in your source code are part of the string.
Fo example, the following literal is similar to one you've already seen. Just the quotes are different.
Another literal that uses double quotes inside a double-quoted string.
"WasWaldo the Illusionist" "Morganna said, \"WasWaldo can't hit anything.\""Notice how the backslash in the second line is used to escape the double quote characters. And the single quote can be used without a backslash.
One major difference between double- and single-quoted strings is that double-quoted strings have some special escape sequences that can be used. Escape sequences represent characters that are not easily entered using the keyboard or that are difficult to see inside an editor window. Table 2.1 shows all of the escape sequences that Perl understands. The examples following the table will illustrate some of them.
| Table 2.1 - Escape Sequences | ||||||||||||||||||||||||||||||||||||||||||||
|
In the next chapter, you'll see why you might need to use a backslash when using the $, @, and % characters.
For example, the \u is used twice in the first word to capitalize the w characters. And the hexadecimal notation is used to represent the age using the ASCII codes for 3 and 4.
This literal represents the following: The kettle was HOT!. The \U capitalizes all characters until a \E sequence is seen.
"\uwas\uwaldo is \x33\x34 years old." "The kettle was \Uhot\E!"Actually, this example isn't too difficult, but it does involve looking at more than one literal at once and it's been a few pages since our last advanced example. Let's look at the \t and \n escape sequences. Listing 2.2-a program displaying a bill with several items.
The previous examples illustrate a cardinal rule of Perl-there's always more than one way to do something. This program uses two methods to cause a line break.
print "Bill of Goods Bread:\t\$34.45\n"; print "Fruit:\t" print "\$45.00\n"; print "\t======\n"; print "\t\$79.45\n";
I recommend using the \n character so that when looking at your code in the future, you can be assured that you meant to cause a line break and did not simply press the ENTER key by mistake.
Caution
Let's see how to use the back-quoted string to display a directory listing of all text files in the perl5 directory.
print `dir *.txt`;All of the escape sequences used with double-quoted strings can be used with back-quoted strings.
This example shows an empty array, an array of numbers and an array of strings.
print "Here is an empty array:" . () . "<-- Nothing there!\n";
print (12, ' ', 014, ' ', 0x0c, ' ', 34.34, ' ', 23.3E-3);
print "\n";
print ("This ", "is ", 'an ', "array ", 'of ', "strings.");
print "\n";
print ("This ", 30, "is ", 'a', "mixed array ", 'of ', 0x08, " items.");
The fourth line of this listing shows that you can mix single- and double-quoted strings in the same array. You can also mix numbers and strings interchangeably, as shown in the last line.
Listing 2.3 uses the period, or concatenation, operator to join a string representation of the empty array with the string " Here is an empty array:" and the string "<-- Nothing there!\n". You can read more about operators in Chapter 4, "Operators."
In this and other examples in this chapters, the elements of an array will be printed with no spaces between them. You will see how to print with spaces in the section "Strings Revisited" in Chapter 4 .
It gives you the idea behind specifying an array by using sub-arrays.
print (("Bright Orange", "Burnt"), ("Canary Yellow", "Sunbeam"));
print (("Bright Orange", "Burnt"), " Middle ", ("Canary Yellow", "Sunbeam"));
So far, we haven't talked about the internal representations of data types.
That's because you almost never have to worry about such things with Perl.
However, it is important to know that, internally, the sub-arrays are merged
into the main array. In other words, the array:(("Bright Orange", "Burnt"), ("Canary Yellow", "Sunbeam"))
is exactly equivalent to
("Bright Orange", "Burnt", "Canary Yellow", "Sunbeam")
Perl uses two periods (..) to replace a consecutive series of values. Not only is this method quicker to type-and less prone to error-it is easier to understand. Only the end points of the series are specified; you don't need to manually verify that every value is represented. If the .. is used, then automatically you know that a range of values will be used.
print (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15); print "\n"; print (1..15);The two arrays used in the previous example are identical, but they were specified differently.
The double periods in the array specification are called the range operator. The range operator is also discussed in Chapter 4.
You can also use the shorthand method to specify values in the middle of an array.
print (1, 2, 7..10, 14, 15);
print "\n"
print ("A", "B", "F".."H", "Y", "Z");
The range operator works by taking the lefthand value, adding one to it,
then appending that new value to the array. Perl continues to do this until
the new value reaches the righthand value. You can use letters with the range
operator because the ASCII table uses consecutive values to represent consecutive letters.
print `dir /*.log`;