Although it is not the easiest language to learn, once you are comfortable using associative arrays, regular expressions, and other keys feature of Perl, you will find that it is very easy to write short but powerful scripts. It is said that Perl programs are generally shorter, easier to write, and faster to develop than corresponding C programs. They are often less buggy, more portable, and more efficient than shell scripts. According to www.perl.org, Perl is the most popular web programming language. Hopefully you now have a sense of why all this might be true.
Table 22–3 lists some of the most important Perl functions introduced in this chapter. Details about these functions, and many more, can be found in perldoc perlfuncs. Table 22–4 summarizes the special characters used in Perl scripting.
Function | Use |
---|---|
| Print a string |
chomp | Remove terminal newlines |
my | Declare a local variable |
reverse | Reverse the order of characters in a string or elements in a list, or swap the keys and values in a hash |
push, pop | Add or remove elements at the end of an array |
unshift, shift | Add or remove elements at the beginning of an array |
sort | Sort a list in ASCII order |
keys, values | Get a list of keys or values for a hash |
if, unless | Conditional statements |
while, until | Loop while a condition is true (or until it becomes true) |
foreach, for | Loop through the elements in a list |
defined | Check whether a variable has a value other than undef |
open, close | Open or close a filehandle |
die | Exit with an error message |
sub | Define a procedure |
return | Exit from a procedure, returning a value |
Symbol | Use | Symbol | Use |
---|---|---|---|
# | comment | <> | Read input from a filehandle |
$ | scalar variable name | $_ | Default variable |
@ | array name | @_ | Values passed to a procedure |
$# | last index in an array | // | Enclose a regular expression |
% | name of a hash | ! | Not |
& | procedure name | &&, || | And, or |