[Courses] LinuxChix Perl Course Part 2

Alice Wood alice at schiffwood.demon.co.uk
Sat Mar 15 13:02:50 EST 2003

LinuxChix Perl Course Part 2: Scalar Data


1) Good programming guide
2) Scalar data
3) Exercises
4) Where to find out more...


1) Good Programming Guide

white space
-w switch
use strict

As beginners it is always a good idea to try and start off with good habits so 
that as we become more experienced, we are regularly building into a program 
pointers to help us when we have a problem. Good programs will be 
appropriately commented and have lots of white space (gaps between sections, 
and longer lines).

It is also possible to use tools that are built into Perl to help you spot 
potential problems. The first is an addition to the top line.

#!usr/bin/perl -w

The -w switch allows Perl to print warnings if the variables in programs are 
_probably_ not being used correctly, if there is a function infinitely deep 
in recursion or when it spots something suspicious in the program. There are 
many switches available for use in Perl. further information about these can 
be found on the Perlrun manpage.

use strict;

It is useful to put this in as the second line to a program (see below). 

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

This will enforce good programming rules throughout your program. For example, 
if you declare

my $suzy = 7;

then later do

$Suzy +=1;

Your mistake will be caught automatically.


2) Scalar Data

There are four basic data types that Perl uses.
These are:
scalars - A single chunk of data
arrays - A bunch of scalars, indexed by a number
hashes - A bunch of scalars, indexed by another scalar known as a key
handles - A pointer enabling the opening of resources from the operating 
system (files, directories, etc.)

A scalar is denoted by $. It may be used to represent integers, strings, 
floats, any block of information. The syntax for creating a scalar is

$variable = <value>;

Where the value may be numeric, string, reference or boolean.
There is no size restriction on a scalar, so you may make your scalar as large 
as necessary. Perl is similar to shell scripting when assigning values to a 

Below are some examples of scalars

$scalar1 = 'this is a string';	# simple scalar assigned 'this is a string'
$value = 10.0;			# simple scalar assigned 10 (number is rounded)
$value2 = '10.0'		# number that acts as a string, number is not rounded
$scalar2 = "this is $value";	# interpolation, assigned 'this is 10.0'
$scalar3 = 'this is $value';	# non-interpolation, assigned 'this is $value'
$empty = '';			# empty string

Note the use of quotes here. The single quote (') indicates that the text will 
be used verbatim and the double quote (") indicates the text will be 
It is possible to operate on scalars.
For example

$suzy = $luigi + 4;

$suzy = $suzy - 3; # alternatively, $suzy -= 3;

The scalars may also be operated on as strings.
For example

$suzy = $suzy x 3; # this will print suzysuzysuzy

$suzy = $suzy . 'space'; # this will append 'space' to the string held in 


3) Exercises

a) Create a program 'newline.pl' containing the following:

print"Hi there.\nThis is my second program.\n"

Run this and then replace the \n with a space or an Enter and compare the 

What is the purpose of \n?

b) 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! Although, the maths is 
normally the hardest part.)

c) Modify the program called 'repetition.pl' to work more efficiently. 
(Remember there's more than one way to do it!)

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



Answers to previous exercises

1) You should have found that your top line was something like as stated in 
part 1 of the lesson.

2) 'Hello World' should have appeared on your screen.

3) Practical Extraction and Report Language, or Pathologically Eclectic 
Rubbish Lister depending on your point of view.


4) Where to find out more...

An archive of lessons is being created at


Also, there are links to useful websites and other books.

The archive should appear on the LinuxChix website soon.

Part 3 of the course will follow on 29th March 2003


Note: The author uses Perl version 5.6.1. All exercises should therefore 
work on later versions. If you wish to find out which version of Perl you are
running type "perl -v" into your command line.

If you have any comments regarding this perl lesson, suggestions for future 
topics, useful website addresses, useful reference material or you just want 
to say what you thought, please send your email to 

alice at schiffwood.demon.co.uk

I will be pleased to hear from you.

Credits and acknowledgements

Thanks to Meryll Larkin and Jacinta Richardson for their help with putting
together this material. 
Sources of material include Learning Perl, Randal Schwartx & Tom Phoenix,
Perl Training Australia (http://www.perltraining.com.au),
Perl 5 Developer's Guide, Peschko & DeWolfe

Suzy is the name of my cat.

Alice Wood

Project Gutenberg newsletter editor

More information about the Courses mailing list