What Is Perl?
Perl is an acronym, short for Practical Extractionand Report Language. It was designed by Larry Wallas a tool for writing programs in the UNIX environment and iscontinually being updated and maintained by him.
For its many fans, Perl provides the best of several worlds. Forinstance:
- Perl has the power and flexibility of a high-level programminglanguage such as C. In fact, as you will see, many of the featuresof the language are borrowed from C.
- Like shell script languages, Perl does not require a specialcompiler and linker to turn the programs you write into workingcode. Instead, all you have to do is write the program and tellPerl to run it. This means that Perl is ideal for producing quicksolutions to small programming problems, or for creating prototypesto test potential solutions to larger problems.
- Perl provides all the features of the script languages sedand awk, plus features not found in either of these two languages.Perl also supports a sed-to-Perl translator and an awk-to-Perltranslator.
In short, Perl is as powerful as C but as convenient as awk, sed,and shell scripts.
Perl is very easy to learn. Indeed, if you arefamiliar with other programming languages, learning Perl is asnap. Even if you have very little programming experience, Perlcan have you writing useful programs in a very short time.
How Do I Find Perl?
To find out whether Perl already is available on your system,do the following:
- If you are currently working in a UNIX programming environment,check to see whether the file /usr/local/bin/perl exists.
- If you are working in any other environment, check the placewhere you normally keep your executable programs, or check thedirectories accessible from your PATH environment variable.
If you do not find Perl in this way, talk to your system administrator and ask whether she or he has Perl running somewhere else. If you don't have Perl running in your environment, don't despair-read
on!
A Sample Perl Program
Now that Perl is available on your system, it's time to show you a simple program that illustrates how easy it is to use Perl. Listing 1.1 is a simple program that asks for a line of input and writes it out.
Listing 1.1. A simple Perl program that reads and writes a line of input.
1: #!/usr/local/bin/perl
2: $inputline = <STDIN>;
3: print( $inputline );
$program1_1
This is my line of input.
This is my line of input.
$
Line 1 is the header comment. Line 2 reads a line of input. Line 3 writes the line of input back to your screen.
The following sections describe how to create and run this program, and they describe it in more detail.
Running a Perl Program
To run the program shown in Listing 1.1, do the following:
- Using your favorite editor, type the previous program and save it in a file called program1_1.
- Tell the system that this file contains executable statements. To do this in the UNIX environment, enter the command
$ chmod +x program1_1
- Run the program by entering the command
$ program1_1
When you run program1_1, it waits for you to enter a line of input. After you enter the line of input, program1_1 prints what you entered, as follows:
$ program1_1
This is my line of input.
This is my line of input.
$