Perl is the new-generation language that's starting to become quite popular among some of the tech elite. Although it's still far from knocking C off as the industry's de-facto programming language standard, Perl is acquiring a special audience of its own (particularly among those who program CGI scripts.)
Perl's syntax borrows from many things, including C, awk, sed, and some Unix command shells, among others. The result can sometimes be messy. Indeed, although basic Perl syntax superficially resembles C, Perl has the potential to become wildly and superlatively obscure, degrading into a series of gibberish-like punctuation marks and other symbols. Mercifully, that's not usually necessary, and I haven't learned how to do it yet anyway, so you won't find any of that here. :)
Actually, you won't find much anything here yet, since I haven't picked up much of the language. What follows isn't even a bare-bones primer; It's simply a demonstration of some of the most elementary functions of Perl.
Just before I begin: If you're wondering where to get a Perl compiler, Perl isn't usually compiled. It's an interpreted language, so you need a Perl interpreter. What's the difference? Compiled programs are compiled into an executable, which is then run. Interpreted programs are left as the source code without converting them into an executable. The source code is run through the interpreter each time the program is run. The disadvantage to interpreted programming languages like Perl is, you need an interpreter for Perl to run any program written in Perl. Go here to get ActivePerl, a cool free Perl interpreter.
Perl uses several operators. Some of these are slightly different from standard operators in any other major programming language, and so it's worthwhile to spend a few moments learning Perl's operators.
+ is used for addition,
- is used for subtraction,
* is used for multiplication,
/ is used for division,
and % is used for modulus (in which division is performed and the
remainer is used as a result).
So far, the basic arithmetic functions seem to exist in their standard form. But now we deviate from the norm a bit...
** is used for exponentiation. Fair enough.
. (yes, a period) is used for string concatenation. That's right, Perl uses a separate operator for this. Most programming languages use the same plus-sign operator for both mathematical addition and string concatenation, relying upon the variable type to decide whether to add or concatenate. Perl does not do this, mainly because it cannot do this, since it does not have separate variable types to distinguish between numeric and string variables. Thus, it is necessary for the programmer to specify when addition or concatenation must be performed. In Perl, "extempo" . "raneous" equals "extemporaneous".
There are also several of the typical combination-style operators that do things in shorthand. For example, the following line...
$a += 3;
...Means to add 3 to the variable called $a. The following line...
$line .= "!!!";
...Will add three exclamation marks to the end of the $line variable.
Boolean logic operators:
&& is logical AND
|| is logical OR
! is logical NOT
!= is NOT EQUAL TO
& is bitwise AND
| is bitwise OR
~ is bitwise NOT
^ is bitwise XOR (EXCLUSIVE OR)
You can also use the textual representations of these, so you could type $a & $b or $a AND $b. That's about it for Perl's operators; Now let's see some code!
The following is Perl's "Hello, World" program.
print "Hello, world!";
As you can see, Perl's print command combines the best of BASIC's PRINT with C's printf: It doesn't use those weird parentheses like printf does, it's print and not printf (because it's stupid to suffix a function with an "f" just because it's a function), it doesn't have to be uppercase like BASIC, and Perl ends command lines with semicolons (like C), instead of assuming that the end of each program line is the end of the command. This means that if you happen to have one really long, hairy command, you can split it into several lines.
This is how you make subroutines with Perl, and also how you call them:
thisisthesub(); thisisthesub(); sub thisisthesub { print "This gets printed twice!\n"; }
...And this is how you define variables, do math with them, and display them:
$a = 34; $b = 17; $c = $a + $b; print $c;
This is how you use if. (It works just like the if in C.)
$a = 16; $b = 4; if($a == 16) { print "This'll get printed because a matches."; } if($b == 9) { print "This won't get printed because b doesn't match."; }
And here's how Perl's while works. (Again, it's just like C's while.)
$a = 5; while ($a > 1) { $a = $a - 1; print $a; }
Here's the basics of doing console I/O with Perl:
print "\nPlease enter your name: "; $name = <>; print "Please enter your age: "; $age = <>; #The "chop" command removes the last character from a string. We must use it #here because a newline character is at the end of both strings, which would #mess us the output. Chopping both string variables keeps all the output on #one line. chop $name; chop $age; #If a dollar sign is used in a string, perl automatically assumes that it is #part of a string name, not that you want to actually print the dollar sign. #If you really want to print a dollar sign, use the \$ escape sequence. print "\nHello $name, you are $age years old!\n";
Well, that's all for now... Go wild! If you want to know still more about Perl (and who doesn't?), check an excellent Perl tutorial here.