Scalar Data and Scalar Variables

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

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.

Strings

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'

Table 2.1. Perl Escape Characters for Strings
Character Meaning
Newline
Carriage return
Tab
f Formfeed
 Backspace
a Bell
e Escape
nn Octal (where nn are digits)
xnn Hexadecimal (where nn are digits, a-f or A-F)
cC Control characters, where C is any character (for example, cC is equivalent to Control-C)
u Make next letter uppercase
l Make next letter lowercase
U Make all following letters uppercase
L Make all following letter lowercase
Q Do not match patterns (regular expressions only)
E End U, L or Q sequences

Note

We'll look at u, l, U, L, and E in “Going Deeper” at the end of this chapter. Q will be covered as part of regular expressions on Day 9, “Pattern Matching with Regular Expressions.”


Empty strings, that is, strings with no characters, are represented simply with quotes and no characters. Note that the empty string "" is not the same as a string with a space (" ").

So which should you use, double- or single-quoted strings? It depends on what you need the string for. If you need escapes and variable interpolation, use a double-quoted string. If you've got a string with a lot of dollar signs in it (actual dollar amounts, for example); then you might use a single-quoted string instead.

Converting Between Numbers and Strings

Because both numbers and strings are scalar data, you don't have to explicitly convert between them; they're interchangeable. Depending on the context, Perl will automatically convert a string to a number or vice versa. So, for example, the string "14" could be added to the number 5 and you'd get the number 19 as a result.

This might sound kind of weird, at first, but it does make things like input and output very easy. Need a number entered at the keyboard? You can just read what was typed and then use it as a string. There's no need to use special functions or procedures to convert everything back and forth. Perl does it for you.

Perl will even attempt to do the right thing with seemingly nonsensical statements such as "foo" + 5 or "23skidoo" + 5. In the former, a string without any numbers ("foo") will convert to 0, so "foo" + 5 evaluates to 5. For the latter, and a numeric string with extra trailing characters "23skidoo" will just lose the extra characters, so, "23skidoo" + 5 evaluates to 28. If you use Perl warnings (via the -w option or by choosing Scripts, Compiler Warnings in MacPerl), Perl will complain about these sorts of operations, as well it should.

Note

There's one exception to the automatic conversion in Perl, and that's with strings that appear to contain octal and hexadecimal numbers. See “Going Deeper” later in this chapter for details.


Scalar Variables

To store a piece of scalar data, you use a scalar variable. Scalar variables in Perl start with a dollar sign ($) and are followed by one or more alphanumeric characters or underscores, like this:

$i
$length
$interest_compounded_yearly
$max
$a56434

The rules for picking a name for your variable (any Perl variables, not just scalar variables) are

  • Variable names should start with a letter or underscore (after the initial $, of course). Other numbers or characters such as %, *, and so on are usually reserved for special Perl variables.

  • After the first character, variable names can contain any other letters, numbers, or underscores.

  • Variable names are sensitive to upper and lowercase—that is, $var is a different variable from $VAR and from $Var.

  • Variables that start with a letter or an underscore cannot be longer than 255 characters (I personally get a headache trying to imagine variable names that long, but hey, knock yourself out).

You don't have to declare or initialize variables in Perl; you can use them as you need them. Scalar variables without initial values will have the undefined value. You don't have to worry about the undefined value—it'll always show up as either an empty string or a 0 value, depending on where you use them (that interchangeability feature again). You really should initialize your variables explicitly to something, however, and if you have warnings turned on in Perl, you'll get warnings that let you know when you're trying to use an undefined variable.

Note

I'm glossing over the undefined value here. At this point, you don't have to know much about it although later on in this book (on Day 4, “Working with Lists and Arrays,” specifically), you'll learn more about how to test for it (using the defined function) or undefine a variable (using the undef function).


To assign a value to a variable, use an assignment operator. The most common one is the equal sign (=). This assignment operator simply assigns a value to a variable, with the variable reference on the left and the value on the right, as follows:

$i = 1;

The assignment operator returns the value of the thing assigned, and assignment expressions evaluate from right to left, so you can “cascade” assignments like this (where $b is assigned the value 4, and then $a gets the value of that expression (also 4):

$a = $b = 4;

Perl also has a number of shortcut assignment operators that I'll discuss tomorrow in “More Scalar Data and Operators.”

If you've used other languages, you know that variables are usually considered to have a global or local scope. Perl variables are no different: A variable used in the main body of a Perl script—as we're using it here, and as we'll be using it for the time being—is global in scope to that script (it's available to all parts of the script).

Perl also allows you to create local variables inside subroutines and loops, and also allows global variable namespace management across scripts via packages, but you have to declare and use those variables in a special way to prevent them from being global. You'll learn more about local variables on Day 11, “Creating and Using Subroutines,” and more about scope on Day 13, “Scope, Modules, and Importing Code,” but for now we'll stick with simple global variables.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset