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:

  • scalars - used to store single data items
  • arrays - used to store lists of data
  • hashes - a special type of array used to store key/value pairs

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:

  • Scalar Data
  • Variables
  • Literals
  • Numbers
  • Strings

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:

  • $ratePerHour
  • $email_address
  • $_NAME

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.

$weekly_salary = 100.0;
$weekly_salary = weekly_salary + 22.50;    # add weekly bonus of £22.50

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:

  • the first character must be '$'
  • the second character should be a letter or an underscore '_'
  • subsequent characters can be letters, digits or underscores
  • the maximum length of a variable name is 255 characters
  • variable names are case-sensitive ($name is different to $Name)

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:

  • $email_address = 'huw@sharpsoftware.co.uk';
  • $year = 2000;
  • $taxRate = 22.5;

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.

Check Point

What type of data represents a single value?


Scalar Variables must begin with which character?
$pay-per-view is a valid name for a Scalar Variable : True   False
$loopcount and $loopCount could be used interchangeably in a Perl program - they would represent the same scalar variable: True   False

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)
-127 (negative integer)
0
17.5 (positive floating number)
-4.6E13 (negative 4.6 times 10 to the 13th power. E denotes exponential notation)

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
0xf0 # f0 hexadecimal, same as 240 decimal
-0Xff # negative ff hexadecimal, same as -255 in 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:

  • the single quote character itself, which must be preceding by a backslash to be included in a string
  • the backlash character, which must be preceded by a backslash.

Examples:

'Single Quoted Strings,' # Single Quoted Strings,
'aren\'t difficult' # aren't difficult
'C:\\AUTOEXEC.BAT' # C:\AUTOEXEC.BAT

A double quoted string is a sequence of characters enclosed in double quotes. Double quoted strings have two important features:

  • The backslash character can be used to specify control characters such as a Newline or Tab
  • Variable names within double-quoted strings are interpolated.
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.

\n Newline
\r Carriage Return
\t Horizontal Tab
\f Form Feed
\b Backspace
\a Alert
\e ESC character
\007 Any octal character (007 = bell)
\x7f Any hexadecimal character (x7f=DEL)
\cD Any Control Character (cD=CTRL/D)
\\ Backslash
\" Double Quote
\l Force next character to lowercase
\u Force next character to uppercase
\U Force all following characters to uppercase
\L Force all following characters to lowercase
\Q Backslash all following non-alphanumerics
\E End \U, \L, or \Q
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";
$print "The URL of this tutorial is $href";

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
$bonus = '222';                        # string value
$salary = $salary + $bonus;     # add string to number
print $salary;                            # prints 1222

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
print "The year is $year";  # $year is converted to the string '2000' before printing

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;
$rate = <STDIN>; # get interest rate
$total = $total * $rate; # WRONG!

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>;
chop ($rate); # get rid of that unwanted newline!

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.

Check Point

Which two characters are used to denote a hexadecimal number?


Which letter in a numeric literal denotes scientific notation?
'032' denotes a numeric literal with value decimal 32: True   False
$dir = 'images';
print 'directory is $dir';

Enter the output from the above 2 lines of Perl:

$dir = '\\images';
print "directory is $dir"
;
Enter the output from the above 2 lines of Perl:
Using a scalar variable before assigning it a value will always cause a fatal run-time error: True   False


Exercises

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.

2. Re-write the 'Hello World' program to prompt for a name, then print 'Welcome, name,to PerlTutor.com'

«previous page
next page»