Lab03: Some pearls (so don’t be a swine!)

Perline

We are going to try and modify some of the perl programs we saw during the lesson. Brace yourself! Note that the slides have been published.

Perlina Alpha

Create a new directory called lab03 in your home, as usual. All the programs you are going to write today should go there.

Now, this is the first perl program we saw today:

# A volte meglio mettere dei commenti
print "Hello World!\n";
if (2 < 3) {
  print "Due e' minore di tre.\n";
}

Now we want to save it in lab03:

  1. open the text editor in Applicazioni -> Accessori -> Editor di Testo,
  2. copy the program above and paste it in a new document,
  3. check that everything looks ok (often things can copy incorrectly from the web) and correct it if not so,
  4. save it as perl-alpha-1.pl in lab03 (but do not close it yet),
  5. in the terminal, go to lab03 and execute the script with:
$ perl perl-alpha-1.pl 

It should write:

Hello World!
Due e' minore di tre.

Now make a small change and change 2 to 3 in the if condition, so that now it is false. Save the program as to perl-alpha-2.pl (the name is different from before, use Save as!) Run it in the terminal and check the output. Now it should just print:

Hello World!

Perlina Beta

On to the second program:

$nome = 'Pina';
if ($nome eq 'Giovanni') {
  # eseguo tutto quello che e' fra { e }
  print "CIAO!\n";
} else {
  # eseguo tutto quest'altro blocco
  print "E allora, chi sei?\n";
}

Try to guess what should the out be? Save it as perl-beta-1.pl in lab03 and run it, following the same procedure given for the previous program. Did you guess right?

Since $name is not ‘Giovanni the first branch of the if gets executed. Now change the program and put ‘Giovanni’ in $nome. Save it as perl-beta-2.pl and run it. The output should change.

Perlina Gamma

The next program:

while ($risultato < 1000) {
  $risultato = $risultato**2;
  print "Siamo arrivati a $risultato\n";
}
print "FINE: $risultato >= 1000\n";

Save it as perl-gamma-1.pl and run it.

As you may have noticed there might be a slight problem in this program (Ctrl-c is your friend). The problem is that we did not put a number in $risultato before using it, so perl will put zero for us. Since zero squared is still zero, $risultato is always zero and the loop never ends since the condition is always satisfied.

But now you can learn through our mistakes! Correct the program by initializing (that is, putting a starting value in a variable) in $risultato. Put 2 in it (1 would not work much better than 0, guess why?). Be sure to put it before the while loop! Save it as perl-gamma-2.pl.

Perlina Delta

Another program (how many there are yet?):

for ($i=10; $i<20; $i++) {
  # eseguo tutto quello che e' fra { e }
  print "Ciao, $i volta su 20!\n";
}

Save it as perl-delta-1.pl and run it, you should be pretty good at this by now 😉

No surprises this time right? In the {…} block, $i goes from 10 to 20, as you can see from the output. Now do the following changes:

  1. change the loop so that it goes from -10 to 13 (save it as perl-delta-2.pl and run it to check that it works!)
  2. change the program so that it only print even numbers (as perl-delta-3.pl)
  3. change the program so that it goes from 45 to 21 and prints one number out of three, like 45, 42, 39, …, 24, 21 (as perl-delta-4.pl)

So, any question?

Is it not cute?

Leave a Reply

Your email address will not be published.