Lab 2: Getting Started with Perl (and speeding tickets!)


Overview
Welcome back! Today, we are going to get started with writing a few basic Perl scripts. We'll also understand what chmod u+x and a "shebang" are good for.

 

Running Programs

Think back to your first Perl program from last time:

#!/usr/bin/perl -w

print "Hello, World!\n";

The very first line, #!/usr/bin/perl -w, is called a "shebang" line. The name comes from a contraction of "hash-bang" which is how the symbols "#!" are sometimes pronounced.

So long as we run our programs by perl hello.pl on the command line, we do not need the shebang line. When we run CGI scripts, though, we need the shebang to tell the web server how to run our script. Also, having a shebang lets us run our Perl scripts from the command line like real programs, without saying perl first. In order to do that, though, we need to tell the shell that our script can be executed. Here's how we do that:

jpr@sulu (~) % chmod u+x hello.pl
jpr@sulu (~) % ./hello.pl
Hello, World!
jpr@sulu (~) %

chmod u+x means "CHange the MODe of the file to allow the User to eXecute it." Note the ./ - that means "look for the script in the current directory," because . is shorthand for the current directory.

You should get into the habit of running chmod u+x on your new Perl scripts.

 

Math with Perl
Perl has all the math operators you'd expect, and some you might not. We'll talk a lot more about them later, but for now let's stick to +, -, *, and /.

Do This:
Log in to sulu.informatics.indiana.edu and use mkdir to create a directory called lab2 in your i211 folder. Use cd to change to your new directory, i211/lab2.

Next, let's write a Perl program to solve some simple math problems. To get you started, here's a Perl program that prints the sum of 768 and 5102:

#!/usr/bin/perl -w

print 768 + 5102, "\n";

The "\n" is code for a new line character. Try running this program with and without this character to see what the difference is.

Do This:
Create a Perl program named math.pl in your lab2 directory that prints out the answers to the following grade-school math problems, as well as the problems themselves. That is, the first line of output your program produces should be 8591 + 919 = 9510. End each answer by a newline (\n). Note: of course, your Perl program should compute the answer to each problem .. don't just type it in!
  1. 8591 + 919 = ?
  2. 2030 - 102 = ?
  3. 699 * 1020 = ?
  4. 1889 / 20 = ?


 

Variables!
Variables in Perl are places to store information that your program cares about. They are a way to give values a name to make them easier (or possible) to work with:

#!/usr/bin/perl -w
use strict; # Causes Perl to check for some common errors, HIGHLY RECOMMENDED

my $a;
$a = 5;
print "a is : $a\n";

jpr@sulu (i211/lab2) % chmod u+x vars.pl
jpr@sulu (i211/lab2) % ./vars.pl
a is : 5
jpr@sulu (i211/lab2) %

Notice how the value of $a was inserted in the text; this is called interpolation.

Of course, we can also do math with variables:

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

my $a = 5; # can do this on one line!

print "a is : $a\n";
$a = $a + 5;
print "a is now : $a\n";

Note that the = sign in Perl is not what you expect from mathematics. In mathematics, = means "these two quantities are the same". In Perl, it means "take the value on the right hand side and put it in the variable on the left hand side." Thus, $a = $a + 5 doesn't mean "a is equal to a + 5", because that's impossible. It means "Take the value of a, add 5 to it, and make that the new value of a."

Another cool thing we can do with variables is get their values from the user:

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

my $name;
print "Enter your name => ";
$name = <STDIN>; # read the name from the keyboard
chomp $name; # remove the "enter" key (\n) from the end
print "Hello, $name.\n";


Do This:
Create a file called handshake.pl in your i211/lab2 folder, and put the above code in it. Run it, and see how it works.


Do This:
Use the cp command to copy handshake.pl to a new file called age.pl. Modify age.pl so it asks for a name and an age, and stores each in a variable. Your program should then print out the person's name, and age in two years. Use what you learned about doing math on variables. An example output is shown below.
jpr@sulu (i211/lab2) % ./age.pl
Enter your name => Jacob
Enter your age => 100
Hello, Jacob. In two years, you will be 102.
jpr@sulu (i211/lab2) %


 

Speeding Tickets
Finally, on to the speeding tickets. The cost of a speeding ticket can be calcuated as follows (it's a little different in Indiana, but same idea):

cost = (miles over limit) * (cost per mile) + (base cost)
where
miles over limit = (your speed) - (speed limit)

Do this:
Write a program that fetches four variables from the user - their speed, the speed limit, the base fine, and the cost per mile over the speed limit. Your program should then apply the equations above to figure out how much they owe the state. Some sample output is shown below. Name your program ticket.pl. Do not worry about illegal input (e.g. when the user is not really speeding).
jpr@sulu (i211/lab2) % ./ticket.pl
Enter the speed limit => 55
Enter your speed => 65
Enter the cost per mile over => 5
Enter the base fine => 100

You were 10 miles over the limit. Thus, you owe 150 dollars.
jpr@sulu (i211/lab2) %

When this program works, congratulations, you're done! Time to submit.

 

Turn in your assignment
As usual, let's make a tarball of your assignment files (make sure they are ALL in the lab2 directory!) For this lab, you should have the following files:
  • math.pl
  • handshake.pl
  • age.pl
  • ticket.pl
As a reminder, here's the command you can use:

jpr@sulu (i211/lab2) > cd ..
jpr@sulu (i211) > tar cz lab2 > lab2.tar.gz
jpr@sulu (i211) >

You should then download this lab2.tar.gz file and upload to the Lab 2 assignment on Oncourse. Make sure you press "submit" and "finish" enough times to actually upload your file.

Okay, that's it! Have a good weekend.



Kidcop image © Chief Corporation
Thanks to Jacob Ratkiewicz for lab content, Sid Stamm for CSS stylesheets, and Rob Signorelli for original syntax-highlighting JavaScript.