Outline
  • Everyone can program (in fact you do it everyday)!
  • Syntax and comments
  • SHEBANG, SHEBANG
  • Hello world and special characters
  • Variables
  • Math
  • Switching input and output
  • Putting it all together

Everyone can program


Programming is like telling a very stupid but competent robot what to do. It never makes mistakes. Rather, we make mistakes by not telling it what to do as exactly as possible. So in programming think of the program as having infinite ignorance, but infinite competence. In this way it's nothing like explaining your work to a collaborator (or undergrad) so that they can reproduce your result. Rather, it's like programming a thermalcycler for PCR. When your PCR doesn't work it isn't because the thermalcycler forgot to add polymerase. It always does its job, but it's too stupid to ask you if you remembered to add all the ingredients. So, how many of you have programmed a thermalcycler? Perl is just like that - infinitely stupid, but infinitely competent!

Syntax and Comments


As anyone who rotates through a couple labs here at Berkeley realizes, each lab has its own thermalcycler and each thermalcycler requires its own slightly modified set of instructions. It's the same in programming - different languages have slight quirks that make it possible to read code very easily. But the underlying logic of each thermalcycler program is the same. So what you will learn in this course is broadly applicable to other languages as well. So when it seems like we are bogged down in syntax (which inevitably it will seem like sometimes), just remember that syntax will come, but the logic is what really matters. To that end, it's always a good idea to start out with pseudocode, which will become more and more important later on. Further, perl lets you add comments into your program to give another programmer (and remember that you 15 minutes from when you first wrote something counts as another programmer) an idea of the logic and flow of what you were doing. This actually makes getting the syntax right easier, and it helps the TAs have an idea of what you are doing.

So let's start with some pseudocode.

MAKE PERL RUN THE PROGRAM
let perl check for errors at the beginning so i don't waste a lot of cluster time
 
Print out to the screen a greeting
 
End the program and return to the prompt

Now that we have the pseudocode, let's start our journey into syntax. The first thing we want to do is make perl run the program. We do this by adding a line called the shebang.

SHEBANG, SHEBANG


#!/usr/bin/perl
 
#let perl check for errors at the beginning so i don't waste a lot of cluster time
 
#Print out to the screen a greeting
 
#End the program and return to the prompt

The number symbol in perl is used to denote comments. Except first line, when the comment is followed by an exclamation point. This is used to tell the computer where to find perl. So I've commented out all the other lines and added the pointer to perl. If I run this program, it will run, but not do anything. If I uncomment some of the lines, perl won't recognize the syntax (it's too conversational - it's pseudocode) and it will fail.

#!/usr/bin/perl
 
let perl check for errors at the beginning so i don't waste a lot of cluster time
 
#Print out to the screen a greeting
 
#End the program and return to the prompt

This is a short program, so perl finds the error and complains pretty quickly. But what if the error is found 5 hours into a run? I've just wasted 5 hours of precious computer time. That would suck. So if we look at my next piece of psuedocode (the part that caused the error) and underneath it write our next bit of syntax, we'll avoid that problem.

#!/usr/bin/perl
 
#let perl check for errors at the beginning so i don't waste a lot of cluster time
use strict;
use warnings;
 
#Print out to the screen a greeting
 
#End the program and return to the prompt
__END__
These two bits of syntax are the most valuable piece of code in the entire arsenal. Notice that running this program now works fine because I've returned the pseudocode to comments. Now we want to write the first part of real code. I want to print something out to the screen that says Hello. I'm also going to get rid of the comment about using strict and warnings, because you should have that at the start of every program no matter what.
#!/usr/bin/perl
use strict;
use warnings;
 
#Print out to the screen a greeting
print "Hello, intro-to-programming for bioinformatics";
 
#End the program and return to the prompt
__END__
The command print send what follows it from one quote to the other out to the screen. The semicolon ends the print statement and tells perl to look for the next command. So we can't add stuff to the end of that line, but we could add another line.

So this won't work...
#!/usr/bin/perl
use strict;
use warnings;
 
#Print out to the screen a greeting
print "Hello, intro-to-programming for bioinformatics" print "Hello, especially to Lenny!";
 
#End the program and return to the prompt
__END__
But this will...
#!/usr/bin/perl
use strict;
use warnings;
 
#Print out to the screen a greeting
print "Hello, intro-to-programming for bioinformatics";
print "Hello, especially to Lenny!";
 
#End the program and return to the prompt
__END__
Notice how they print out onto the same line. This can be avoided with the special characters \n, which means newline!
#!/usr/bin/perl
use strict;
use warnings;
 
#Print out to the screen a greeting
print "Hello, intro-to-programming for bioinformatics\n";
print "Hello, especially to Lenny!\n";
 
#End the program and return to the prompt
__END__
If infact I wanted to have them print on the same line, but in two separate strings, I could do this:
#!/usr/bin/perl
use strict;
use warnings;
 
 
#Print out to the screen a greeting
print "Hello, intro-to-programming for bioinformatics ","Hello, especially to Lenny!\n";
 
#End the program and return to the prompt
__END__
Great. You should spend some time (a lot of time) playing with the syntax of print statements. We've now written a program that prints out multiple statements and uses correct syntax to separate them. What computer programing is more about though is taking advantage of variables. We can store something and reuse it over and over again. This is especially helpful because most computer programmers are lazy - very lazy. So if I was tired of typing the word "Hello" multiple times, I could instead have perl print out any greeting of my choice. The way you declare a variable in perl is with the dollar sign ($).
#!/usr/bin/perl
use strict;
use warnings;
 
my $greeting = "Hello"; #this is a variable!
 
 
#Print out to the screen a greeting
print "$greeting, intro-to-programming for bioinformatics\n";
print "$greeting, especially to Lenny!\n";
 
#End the program and return to the prompt
__END__
While this is more work to type the first time, it's less work if we want to change our greeting multiple times.

Enough with the greetings, we can also use variables to do math. Perl syntax for this is really similar to Excel.
#!/usr/bin/perl
use strict;
use warnings;
 
my $first_number = 12;
my $second_number = "3";
 
my $sum = $first_number + $second_number;
 
print $sum, "\n";
 
__END__
You can get pretty fancy with math in perl
#!/usr/bin/perl
# Author        :
# Date          : Mon Aug  6 09:29:44 UTC 2007
# Description   :  Math is also awesome!
 
use strict;
use warnings;
 
 
my $first_number = 12; #This is a variable
my $second_number = "3"; #this is also a variable
 
my $sum = $first_number + $second_number; #here is another variable defined by the sum of the first two variables
 
print $sum, "\n";
 
$sum = $sum + $sum;
 
print "the sum is now $sum, suckers!\n\n";
 
__END__
You can play around with the other math functions during the exercises and by reviewing "Learning Perl". You can make comparisons between values stored between variables. This will become especially important when we are learning about control structures.

The final aspect of programming that I want to talk about before you start the exercises is interacting with users. You've already learned about sending output to standard out via print statements. Now I will show you how to get information into your program without editing it and we'll go back to greeting.
#!/usr/bin/perl
use strict;
use warnings;
 
#it's a good idea to ask the user for input
print "what's your name?";
 
my $name = <STDIN>;
 
print "hello $name, nice to meet you\n";
 
__END__
Wait a minute... I didn't put a \n after my variable $name. Why did it skip a line? It skipped a line because it includes the ENTER in the input. To get rid of that we need to include another function called chomp gets rid of trailing newline (and only a trailing newline. We can see it in this example:
#!/usr/bin/perl
use strict;
use warnings;
 
my $name = "jaime\n";
print "hello $name, nice to meet you\n";#output includes trailing newline
chomp $name;
print "hello $name, nice to meet you\n";#chomped away
$name = "jaime2";
chomp $name;
print "hello $name, you are so cool\n";#chomp doesn't delete anything but a trailing newline\n
__END__
you can read multiple lines of user input by reading <STDIN> multiple times
#!/usr/bin/perl
# Author        :
# Date          : Mon Aug  6 09:34:02 UTC 2007
# Description   :
 
use strict;
use warnings;
 
 
#it's a good idea to ask the user for input
print "what's your name?";
 
my $name = <STDIN>; #this will wait for a user to input
 
print "what is a greeting?";
 
my $greeting = <STDIN>; #this will wait again
 
chomp $name;
chomp $greeting;
 
print "$greeting $name, nice to meet you\n";
 
 
__END__

EXERCISES

1: The Greeter

  • Write a program that asks for the user's name and wishes them a happy new year.

2: Is that rude?

  • Ask for their year of birth and compute their age.
  • Print out their age in the following format:
Your age is 25.

3: Ratio

  • Read two numbers in (user input, from STDIN)
  • Print out their ratio.
  • Figure out how to make this program fail.

4: Sum and Mean

  • Read five numbers in.
  • Print out their sum and mean.

5: Swap


  • The user enters two numbers.
  • Store the numbers in two variables called "$input1" and "input2"
  • Swap the values of the two variables so that $input1 has the value of $input2 and vice versa.
  • Print out the two variables.

Advanced Topics
-What is the difference between enclosing a print statement in double and single quotes? Hint: try and print out the value of a variable ($variable) in a single quote vs. double quote print statement
-Try doing exercise 4 using only a single variable.

SOLUTIONS