Perl Babysteps 1: Your First Simple Scriptby webfiend (Vicar)
|on Dec 30, 2004 at 00:10 UTC||Need Help??|
Note: This copy of the tutorial has been going stale as Perl moves on. There is another descended from the same material on my own site. I haven't decided yet whether to update this page and add the material that has been added to Perl Babysteps tutorial since then.
This page is intended to provide the non-programmer with a gentle introduction to the Perl programming language. When you are done with it, you should feel ready to learn more. You will not be an expert, but you will be able to find the information you need to go farther. Beginners and experts alike should feel free to send suggestions about how to improve this tutorial.
It is very easy to find and install a copy of Perl for your operating system. How easy? Well, it's a single simple download, and you may already have it on your machine!
Well, okay. If you are using a Windows machine, you probably don't already have Perl on your machine. But it's easy to get. You even have a couple different versions to choose from.
This is the officially blessed version of Perl for Windows. It is released by ActiveState. ActivePerl can be downloaded for free, or you can order the ActiveCD from them. It comes with a wealth of widely used third-party libraries such as Tk, LWP, and the XML bundle. Whether you choose to download or purchase, here is your best starting point:
Perl on Cygwin
On the other hand, maybe you prefer to have Windows with a touch of UNIX on top. First, I want to let you know that Perl is perfectly happy under Windows these days. But if you insist, or just happen to be curious, Perl is also available for Cygwin. Just rerun Cygwin's setup.exe program, and make sure that "Perl" is checked. Oh, and add an editor while you're at it, too. Maybe Vim or Emacs.
You probably already have it, especially if you happen to be on a UNIX-based operating system such as Linux, FreeBSD, or Mac OS X. Seriously. Try the which command if you don't believe me.
That'll teach you to doubt me. Just kidding. If you don't have it installed, you'll get a message along the lines of perl not found in .... Double-check to see if it's on your installation media. For OS X folks, you just need to install the Developer Disk.
The Macho Geek Option: Compile From Source
Whatever operating system you are on, this is a valid choice. Still, there are a lot of options when building. It can be more than a little overwhelming. I would suggest you go with one of the available binary downloads while you're just starting out.
Creating Perl Programs
The tradition in programming literature is to start by creating a program that prints a simple phrase, such as "Hello, World!" The idea is to give you some clue how much work is involved in creating a minimal program. I, for one, am not going to argue with tradition. Not this one, at least. Type the following into your text editor:
Save the file as hello.pl. We will run it in a few moments, but first, let's take a quick look at what we've got so far.
On each line, everything from # to the end of the line is a comment. Perl ignores comments, so they allow you to communicate with other people who read your code. Comments are very very good. When you come back to look at a complex script after a few months, you might forget what some block of code does, or why you chose one solution over another. Having the comments there help to remind you what you were intending, and generally serve to make it much easier sorting everything out.
I like to start all of my scripts off with a quick header to describe the purpose of the program. Here is a rough template:
All we have is the name of the file and a brief description of what the program does. That is often all that you need, especially for simple scripts like this one.
NOTE: The official documentation system for Perl is POD, or "Plain Old Documentation". It is powerful and widely used, but it is also beyond our scope for this stage of the tutorial. However, to give you a little taste of how much easier POD makes life, here is the first sentence displayed after you type the command perldoc -f print at the command line:
Prints a string or a list of strings.
You should read POD in 5 Minutes if you are curious to learn about how to format POD documentation for reading with the perldoc command.
Now you would probably like to know how to actually run your program. Save the file you have been editing and switch to a command line. Make sure you are in the same directory as your script - this should be as simple as cd project_directory. Once you are in the right place, type the following into the command line:
Oh, what was the "\n" at the end of the string for? That is a fair question. It's a special code for the newline character. What's the newline character do? It's easier to show you what happens when you don't have it:print "Hello, World!";
Save it and run it.
Adding a newline character in the string tells Perl that you want to print a new line. Now do you see?
All this is kind of cool, but it would be nice to customize it a little bit. Maybe we could change the program so that it says "Hello" to us personally.
Now you might notice the semicolon at the end of each line. What? You noticed them before? Well, you're quicker than me. Perl recognizes the semicolon as the marker which ends one statement. So each statement must be terminated by a semicolon. Putting them on separate lines like that is something that Perl doesn't care about, but you might lose friends if you tried to put everything on one line.
Now -- what? Another question? Okay, go ahead. What's that my doing? We use the word my to declare variables. Declaration is when we tell Perl that we have a variable we plan on using. The language doesn't require declaration, but it is good form, and you will find your code easier to debug later on as you learn more about clean programming.
What's a variable? We'll get to that in a second. I'm impatient to see a running program! Save the file, and run it again.
There, I feel better. Let's move on to talking about variables.
We stored the string "Brian" in the variable name. A variable is basically just something you want the computer to remember so that you can get to it when you want it later. You can get a lot more complicated than that if you want, and a lot of programmers do. However, this definition should hold us over for a long time.
The "$" symbol at the beginning tells Perl what kind of information this variable holds. Perl basically has two kinds of variables:
When you start digging in, you will see how broad of a generalization I've just made. Still, I think that this will last us until the end of this article, and the basic idea holds true for a lot of Perl programming.
Anyways. Individual things are also called scalars by people who think that you don't have enough things to remember. Ah well, we'll get used to it. To their credit, "scalar" is a shorter term than "individual thing". Perl makes it easy to recognize a scalar, by making you prefix all scalar variables with a dollar sign. So $name is "the scalar variable name", or even "the individual thing called name". Eventually many Perl hackers end up calling it "dollar name".my $name = "Brian"; # My scalar variable called 'name' has the value "Brian"
Having a program that displays the exact same message every time you run is nice when it comes to being consistent, but not so entertaining as a program. "What does it do?" "It prints out my name." "Oh." See? Boring. Let's make things a little more interesting. We could change the value of name in the code, but it might be a little tiresome to do this before showing it to each new person. How about making the program ask for a name? User interaction - a neat idea.
We only made one small change, but I'll admit that there's a lot going on with that one change. Here's the short version:
<STDIN> gets input from the user that you can save in a variable.
There's a long version. Feel free to skip it if you just want to get on with it. The long version works out like this:
The scalar $name is assigned the value of <STDIN> . <...> tells Perl that we want it to read a filehandle and hand the results back to us. A filehandle is a source of information. It could be an open file, but in this case it is STDIN. STDIN is the standard input stream - geek talk for "wherever we expect user input to be coming from". Most of the time, STDIN just means "keys the user enters from the keyboard". The result of reading from the filehandle (in this case, STDIN, which is in this case the text you entered from the keyboard) is stored in $name.
There's a really long version, but I'm getting bored so we'll skip it for now.
Hey, what happened? The exclamation is all the way on the next line!
Calm down. Everything is going to be okay. Perl just counts the ENTER key at the end as a part of the input, and ENTER prints out as a newline ('\n' if you've been paying attention). Counting the newline is fine for reading text from a file, but it's a little inconvenient when you are handling input from the keyboard. Fortunately, there's the chomp function. What's it do? Let's ask perldoc -f chomp. On second thought, let's not. You can go right ahead, but I just did, and discovered that the docs for chomp are a little dense for today. Let's try mine:
chomp removes the last character from a string, but only if that character is whitespace or a newline.
Let's give it a try.
You already know what chomp does, so running this script shouldn't provide any surprises:
... and we're done!
Nice work! You have begun to learn Perl by writing a complete program which gets input from a user and prints output including a modified version of their input. Stop for a minute and think about it. Yes, there is much more to learn, but you have dipped your toes into the pool. Now you can go out there and start learning about the huge and wild world of Perl programming.
Here are a few links to resources that you can use while learning about Perl and how to use it for your own nefarious purposes.
There are a lot more, but I can't recall many of the links right now. I may put them up as time goes on.
Update:(30 Dec 2004)
Threw in some suggested links from kutsu, too.
Update:(26 Jan 2005)
Update:(9 May 2006)