Week 1: Perl Intro

Example Code

  • Example code similar to what was done in class

    Today we start with the basics of perl. Remember - everything is case sensitive.

    Writing and Running Perl

    You should write your code in a plain text editor, and save it as a file that ends in .pl

    There should be no spaces in your file name. To run the code, at the command line type:

    perl file.pl Where file.pl is the name of your file.

    Comments

    Comments are a very useful part of perl. You can use them to make notes in your code or block code from executing. To make a comment, start with the # character. The computer will ignore the rest of that line. You can do whole line comments or start in the middle. It will always make the computer ignore the entire rest of the line: #this is a whole line comment print "yo!"; #this line will print the word yo

    Printing

    Here is the simplest first perl program we wrote: #!/usr/bin/perl print "Hello World!"; The first line tells the computer where to find the perl interpreter. It always starts with #! and then has the path of the program. The next line starts with print. That is used whenever we want to print something. It's followed by the text we want to print in double quotes. Finally, the line ends with a semicolon. Pretty much every line ends with a semicolon. Think of it like a period on a sentence.

    When we run this program it says:

    Hello World! There are some special characters we may want to add in. If we want a line break after our text, we add a \n inside the quotes. That adds a new line. print "Hello World!\n"; There are other special characters including \t (for tab). You can also use the \ as a delimiter for other characters. A delimiter indicates that the computer should treat the character after it in a special way. One way you may use this is if you want to print a double quote. You cannot just add it like this: print "Hello "World"!\n"; When the computer sees a ", it just searches for the next one to match it. In this case, the next one is the " in front of the word World. When it reaches it, perl thinks the string is done and doesn't know what to do with the text after it. If we want to actually print " characters, we delimit them by putting a \ before them. print "Hello \"World\"!\n"; It is important to remember that in perl, your statements should all be on one line. You can't break them in the middle like this: print "Hello World!\n"; However, if you want to print multiple lines of text, there is a special way. print qq+ This is a multi line printing +; In this example, perl sees the qq followed by a special character. It will print everything that comes after it until it sees that character again. Notice we have qq+ followed by some text and then a +; at the end. The + after the qq and the + at the end match.

    Variables

    Variables in perl are like variables in math. They store a value. For example, in math we may say x=3. Then, if I ask you what x+1 is, you know it is 4 because you substitute x with the value it stores. it's the same in programming, although you can store much more complex values. In perl, variables always begin with a $. For example: $x = 3; $age = 21; $name = "My name is Sam."; You can do mathematical operations when variables store numbers. We can add (+), subtract (-), multiply (*), divide (/), and do modular arithmetic (%). To remember the value of the operation, you should store it in another variable. For example: $x = 3; $x +1; $y = $x + 2; In the above example, $y will be equal to 5. The value of $x does not change because we did not store it. If we want to change the value $x stores, we can do this: $x = $x+1; This is a bit different than math where we would say the above statement is just wrong (because x is never equal to itself plus 1). However, when writing computer code, the computer looks at the right hand side of the equal sign, evaluates that, and then sets the left hand side equal to it. So if $x =3, the computer would first compute that $x + 1 is 4, and then reset $x equal to 4.

    Printing variables

    You can print variables in perl. There are many options: print $x; print "$x\n"; print "X is equal to $x\n"; print "X is still equal to " . $x . "\n"; Note in the second line that the variable is inside quotes. That is ok in perl. It will replace the variable with the value and print a new line after it. You can also add longer text, as shown in the third line. Finally, there is a more complex way of printing, where you can print strings of text, and concatenate (stick them together) with other text. The . is used to concatenate strings.

    User Input

    We briefly saw how to get user input. When we run the program, we can add arguments (aka inputs) to the end. perl file.pl 21 We can then get the values that come after the file name in the program. We do that like this: $userInput = @ARGV[0]; Now, the variable $userInput will store 21, the value I typed when I ran the program. We didn't discuss that weird right hand side of the expression, but we will in coming weeks. Just trust me for now.

    You can also prompt the user for input. This will stop your program and make it wait for the user to type in something and hit return. You can do that like this:

    $input = <>; It's a good idea to print something before that so the user knows what you want them to type, for example: How old are you? $age = <>; The variable $age in the above example will store their input. Note that it will have a newline character at the end that you might need to get rid of. To do that, you do: chomp $age;

    Control Flow

    Finally, we did some basic control flow. This lets you specify that certain code should only be executed under certain conditions. For example, if the user enters their age, we can print one thing if they are over 21 and another thing if they are not over 21. $age = @ARGV[0]; if ($age > 21) { print "Hi!\n"; } else { print "Go away.\n"; } We start with the word if. That is followed by a condition . The condition compares two values and needs to be either true or false when evaluated. The condition is always in parentheses. Then, the if statement is followed by a set of curly braces. The code in those braces will only execute if the condition is true. In the example, we will only print Hi! if the user is over 21. You can leave it at that, or you can add an "else" expression. The else gives code to execute when the condition is false. To do that, put the word else after the closing curly brace for the if code. The else is also followed by curly braces containing the code to print if the condition is false.

    The above example is simple, but you can put lots of code in those curly braces. We will do more of this as we progress.

    There are many comparison operators:

    • > greater than
    • < less than
    • >= greater than or equal to
    • <= less than or equal to
    • == equal to (note that this is TWO equals signs. one = sets the left hand value equal to the right hand value. == compares the values and returns true if they are the same or false otherwise)
    • != not equal to
    • eq equal to for strings
    • ne not equal to for strings (more about these two later