Lab02/3: first steps with Perl

This post is a part of the second laboratory session. Please refer to the main post. 

Ready to start programming? We hope so. This post will teach you the very first things 🙂

What a Perl program is

As we already said in class, a Perl program (or script, better), is a plain text file, as a FASTQ or SAM file is. It contains a set of instructions that the computer can execute. Unlike human beings, computers love repetitive tasks…

We are going to give you an actual lesson of Perl next Friday, but we think it is a good idea to make you touch it in advance.

Every programming language has:

  • variables: basically they are a piece of memory that can store an information, and has a name.
  • conditionals: we execute an instruction IF some condition is verified.
  • loops: we execute a set of instruction as many times as needed. Example: for every sequence in a FASTA file, calculate the reverse complement and print it.

Warning - Please be careful This tutorial will make you understand something about programming. You’ll meet both notions and questions. Answer to the questions in your notebook, as you’ll be required to answer orally on Friday.
If you have to or want to do this part at home, you’ll find an online version of all the programs mentioned throughout the post at the end of the page. 

Launching your first program

In your home folder you’ll find a demo02 directory containing some files. Yes, they are simple Perl scripts. To run a Perl program the general syntax is:

perl /path/to/script_file.pl

if the script is in the folder we are working in, we can just type “perl script_file.pl“, otherwise, as always, we need to specify its path (absolute or relative).

Go in your home folder and type:

perl demo02/primo.pl

Something happened: a universal message of love appeared on thy screen. What is inside the script? Check it with

cat demo02/primo.pl

Warning - Please be careful Before going on, be sure to understand what Save as means!

Inspecting your first program

To create a program we need a Text editor. The one in Applicazioni -> Accessori -> Editor di Testo will work (it’s called gEdit, just to know).

Now, open the primo.pl script from the text editor. You should immediately notice a couple of things:

  • The editor colours different portions of the script with different colours. This is so handy that you can’t live without. This feature is called syntax highlighting.
  • In Perl you can embed comments (i.e. notes for you) after a “#” character.

Then we can introduce some more things:

  • a Perl command needs a “;” at the end. That is, after every command we have to put a “;”
  • The print command will print a string on the screen. We are used to think of printing as a process involving some paper, and in fact that is the origin of the name! In several programming languages it just means “display a text on the screen”.
  • The backslash “\” alters the behaviour of the subsequent character. If you didn’t understand what the “\n” special entity means,… go on and discover it by yourself.

Editing your first script

Now let’s try doing the first change on the script. First, save it asprimo2.pl” in the same demo02 directory.

Now remove the “\n” at the end of the print command. Save the script and execute it from the terminal (you should know how to do, from the home directory it’s just perl demo02/primo2.pl. Compare the output with the original “primo.pl”.

Now restore the \n that you removed (always working with “primo2.pl”), and add an extra one after “Hello”.
Save the script and execute it… It’s now crystal clear what does the \n char do?!

Now reopen the original “primo.pl” script and save it as “primo3.pl” in the same demo02 directory. Replace the double quotes (“) with single quotes (‘) in the print command.

From the terminal, execute it and keep note of the difference.

A second Perl script: simple variables

From the terminal, execute the secondo.pl script in the demo02 directory. Again it is only printing some text on your screen.
Open it with the Text Editor, and check the syntax.

This scripts has two variables. In Perl you can recognize simple variables as they are preceded by a dollar sign ($). Their names, as everything else in Perl, are case sensitive. Variable names cannot start with a number and cannot contain special characters other than the underscore (_). Some examples: $VARIABLE, $variable, $var_1.

If you find still difficult to understand what variables are, don’t worry, you’ll find it more easy at the end of the day!

In Perl the “=” sign is used to assign some value to a variable. The syntax is:

$numeric_variable = 20;
$text_variable = 'text';

Note that we can write numbers without quotes, while we have to add quotes to assign to a variable a text value.

Now save the script as secondo1.pl, and replace the double quotes (“) with single quotes (‘) in the print command.

What’s happening now?

Note that the different behaviour of the quotes applies also when assigning a value to a variable.

Numbers!

Here we introduce some numerical operators, please keep note of them.

From the terminal, execute the operazioni.pl script. As you can see you’ll have the results of many operations… Again, open the script from Text Editor.

Now we can see what is going on: we can use operators (like +, – …) with variables to perform operations. I think you’ll be surprised by the % operator: did you understand what it’s used for?

Now save the script as operazioni1.pl, and replace the value of the two variables with numbers of your choice. Run it and test if the results fits your expectations 🙂

Now save the script as secondo2.pl, and replace the value of the second number with a “0” (zero). Execute it: what’s happening now???

This is your first crash of the program: if something goes wrong, Perl will “die”.

Strings (i.e. text)!

Here we introduce string variables (text), and operators working on them.

Also strings can be manipulated with operators. From the terminal, execute the parole.pl script. Inspect the output, and then open it.

As you can see we have two operators working here: the “x” (times) one, and the most popular: “.” (concatenate) operator.

In the “print” commands we can see another interesting fact. Strings are surrounded by quotes (single or double). So when we need a quote inside a string, we can add the \ before. Yes, again the backslash alters the behavior (it’s called to escape the default behaviour).

print "Questo comando stampa una stringa che contiene le \"virgolette\".\n";

If this, then that

We want to introduce here a very important structure: the conditional statement. First you should know that we can group a set of commands between curly brackets (like {these ones}). A set of instruction between brackets it’s called a block of code.

Now open the if.pl program, inspect its structure and then execute it. We have a structure like the following one:

if (condition is true) {
    execute this block
} else {
    execute this other block
}

What is less intuitive is that the “equal” sign in comparison is different than in assignments. When we assign a value to a variable we use the “=” sign, but when we want to compare two entities we have to use the “==” operator. Keep this in mind!

If you want to check if two numbers are different you can use the “!=” operator. Both the “==” and “!=” (called equal and not equaloperators can be used only with numbers.

If you want to compare two strings you have to use the eq (equal) or ne (not equal) operators. Example:

if ($name eq 'Andrea' or $name eq 'Giovanni') {
    print " You are the master!\n";
else {
    print " Good luck with the practicals!\n";
}

As you guessed… we are introducing the “or” operator. The first block is executed if at least one of the two condition is true, that is if $name is equal to Andrea or to Giovanni.

I fear you are scared enogh, but you shouldn’t! We are really going to explain a lot of Perl in a very dense lesson on Friday. That’s why it’s so much better for you to have worked a little on this!

Playing with Perl (home)

At home you can test some Perl scripts with the CodePad website, just remember to select Perl as desired language. You will see that this website will allow you to execute Perl code easily.

Try this simple script on codepad! You can change the code and execute it again to check what changes.

Here you are the online version of the scripts you have in your home directory:

Want to see a first loop (a structure to iterate operations?), of course you want. Here you are the for loop.

Tagged , , ,

2 thoughts on “Lab02/3: first steps with Perl

  1. […] Open the “first time with perl” page […]

  2. […] Friday we’ll meet in Aula M Pr at 11.30, for a comprehensive course on Perl, so remember to repeat this part at home, try changing things in the […]

Leave a Reply

Your email address will not be published.