Contact:[email protected]
Mastering Perl: Part One In Love, Anything is Possible. For Everything else, there is Perl By Anders Brownworth Okay, eveyone. Pay attention. A most intensive, and intensely interesting, class is about to begin. Perl may stand for "practical extraction and report language," but trust us�it definitely is not as boring as that sounds! In fact, Perl has actually become the de facto standard glue language for UNIX. You might as well get used to saying, "Oh, that can be done with Perl," because that's usually the case! About its only downside is its speed or, more accurately, its lack thereof. Sure, working in Perl can be a tad slow, but for the overwhelming majority of tasks Perl is used for, it's more than fast enough. In short, it rocks! Of course, the real problem is, "How does one get over the learning curve?" This, oddly enough, is both Perl's chief strength and its chief weakness. As you learn the Perl ropes, you'll soon discover that Perl is extremely concise and features many simple ways to do fairly complicated things. In other words, it's handy but cryptic. Stick with it, do your homework diligently, and you'll be as happy as the proverbial clam. Our own resident Linux stud and this month's professor, Anders Brownworth, is back again for your reading pleasure, and he's going to take you through several example scripts to get you up to speed. Make sure you're sitting in front of your favorite Linux machine, and try each and every one of the examples he provides. Because Anders won't be able to take you personally in all the directions you might want to go, you should familiarize yourself with the Perl interpreter, too. Now, Let's get started! Lesson One: Sample scripts Perl is an interpreted language. This means that you make little text files and "execute" them by sending them through the Perl interpreter. Break out your favorite text editor (such as emacs) and create a file called "sample.pl" and enter the following program. (Note: I usually use the extension ".pl" to denote perl scripts, but you can use whatever you want.) #!/usr/bin/perl print "World domination, one line of perl at a time.\n"; Now make that file "executable" and run it by typing its name. eyore:~> chmod +x sample.pl eyore:~> sample.pl World domination, one line of perl at a time. eyore:~> (Note: If execution fails with a "command not found," then your default path may not include the current directory. Execute the program like this: "./sample.pl" or check whether Perl lives in /usr/bin/perl or somewhere else.) When you "run" this script, the shell takes notice that sample.pl is an executable text file, so it reads it to process the contents as if they were shell commands. All modern shells will notice that the file starts with "#!" so they give up trying to interpret it and launch the named program, sending the rest of the file to it as input. In our case, they launch /usr/bin/perl and send our script (all of one line) to it for execution. That's the first line of sample.pl. The script is just a simple print statement that just does exactly what you would expect: It prints whatever you type between the quotes. Don't forget: Perl statements end in a semicolon ";". Many times, syntax errors crop up because you didn't put a semicolon somewhere. Don't forget: What is the "\n" all about? "back n" stands for the special character "newline," or the same thing as hitting the enter key. Sniglet: Line endings The standard UNIX line ending is a "newline" character ( \n ), but DOS and Windows terminate lines with the two characters "newline linefeed" ( \r\n ). This really won't matter to you until you need to start reading Windows files with your Perl scripts. Now, students, let's make it a little more interesting. Let's add a variable. #!/usr/bin/perl $name = "Anders"; print "Your name is $name?\n"; $name is a variable that we're using to store my name. We could also set this variable on the fly: #!/usr/bin/perl print "What's your name? "; $name =; chomp $name; print "You wouldn't happen to be $name?, would you?\n"; (Note: Chomp is a quick little function that lops off the last character in a string if it is a newline or whitespace character. Notice the lack of a \n character in the first print statement. That is done because the user presses enter after entering their name.) Or we could store a number in a variable: #!/usr/bin/perl $number = 5; $square = $number * $number; print "The square of $number is $square\n"; Back to the Index