next up previous
Next: Arrays Up: Basic Perl Programming Previous: Perl Variables

Scalar Variables

Scalar variables can be either a number or a string -- What might seem confusing at first sight actually makes a lot of sense and can make programming a lot easier.

Defining Scalar Variables

You define scalar variables by assigning a value (number or string) to it.

The following are simple examples of variable declarations:

$first_name = "David";
$last_name = "Marshall";

$number = 3;

$another_number = 1.25;

$sci_number = 7.25e25;

$octal_number = 0377; # same as 255 decimal

$hex_number = 0xff; # same as 255 decimal

NOTE:

String Scalar Variables

Strings are a sequence of characters. Perl has two types of string:

Single-quoted strings
-- denoted by `....'. All characters are regarded as being literal characters. That is to say special format characters like $\backslash$n are regarded as being two characters $\backslash$ and n with no special meaning. Two exceptions:
Double-quoted strings
-- Special format characters now have a special meaning.

Some special format characters include:

\n      newline
\r      carriage return
\t      tab
\b      backspace
\\      backslash character
\"      double-quote character
\l      lower case next letter
\L      lower case all letters until \E
\u      upper case next letter
\U      upper case all letters until \E
\E      Terminate \L or \E

Operators

Just as in most standard programming languages variables are operated on or assign results of operations. In Perl scalar variables and constants can be mixed an assigned as normal.

Common arithmetic operation are denoted by +,-,*,/, % (modulus) and ** (power).

For example:

$x = 3 + 1;
$y = 6 - $x;

$z = $x * $y;

$w = 2**3;  # 2 to the power of 3 = 8

Strings can be concatenated by the . operator.

For example:

$first_name = "David";
$last_name = "Marshall";

$full_name = $first_name . " " . $last_name;

we need the " " to insert a space between the strings.

Strings can be repeated with tt x operator

For example:

$first_name = "David";

$david_cubed = $first_name x 3;

which gives "DavidDavidDavid".

String can be referenced inside strings

For example:

$first_name = "David";

$str = "My name is: $first_name";

which gives "My name is: David".

Conversion between numbers and Strings

This is a useful facility in Perl. Scalar variables are converted automatically to string and number values according to context.

Thus you can do

$x = "40";
$y = "11";

$z = $x + $y;  # answer 41

$w = $x . $y;  # answer "4011"

Note if a string contains any trailing non-number characters they will be ignored.

i.e. " 123.45abc" would get converted to 123.45 for numeric work.

If no number is present in a string it is converted to 0.

Binary Assignment Operators and Auto increment

In common with C, Perl has two short hand operators that can prove useful.

In many statements we frequently write something like:

$a = $a + 1;

we can write this more compactly as:

$a += 1;.

This works for any operator so this is equivalent:

$a = $a * $b;
$a *= $b;

You can also automatically increment and decrement variables in Perl with the ++ and - operators.

For example all three expressions below achieve the same result:

$a = $a + 1;
$a += 1;
++$a;

The ++ and - operators can be used in prefix and postfix mode in expressions. There is a difference in their use.

In Prefix the operator comes before the variable and this indicates that the value of the operation be used in the expression: I.e.

$a = 3;
$b = ++$a;

results in a being incremented to 4 before this new value is assigned to b. That is to say BOTH a and b have the value 4 after these statements have been executed.

In postfix the operator comes after the variable and this indicates that the value of the variable before the operation be used in the expression and then the variable is incremented or decremented: I.e.

$a = 3;
$b = $a++;

results in the value of a (3) being assigned to b and then a gets incremented to 4 That is to that after these statements have been executed b = 3 and a = 4.

The chop() operator

chop() is a useful operator which takes a single argument (within parenthesis) and simply removes the last character from the string. The new string is returned (overwritten) to the input string.

Thus

chop('suey') would give the result 'sue'

Why is this useful?

Most strings input in Perl will end with a \n. If we want to string operations for output formatting and many other processed then the \n might be inappropriate. chop() can easily remove this.

Many other applications find chop() useful


next up previous
Next: Arrays Up: Basic Perl Programming Previous: Perl Variables
Dave Marshall
9/28/2001