Perl Variables  «Prev  Next»
Lesson 5Numeric Strings and Literals in Perl
ObjectiveExamine how numeric and string literals are specified.

Numeric Strings and Literals in Perl

In programmer-speak, literals are values that are entered in the source code explicitly. For example, in the expression $x = 5 the number 5 is a literal.
There are two types of literals to consider:

Perl Numeric literals

Question: What is the numeric literal for the number 10 in Perl?
In Perl, the numeric literal for the number 10 can be represented in several ways. Here are some examples:
  1. As a decimal integer:
    10
    
  2. As an octal integer (using the "0" prefix):
    012
    
  3. As a hexadecimal integer (using the "0x" prefix):
    0xA
    
  4. As a binary integer (using the "0b" prefix):
    0b1010
    


Numeric literals can be specified in a number of different manners.
479            # simple integer
479.38         # simple real number
               # (floating point)
.23E-10        # scientific notation
0x01df         # hexadecimal
0377           # octal numbers have 
               #    a leading 0
4_294_967_296  # underscores are ignored  
               #    and can be used for
               #    legibility

All of the following declarations are equivalent:
$x = 479;      # decimal (base 10)
$x = 0x01df;   # hexadecimal (base 16)
$x = 0737;     # octal (base 8)

Note that a leading zero is used to specify octal, so the following two declarations are not equivalent:
$x = 473;
$x = 0473; # 473 octal is 315 in decimal!

Tip on Octal Numbers

Since 9 is not a valid digit in octal (0-7),
$x = 0479 will produce an error message.
What is the difference between these two lines of code?

$x = 374;
$x = 0374;

The first one is decimal and the second is octal. Remember, literal numbers that start with a zero are interpreted in octal (base 8).

String literals in Perl

String literals are normally enclosed in single quotation marks (') or double quotation marks (").
Single quotation marks are used to represent the exact text of the literal string; double quotation marks are used to interpolate the contents of the string.
For example,
#!/usr/bin/perl
$hi = 'Hi!';
print 'This is in single quotes: $hi'; 
print "\n";

will print this:
This is in single quotes: $hi

And this version,
#!/usr/bin/perl

$hi = 'Hi!';
print "This is in double quotes: $hi"; 
print "\n";

will print:
This is in double quotes: Hi!


In cases where either form will do, it is really a matter of style which one you choose to use. Some programmers favor the double quotation marks, and others favor single quotation marks. Rumors that one performs better than the other are largely unfounded.

Quote operators in Perl

In addition to the quotation marks themselves, there are also operators that you can use to specify your own quote characters. In short, the q operator specifies a character that works like a single quotation mark, and the qq operator specifies a character that works like a double quotation mark. These operators are covered in more detail in the "String Operators" lesson of the next module, but they essentially work like this:

$x =q(This uses parenthesis as single quotes);
$x = qq|This uses vertical bars as double quotes|;


When characters that have matched pairs are used (for example, parenthesis, brackets, and so on), the matching character is used to close the quotes; otherwise the same character is used on both sides. For example, if you have some text that you want to use, but the text itself includes quotation marks, it can be convenient to use the quote operators to delimit the text:

$phrase =  q{"Four score and seven years ago," said Abe.};

Numeric literals are frequently used and represent a number that your program will need to work with.
Most of the time you will use numbers in ten-base expression.
Perl will also let you use base 8 (octal) or base 16 (hexadecimal).
  1. In octal notation or base eight, when you see the value 15 it signifies (1 * 8) + 5 or 1310.
  2. In hexadecimal notation when you see the value 15 it signifies (1 * 16) + 5 or 2110.
    Base 16 needs an additional 6 characters besides the digits 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.