Workshop in Computational Bioskills - Lesson 2 - Scalars

Workshop in Computational Bioskills - Spring 2011

Lesson 2 - Perl I

Introduction to Perl
Part 1 - Scalars ($)
Part 2 - Lists/Arrays (@)
Part 3 - Hash tables/Associative Arrays (%)

Scalar Data:
Scalars are one-dimensional variables, denoted with a "
$".
These variables are usually numbers or strings.

Numbers:

All numbers in Perl share the same internal representation (double-precision floating-point).
There's no real difference between
$int = 3;
$double = 3.0;

Strings:

Merely a sequence of characters.
A single-quoted
'string' is different than double-quoted "string":
Variables and backslash have meaning only in a double-quoted string.

Let's try both versions:
$a = 4; # Set the scalar variable $a to 4.
$dou = "\$a is $a"; # being interpreted as "$a is 4"
$sin = '\$a is $a'; # being interpreted to "\$a is $a"

Variables:

There are almost no limitations regarding variables names (should begin with a letter).
Variables names are case sensitive.


Operations on Scalars:

We'll review some basic operators for numbers and strings.

Numbers:

- Nothing too revolutionary here..., very similar to C: =, !=, <, <=, ++...
- "
<=>" is a nice binary comparison operator (similar to strcmp) that returns 1, 0 or -1.

Strings:

- Comparison between strings is done in alphabetic order using the {eq,ne,lt,gt,le,ge} commands.
- To concatenate strings use "
." (a dot).
- "
x" functions as a repetition element. (try "="x70)
- The "
++" operator is well defined on strings. Do try it. How about "--" ?
- The "
length" command returns the length of a string. What will it do with numbers ?

Conversions between numbers and strings:

Usually, you won't be needing to explicitly convert between strings and numbers.
Instead, let Perl do the job. It does it pretty well.

The automatic conversion rules are quite simple, though:
(a) A string representation of a number will be converted to that number.
(b) A string with a leading number will be converted to that number (warning).
(c) All other strings will be converted to 0 (warning).

All numbers will be converted to their string representation:
(e.g. 3, 3.0, 3e0, 0.3e1 will all be converted to "3" in a string context)

Boolean Values:

In Perl, there are no boolean values.
Instead, only two scalars are defined as "
false":
(a) The empty string "", and (b) the zero string "0".

All other values get the default value of "true".
Although a bit bizarre, this works well. Try a few
if conditions.

undef:

undef is the value of uninitialized variables.
It is automatically converted to 0 or "" according to the context, with a
warning.

I/O:

We'll discuss I/O more thoroughly later, but for the meantime...:
Reading a line from the standard input into a variable is done by the diamond
< > brackets.

$a = <STDIN>;
will wait until a line is read from the standard input.
$a will now hold the line *including* its trailing newline.

The command "chomp" will delete it.
chomp($a=<STDIN>); # will read the line into $a, and chomp it.

Let's try writing our first program: NumberChecker.pl


This summarizes Scalars. Now Let's continue to Part 2 - Lists/Arrays (@)

[Based upon "Learning Perl, 2nd Ed.", by Randel L. Schwartz & Tom Christiansen, Chap. 2]