|
PerlTutor - The Interactive Perl Tutorial
|
|||||||||||||||||||||||||||||||||||||
|
Lesson
2 - Scalar Data
|
|||||||||||||||||||||||||||||||||||||
|
Perl is essentially a data processing language. A Perl program can input data, store data, process data, and output data. Perl has three basic data types:
This Lesson introduces scalar data. Arrays and hashes are covered in later lessons. By the end of this Lesson you should understand the following concepts:
|
|||||||||||||||||||||||||||||||||||||
|
2.1 Scalar Variables & Literals |
|||||||||||||||||||||||||||||||||||||
|
A scalar is a single item of data, such as a number or a string. Scalar variables always begin with the '$' character. Here are some examples:
Think of a
variable as a container used to store data.
If a Perl program asks you to input your name, it needs somewhere to store
that data - a variable. True to its name, the value of a variable can
vary during execution of a program: e.g. The $ tells Perl that weekly_salary is a scalar variable. As you will see later, variables of other data type are denoted by different initial characters - e.g. array variables must begin with the '@' character. This example also introduces an example of a scalar literal. A literal is a constant value in a Perl program, e.g '22.50' or 'December'. It is a fixed or hard-coded value, which can be a number or a string. The rules for declaring scalar variables are:
It is good programming practice to give variables meaningful names - e.g. $loop_index is more descriptive than $i. Scalar variables can be assigned values, and manipulated with operators. The equal sign '=' is used to assign values. An assignment statement consists of a variable name to the left of the '=' and the value to be assigned on the right. Some examples of assignment statements:
You will see from the above examples that scalars can be both strings and numbers, and in fact Perl uses strings and numbers interchangeably - more on this later. |
|||||||||||||||||||||||||||||||||||||
| 2.2 Numbers | |||||||||||||||||||||||||||||||||||||
|
Perl can handle both whole numbers (integers, like 37) and floating-point numbers (real numbers with decimal points, like 17.5 or -235.2). Internally, Perl handles both as 'double precision floating-point values', but in Perl code they are treated the same way and can be used interchangeably. Here are some examples of number literals : 128 (positive
integer) The last numeric literal above is an example of exponential or scientific notation, used when you need to work with very large or very small numbers. In addition to decimal literals, Perl supports octal (base 8) and hexadecimal (base 16) literals. Octal literals are denoted by a leading 0, and hex literals by a leading 0x or 0X. For example: 0177 # 177 octal, same as 127 decimal Many people writing Perl programs will never need to work with octal or hex numbers - but be careful not to specify decimal numbers with a leading zero, because Perl will interpret them as octal. |
|||||||||||||||||||||||||||||||||||||
| 2.3 Strings | |||||||||||||||||||||||||||||||||||||
|
A string is a sequence of characters. Each character is an 8-bit value from the standard ASCII 256-character set. Like numbers, strings have a literal representation (i.e. the way the string is represented in your Perl code. The shortest Perl string has no characters, and there is no theoretical upper limit - the only constraint is the free memory available. In practice of course, you are unlikely to define a string which uses all of your computer's memory! Strings may be delimited by single or double quotes. A single quoted string is simply any sequence of characters enclosed by single quotes. The only exceptions are:
Examples:
A double quoted string is a sequence of characters enclosed in double quotes. Double quoted strings have two important features:
|
|||||||||||||||||||||||||||||||||||||
| 2.3.1 Backslash Control Characters | |||||||||||||||||||||||||||||||||||||
|
The most commonly used Backslash Control Character is \n (Newline). For example, "Welcome to my home page\n" is interpreted by Perl as "Welcome to my home page" plus a newline. The complete list of backslash escapes is shown below. Don't worry too much about memorising them all right away, but you should be aware that they exist.
|
|||||||||||||||||||||||||||||||||||||
| 2.3.2 Variable Interpolation | |||||||||||||||||||||||||||||||||||||
|
When a string is double-quoted, it is scanned for variable names. When a variable reference is found, it is replaced with its current value. This process is known as variable interpolation. For example: $href = "www.perltutor.com"; The output from these two statements would be: The URL of this tutorial is www.perltutor.com If a variable is referenced which has not yet been assigned a value, it is replaced by an empty string. |
|||||||||||||||||||||||||||||||||||||
| 2.4 Auto-Conversion Between Strings and Numbers | |||||||||||||||||||||||||||||||||||||
|
When a string value is used in a context where a number value is required, Perl automatically converts the string to its equivalent numeric value. For example: $salary = 1000; #
number value Conversely, if a numeric value is specified where a string is needed, the numeric value is expanded into whatever string would have been printed for that number. For example: $year = 2000; #
number value This subject is dealt with in more detail in Lesson 3 (Operators). |
|||||||||||||||||||||||||||||||||||||
| 2.5 <STDIN> as a Scalar | |||||||||||||||||||||||||||||||||||||
|
Perl uses filehandles to control input and output. Perl has 3 built-in filehandles which are opened automatically for each program. These are STDIN, STDOUT, and STDERR. Additional filehandles are created by the open command - more on this later. Each time you use <STDIN> where a scalar value is expected, Perl reads the bext complete text line from standard input (normally your terminal) up to the first newline, and uses that string as the value of <STDIN>. If there's no data waiting to be read, your Perl program will wait for you to enter some characters, terminated with a newline. $name = <STDIN>; # get the string of characters
typed by the user The string value of <STDIN> typically has a newline on the end of it. Often, you'll want to get rid of the newline right away. For example: $total = 123.0; Why is this wrong? Although Perl will obligingly convert $rate from a string to a number for the calculation, the string input as <STDIN> contains a newline, and should be removed first. Fortunately, Perl provides a special operator to do just that. The chop() operator removes the last character from a string variable: $rate = <STDIN>; A common abbreviation for the above 2 lines is: chop ($rate = <STDIN>); |
|||||||||||||||||||||||||||||||||||||
| 2.6 The undef value | |||||||||||||||||||||||||||||||||||||
|
So what happens if you use a scalar value before assigning it a value? Well, unlike some other languages, nothing fatal. Before a variable is explicitly assigned a value, it has the undef value. This looks like a zero when used as a number, or a zero-length empty string when used as a string. An operator will normally return undef when it can't make sense of the arguments. An example of this is the <STDIN> operator, which normally returns the next line read as a string. However, if there are no more lines to read - for example, if you press CTRL/D at the terminal - then the operator returns undef as a value. |
|||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||
| 1. Write a program which stores the string 'PerlTutor.com' in a scalar variable, then outputs the string three times, on separate lines: firstly as typed, secondly in all lowercase, and thirdly in all uppercase. |
|||||||||||||||||||||||||||||||||||||
| «previous page | |||||||||||||||||||||||||||||||||||||