03 Part 2: Scalar Variables

LinuxChix Perl Course Part 2: Scalar Variables

1) Introduction
2) Scalars
3) Operators
4) Quotes
5) Exercises
6) Answers to previous exercises
7) Acknowledgements
8) Licensing


----------------------------------------

1) Introduction

There are four basic data types in Perl. They are:
a) scalar - A single chunk of data
b) array - A bunch of scalars, indexed by a number
c) hash (hashtable) - A bunch of scalars, indexed by another scalar
d) handle - A pointer enabling the opening of resources from the
operating system (files, directories, etc.)

For right now we're only going to look at scalars.

----------------------------------------

2) Scalars

A scalar may represent text or a number. A scalar variable will
always begin with a dollar sign.

To declare a scalar variable (or any other, for that matter), use the
"my" keyword:

my $foo; # Initialises to the undefined value.
my $bar = "some text"; # Initialises to a string (text).
my $baz = -25; # Initialises to an integer.
my $pi = 3.14; # Initialises to a floating point.

Once you have declared a scalar variable, you can assign it any
scalar value you like. There are no type restrictions.

my $a = 10;
my $b = 20;
$a = $a + $b; # Now $a=30.
$b = "a bunch of text"; # Now $b is a string. That's okay.
my $c = "I see $a stars"; # Now $c="I see 30 stars".
$b = undef; # Now $b has the undefined value.

There are also no restrictions on the size of strings (text).
Standard restrictions apply on the size of numeric types (for
example, I think that integers are 64 bits in Perl, so an integer
can't exceed 2^61).

----------------------------------------

3) Operators

The reason that there are no type restrictions on scalars is that all
scalars are inherently text (except maybe references, but we'll get
to that later). Some operators just extract numerical information
from that text. For example:

my $a = 2 + 5; # Now $a=7
my $b = "2" + "5"; # Okay, same as previous.
my $c = " 2 " + " 5 "; # Okay, same as previous.
my $d = "6" / "3"; # Now $d=2

Because a scalar can represent so many types, the comparison
operators could lead to ambiguity. For example, when performing the
comparison "3 < 12", the result depends on whether to we want to
compare as a number or as a string (because the first character of
"12" is "1", which is less than "3").

To resolve this ambiguity, Perl has both lexical (string) and
numerical comparison operators:
Numerical Lexical Meaning
== eq equal
!= ne not equal
< lt less than
<= le less than or equal to
> gt greater than
>= ge greater than or equal to

In addition to these, Perl has almost all the operators found in C++:
arithmetic, binary and logical. The few exceptions include the unary
"&" (memory address, which in Perl is a backslash) and cast
operations (which don't exist in Perl).

In addition, here are two other Perl operators not found in C++:
. (period) string concatenation (don't use + for this)
** (two astrisks) exponentiation (don't use ^ for this)

----------------------------------------

4) Quotes

Perl treats single and double quotes similarly to a shell script:

my $animal = 'dog';
my $double = "Cute $animal!\n"; # Cute dog![newline]
my $single = 'Cute $animal!\n'; # Cute [dollar]animal![slash]n

Perl supports a variety of other types of string literals, such as
"here documents":

my $foo = <<HERE;
This is a
very long string.
HERE

If you're not familiar with "here documents", don't worry about it.

----------------------------------------

5) Exercises

a) Write a program called 'average.pl' to calculate the average of
the following numbers: 23, 28, 31, 17, 1

(Remember an average is where you add the numbers together and divide
by the amount you have, this is learning perl not maths 101! On the
other hand, the maths is normally the hardest part.)

b) Consider the following program:

#!/usr/bin/perl -w
use strict;

my $foo = 'Suzy' x 4;
print "$foo\n";

What does the "x" operator do? Why can't we write "4 x 'Suzy'" as
well as "'Suzy' x 4"?

c) Consider the following program:

#!/usr/bin/perl -w
use strict;

my $a = q/This is string A/; # using slash
my $b = q!This is string B!; # using exclamation point
my $c = q:This is string C:; # using colon
print "$a\n$b\n$c\n";

What does the "q" operator do? What is the advantage of using it
rather than a single quote?

----------------------------------------

6) Answers to previous exercises

a) "\n" is a newline.

b) Perl stands for Practical Extraction and Report Language, or
Pathologically Eclectic Rubbish Lister depending on your point of
view.

----------------------------------------

7) Acknowledgements

A big thank you to Jacinta Richardson for suggestions and
proofreading. More advanced Perl users might want to check out the
free material from Perl Training Australia
<http://www.perltraining.com.au/>, which she is a part of.

Other contributors include Meryll Larkin.

----------------------------------------

8) Licensing

This course (i.e., all parts of it) is copyright 2003-2005 by Dan
Richter and Alice Wood, and is released under the same license as
Perl itself (Artistic License or GPL, your choice). This is the
license of choice to make it easy for other people to integrate your
Perl code/documentation into their own projects. It is not generally
used in projects unrelated to Perl.