A Beginner's Introduction to Perl 5.10
by chromatic , Doug SheppardApril 23, 2008
First, a Little Sales Pitch
Editor's note: this series is based on Doug Sheppard's Beginner's Introduction to Perl. A Beginner's Introduction to Files and Strings with Perl 5.10 explains how to use files and strings, and A Beginner's Introduction to Regular Expressions with Perl 5.10 explores regular expressions, matching, and substitutions. A Beginner's Introduction to Perl Web Programming demonstrates how to write web programs.
Welcome to Perl.
Perl is the Swiss Army chainsaw of programming languages: powerful and adaptable. It was first developed by Larry Wall, a linguist working as a systems administrator for NASA in the late 1980s, as a way to make report processing easier. Since then, it has moved into a several other areas: automating system administration, acting as glue between different computer systems, web programming, bioinformatics, data munging, and even application development.
Why did Perl become so popular when the Web came along? Two reasons: First, most of what is being done on the Web happens with text, and is best done with a language that's designed for text processing. More importantly, Perl was appreciably better than the alternatives at the time when people needed something to use. C is complex and can produce security problems (especially with untrusted data), Tcl can be awkward, and Python didn't really have a foothold.
It also didn't hurt that Perl is a friendly language. It plays well with your personal programming style. The Perl slogan is "There's more than one way to do it," and that lends itself well to large and small problems alike. Even more so, Perl is very portable and widespread -- it's available pre-installed almost everywhere -- and of course there are thousands of freely-distributable libraries available from the CPAN.
In this first part of our series, you'll learn a few basics about Perl and see a small sample program.
A Word About Operating Systems
This series assumes that you're using a Unix or Unix-like operating system (Mac OS X and Cygwin qualify) and that you have the perl binary available at /usr/bin/perl. It's OK if you're running Windows through ActivePerl or Strawberry Perl; most Perl code is platform-independent.
Your First Perl Program
Save this program as a file called first.pl:
use feature ':5.10';
say "Hi there!";
(The traditional first program says Hello world!, but I'm an iconoclast.)
Run the program. From a command line, go to the directory with this file and type perl first.pl. You should see:
Hi there!
Friendly, isn't it?
I'm sure you can guess what say does. What about the use feature ':5.10'; line? For now, all you need to know is that it allows you to use nice new features found in Perl 5.10. This is a very good thing.
Functions and Statements
Perl has a rich library of built-in functions. They're the verbs of Perl, the commands that the interpreter runs. You can see a list of all the built-in functions in the perlfunc man page (perldoc perlfunc, from the command line). Almost all functions can take a list of commma-separated parameters.
The print function is one of the most frequently used parts of Perl. You use it to display things on the screen or to send information to a file. It takes a list of things to output as its parameters.
print "This is a single statement.";
print "Look, ", "a ", "list!";
A Perl program consists of statements, each of which ends with a semicolon. Statements don't need to be on separate lines; there may be multiple statements on one line. You can also split a single statement across multiple lines.
print "This is "; print "two statements.\n";
print "But this ", "is only one statement.\n";
Wait a minute though. What's the difference between say and print? What's this \n in the print statements?
The say function behaves just like the print function, except that it appends a newline at the end of its arguments. It prints all of its arguments, and then a newline character. Always. No exceptions. print, on the other hand, only prints what you see explicitly in these examples. If you want a newline, you have to add it yourself with the special character escape sequence \n.
use feature ':5.10';
say "This is a single statement.";
say "Look, ", "a ", "list!";
Why do both exist? Why would you use one over the other? Usually, most "display something" statements need the newline. It's common enough that say is a good default choice. Occasionally you need a little bit more control over your output, so print is the option.
Note that say is two characters shorter than print. This is an important design principle for Perl -- common things should be easy and simple.
Numbers, Strings, and Quotes
There are two basic data types in Perl: numbers and strings.
Numbers are easy; we've all dealt with them. The only thing you need to know is that you never insert commas or spaces into numbers in Perl. Always write 10000, not 10,000 or 10 000.
Strings are a bit more complex. A string is a collection of characters in either single or double quotes:
'This is a test.'
"Hi there!\n"
The difference between single quotes and double quotes is that single quotes mean that their contents should be taken literally, while double quotes mean that their contents should be interpreted. Remember the character sequence \n? It represents a newline character when it appears in a string with double quotes, but is literally the two characters backslash and n when it appears in single quotes.
use feature ':5.10';
say "This string\nshows up on two lines.";
say 'This string \n shows up on only one.';
(Two other useful backslash sequences are \t to insert a tab character, and \\ to insert a backslash into a double-quoted string.)
Pages: 1, 2 |

