Depending on who you talk to, Perl either stands for "Practical Extraction and Report Language" or "Pathologically Eclectic Rubbish Lister."
Larry Wall created Perl while trying to generate reports for a Usenet-like system that he'd hacked together for his employer. The tools available to him at the time, such as awk, just wouldn't quite cut it. So he created a general-purpose tool, the first version of Perl.
Perl has since evolved into an advanced high-level, object-oriented programming language. It is essentially an interpreted language, although there is a compiler for it shipped with the latest version. Larry Wall has also created a version that will compile to Java bytecode, so that it will run under any JVM.
For more information about Perl, take a look at the Springfield.pm web site. We'll be developing a list of links to other web sites, book titles, and more there in the very near future.
First, please note that the language is called Perl (note case). The interpreter, or the actual program used to run Perl code, is called perl.
If run with no arguments, perl will read a script from stdin. If you pass a filename as an argument on the command line, perl will try to use that file as the script to execute. Any subsequent arguments are used as arguments to the script.
On Unix (or Unix-like, including Linux) systems, you can also execute Perl scripts as you would any other script. First, be sure that the first line of your script looks like
#!/usr/bin/perl(Substitute the actual path to perl if it is installed somewhere other than /usr/bin.) Write the script to a file, then use chmod +x filename to make it executable. You can then run the script by simplying giving the path to it, i.e. ./filename.
Perl scripts can also be run on the command line. The primary option that is used to run scripts on the command line is -e, which specifies that the next command-line argument should be parsed as the script. For example, the "Hello, world!" script listed later could be run as
perl -e 'print "Hello, world!\n";'See the perlrun(1) man page for all of the possible command-line options.
The syntax of Perl most closely resembles that of C (and other C-like languages, like C++ and Java). Statements are terminated with a semicolon (";"). Whitespace is mostly optional. Blocks are surrounded by curly braces ("{" and "}"). Unlike C, comments in Perl begin with #, as they do in the shells (and most other Unix scripting languages).
Here's an example of a simple Perl script:
To run the script, simply save it to a file (i.e. one called hello), make it executable (with chmod +x hello), and run it (with ./hello). The output will look like#!/usr/bin/perl -w print "Hello, world!\n";
Hello, world!
Note that the script will actually be run with the command /usr/bin/perl -w (as specified on the #! line). The -w tells perl to turn on some extra warnings. It quite often will catch code that is syntactically correct, but won't do what you probably meant.
The single statement in the script, print "Hello, world!\n";, obviously prints the specified string to stdout.
The basic data types in Perl are the scalar, the array (or list), and the associative array (or hash). A scalar is simply a number, a string, or a reference to some other data type. An array is simply an ordered list of scalars. An associative array is like an array, but the scalar values are indexed by some other scalar value. (If none of this makes sense, don't worry. It will all be explained later.)
The basic data types are represented by a special character, then the variable name. For example, a scalar named "foo" would look like $foo, a list named "foo" would look like @foo, and a hash named "foo" would look like %foo.
The scalar is the basic datatype of perl. (Lists and hashes are both more complicated datatypes constructed from collections of scalars.) A scalar can be a string, a number, or some other similar piece of data.
One of the great features of perl is that scalars automatically "become" whatever they need to be. As an example, take the following simple script:
#!/usr/bin/perl -wThe output of this script look like
$foo="10ab";
print $foo+1, "\n";
print "$foo\n";
$foo+=1;
print "$foo\n";
11Note that initially $foo was set to the string value 10ab. The print statement outputs the value of $foo+1 (followed by a linefeed character). The next statement prints the value of $foo, just to show that the value hasn't changed. The next statement adds 1 to the value of $foo, which changes the value to 11. That is then printed out.
10ab
11