Joe’s Notes on Perl #1 – Scalars and Operators

Hi all! As you may know, I’ve started studying BioInformatics. Part of this includes learning Perl, a very versatile and widely used language to help manipulate data.

Now what am I hoping to accomplish with these posts? Hopefully some sort of understanding in my head about how Perl works and how it can be adapted. If these notes help you, please post a comment.

These notes are written for Linux based systems, but will also work on Windows. Try Strawberry Perl.


Basics of Perl – Scalars and Operators

This is probably one of the more wordy posts to do with Perl. Don’t worry though, take your time and read though it.




Important note on syntax


Scalars – $

Scalars are variables that can store a single number or line of text. They are one of the most basic things in Perl to know as they can be used in a variety of cases. Scalars begin with a dollar sign “$” and can take any name you like. A semicolon “;” denotes the end of a variable and must be included. For example:


Be very careful when naming variables in Perl, some names may be restricted. See here for a list of them. This applies to all variables not just scalars.

So how can we use scalars in a simple program?

Open your favourite text editor (please don’t use Word, I recommend Notepad++ for Windows and Geany or Sublime Text 2 for Linux) and try this:


Save this somewhere with the extension .pl and open up your terminal. Change directory to the folder and run your script with: perl

Probably the most simple of all programs, the output will simply be “Hello World!”.



Syntax: print

A word on the print statement while we’re at it. print will print out anything (as the name suggest) to the terminal.

Using different quotation marks will yield different results:


Double quoted strings will not accept special characters (eg \ or ) unless they are escaped. Escaping characters is simple, just place a backslash in front of the special character you want to print. For example, if you wanted to print a double quote in a double quoted string: “Hello \“World\“”. Punctuation (. ,) doesn’t need to be escaped in quoted strings.

Now printing out in this way is all well and good, however if you had multiple print statements in a row you would end up having a single chunk of text instead of single lines. Special characters are included in Perl to indicate a newline (\n) or a tab (\t).


Integers and real numbers

Scalars are good at storing numbers but say you want to do something to those numbers. Perl supports arithmetic operators:


In loops (coming later), adjusting numbers by one can be very useful for counting things.


Checking relationships between strings can be accomplished by using equality operators:


String operators

Strings have their own set of operators especially for them, they work the same as numerical operators:


Repetition and Concatenation

If you need to repeat a scalar many times or concatenate them into one string, using ($scalar x n) and a period ($scalar1 . $scalar2) will help you:


A note on Syntax and good practice

When writing a Perl script, it is generally good practice to head your script with the following:

Now what do these 2 lines do?

Line #1 points Linux to the correct path to run Perl from and -w instructs Perl to give warnings if something goes wrong. The path /usr/bin/perl may change depending on your distribution or on Windows. Consult which perl in your terminal for the correct path. Also this allows you to run your script by calling it and not though the Perl interpreter. Chmod your script by using u+x and run the script with ./ from the terminal.

Line #2 tells Perl to use strict markup. Why is this important? When writing in Perl it is all too easy to miss type a variable name using strict forces you to prefix all variables with the word my:

With all that, your brain is probably melting. The important thing is to practice to get the hang of things. Next up are arrays and hashes, but that’s for another time.


Inspired by lecture notes from Dr Shepherd and from PerlDocs.