Perl, for the most part, has a flexible concept of data types. Unlike languages such as C or Java, which have distinct types for integers, characters, floating-point numbers, and so on (and strict rules for using and converting between types), Perl only distinguishes between two general kinds of data. The first type is scalar data, for single things such as numbers and strings of characters; and the second is list data, for collective things such as arrays. The distinction is not academic; Perl has both scalar and list variables to keep track of, and different constructs in Perl might behave differently depending on whether they operate on scalar or list data, or the context—scalar or list context— in which they are operating. You'll learn about all these things as the book progresses. For today, however, let's keep things simple and stick to only numbers and strings and the variables that can hold them. We'll leave list data for later on in the book.
Note
Although today I'll refer exclusively to numbers and strings as scalar data, there's actually a third form: references are also a form of scalar data. But you don't need to know about references this early in your Perl education, so I'll ignore them for now. You'll learn about references much later, on Day 19, “Working with References.”
Numbers in the text of your Perl scripts can be used just about any way you'd like to type them. All the of following are valid numbers in Perl:
4 3.2 .23434234 5. 1_123_456 10E2 45e-4 0xbeef 012
Integers are represented by whole numbers, and floating-point numbers with an integer and decimal part. Floating points less than 1 can start with the decimal itself (as in .23434234), or can contain a decimal with no actual decimal part (5.). Commas are not allowed; the underscores in 1_123_456 are an optional way of expressing a longer number so that it's more readable (they're removed from the number when it's been evaluated). You indicate exponents using an uppercase or lowercase e and a positive or negative number (for a positive or negative exponent). Hexadecimal and octal numbers are represented by a leading 0x or a leading 0, respectively.
Perl does not differentiate between integers and floats, signed and unsigned, or short and long numbers. A number is simply a type of scalar data and Perl converts between number types as needed in your scripts.
In other languages, such as C, strings are considered a collection of characters and are stored in collective arrays, but in Perl a string is a singular thing and is simply another form of scalar data.
Strings can be represented in two ways: as zero or more characters surrounded by single quotes (' ') or by double quotes (" "). For example, all of the following are strings:
'this is a string' "this is also a string" "" "$fahr degrees Fahrenheit is $cel degrees Celsius" "Hello, world! "
Strings can contain any kind of ASCII data, including binary data (both high and low ASCII characters). However, text strings typically contain only low ASCII characters (regular characters, no accents, or special characters). Strings also have no size limits; they can contain any amount of data, limited only by the amount of memory in your machine. Although reading the complete works of Shakespeare or a ten-megabyte Unix kernel into a single Perl string might take a while and eat up a whole lot of memory, you could certainly do it.
There are two differences between using single quotes and double quotes for strings. The first is that a double-quoted string will perform variable interpolation on its contents, that is, any variable references inside the quotes (such as $fahr and $cel in the previous example) will be replaced by the actual values of those variables. A string with single quotes, such as '$fahr degrees Fahrenheit is $cel degrees Celsius', will print just like you see it here with the dollar signs and variable names in place. (If you want an actual dollar sign in a double-quoted string you'll have to put a backslash in front of the dollar sign: "that apple costs $14 dollars".) I'll discuss variables more in a bit.
Note
Variable interpolation also works with strings that contain list variables. You'll learn more about this on Day 4, “Working with Lists and Arrays.”
The second difference between single and double-quoted strings is that double-quoted strings can contain the escape characters shown in Table 2.1. These will look familiar to you if you know C, but there are a number of other escapes special to Perl. In single-quoted strings, escape sequences are, for the most part, printed as typed, so 'Hello World! ' will print as Hello World , instead of including a newline at the end. There are two exceptions to that rule: ' and \ will enable you to put quotes and backslashes, respectively, into single-quoted strings.
'This is Laura's script' # prints This is Laura's script 'Find the input in C:\files\input' # prints Find the input in C:filesinput'
Character | Meaning |
---|---|
Newline | |
Carriage return | |
Tab | |
f | Formfeed |
Backspace | |
a | Bell |
e | Escape |