Numbers
All Numbers Have the Same Format Internally
Internally, Perl computes with double-precision floating- point values. This means that there are no integer values internal to Perl—an integer constant in the program is treated as the equivalent floating-point value. Examples:
255
61298040283768
That last one is a little hard to read. Perl allows underscores for clarity within integer literals, so you can also write that number like this: 61_298_040_283_768
Nondecimal Integer Literals
Perl allows you to specify numbers in other than base 10 (decimal). Octal (base 8) literals start with a leading 0, hexadecimal (base 16) literals start with a leading 0x, and binary (base 2) literals start with a leading 0b. The hex digits A through F (or a through f) represent the conventional digit values of 10 through 15. Examples:
0377 # 377 octal, same as 255 decimal
0xff # FF hex, also 255 decimal
0b11111111 # also 255 decimal
Numeric Operators
Perl also supports a modulus operator (%). The value of the expression 10 % 3 is the remainder when 10 is divided by 3, which is one. Both values are first reduced to their integer values, so 10.5 % 3.2 is computed as 10 % 3. Additionally, Perl provides the FORTRAN-like exponentiation operator, which many have yearned for in Pascal and C. The operator is represented by the double asterisk, such as 2**3, which is two to the third power, or eight.
Strings
Single-Quoted String Literals
- Any character other than a single quote or a backslash between the quote marks (including newline characters, if the string contin- ues onto successive lines) stands for itself inside a string. To get a backslash, put two backslashes in a row, and to get a single quote, put a backslash followed by a single quote. Examples:
'fred' # those four characters: f, r, e, and d
'' # the null string (no characters)
'the last character is a backslash: \\'
'hello\n' # hello followed by backslash followed by n
'\'\\' # single quote followed by backslash
Note that the \n within a single-quoted string is not interpreted as a newline, but as the two characters backslash and n. Only when the backslash is followed by another backslash or a single quote does it have special meaning.
Double-Quoted String Literals
- But now the backslash takes on its full power to specify certain control characters, or even any character at all through octal and hex representations.
"fred" # just the same as 'fred'
"hello world\n" # hello world, and a newline
"The last character of this string is a quote mark: \""
"hello\tworld" # hello, a tab, and world
String Operators
- String values can be concatenated with the
.
operator. (Yes, that』s a single period.) This does not alter either string, any more than 2+3 alters either 2 or 3. The resulting (longer) string is then available for further computation or assignment to a variable.
"hello" . "world" # same as "helloworld"
"hello" . ' ' . "world" # same as 'hello world'
'hello world' . "\n" # same as "hello world\n"
- A special string operator is the string repetition operator, consisting of the single lowercase letter
x
. This operator takes its left operand (a string) and makes as many concatenated copies of that string as indicated by its right operand (a number). Examples:
"fred" x 3 # is "fredfredfred"
"hello" x (1+1) # is "hello" × 2, or ""hellohello"
5 × 4 # is really "5" × 4, which is "5555"
4 x 5 # is really "4" x 5, which is "44444"
The copy count (the right operand) is first truncated to an integer value (4.8 becomes 4) before being used. A copy count of less than one results in an empty (zero-length) string.
Automatic Conversion Between Numbers and Strings
- For the most part, Perl automatically converts between numbers and strings as needed. Examples:
"12" * "3" # result is 36
"12fred34" * "3" # result is 36, trailing nonnumber stuff and leading whitespace are discarded
"Z" . 5 * 7 # same as "Z" . 35, or "Z35"
Scalar Variables
A scalar variable holds a single scalar value, as you』d expect. 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 $Fred
is a different variable from $fred
.
Most variable names in our Perl programs are all lowercase
, like most of the ones you』ll see in this book. In a few special cases, uppercase letters are used. Using all caps (like $ARGV) generally indicates that there』s something special about that variable.
Output with print
Examples:
print "hello world\n"; # say hello world, followed by a newline
print "The answer is ";
print 6 * 7;
print ".\n";
You can give print a series of values, separated by commas:
print "The answer is ", 6 * 7, ".\n";
Interpolation of Scalar Variables into Strings
When a string literal is double-quoted, it is subject to variable interpolation(besides being checked for backslash escapes). This means that any scalar variable name in the string is replaced with its current value. For example:
$meal = "brontosaurus steak";
$barney = "fred ate a $meal"; # $barney is now "fred ate a brontosaurus steak"
$barney = 'fred ate a ' . $meal; # another way to write that
Don』t bother with interpolating if you have just the one lone variable:
print "$fred"; # unneeded quote marks print
$fred; # better style
What if you want to follow the replaced value immediately with some constant text that begins with a letter, digit, or underscore?
$what = "brontosaurus steak";
$n = 3;
print "fred ate $n $whats.\n"; # not the steaks, but the value of $whats
print "fred ate $n ${what}s.\n"; # now uses $what
print "fred ate $n $what" . "s.\n"; # another way to do it
print 'fred ate ' . $n . ' ' . $what . "s.\n"; # an especially difficult way
Comparison Operators
For comparing strings, Perl has an equivalent set of string comparison operators, which look like funny little words: lt le eq ge gt ne.
others
Boolean Values
But how does Perl decide whether a given value is true or false? Perl doesn』t have a separate Boolean data type, like some languages have. Instead, it uses a few simple rules:
- If the value is a number, 0 means false; all other numbers mean true.
- Otherwise,if the value is a string,the empty string('') means false;all other strings mean true.
- Otherwise(that is,if the value is another kind of scalar than a number or a string), convert it to a number or a string and try again.
There』s one trick hidden in those rules. Because the string '0' is the exact same scalar value as the number 0, Perl has to treat them both the same. That means that the string '0' is the only nonempty string that is false.
Getting User Input
$line = <STDIN>;
if ($line eq "\n") {
print "That was just a blank line!\n";
} else {
print "That line of input was: $line";
}
The chomp Operator
chomp($text = <STDIN>); # Read the text, without the newline character
$text = <STDIN>; # Do the same thing...
chomp($text); # ...but in two steps
chomp is actually a function. As a function, it has a return value, which is the number of characters removed. This number is hardly ever useful:
$food = <STDIN>;
$betty = chomp $food; # gets the value 1 - but you knew that!
As you see, you may write chomp with or without the parentheses. This is another general rule in Perl: except in cases where it changes the meaning to remove them, parentheses are always optional.
If a line ends with two or more newlines, chomp removes only one. If there』s no newline, it does nothing, and returns zero.
The undef Value
Variables have the special undef
value before they are first assigned, which is just Perl』s way of saying, 「Nothing here to look at—move along, move along.」 If you try to use this 「nothing」 as a 「numeric something」, it acts like zero. If you try to use it as a 「string something,」 it acts like the empty string. But undef
is neither a number nor a string; it』s an entirely separate kind of scalar value.
Because undef
automatically acts like zero when used as a number, it』s easy to make an numeric accumulator that starts out empty:
# Add up some odd numbers
$n = 1;
while ($n < 10) {
$sum += $n;
$n += 2; # On to the next odd number
}
print "The total was $sum.\n";
Similarly, you could have a string accumulator that starts out empty: $string .= "more text\n";
If $string is undef, this will act as if it already held the empty string, putting "more text \n" into that variable. But if it already holds a string, the new text is simply appended. Perl programmers frequently use a new variable in this way, letting it act as either zero or the empty string as needed.
The defined Function
The line-input operator <STDIN>
is one operator that can return undef
. Normally, it will return a line of text, but if there is no more input, such as at end-of-file, it returns undef
to signal this. To tell whether a value is undef
and not the empty string, use the defined
function, which returns false for undef, and true for everything else.