Introductory Perl 
1/5 

If you're going to learn this wonderful langauge, you're going to have to learn how to start it. Perl is not a compiled language like C or Java, nor is it quite like a shell or batch script. There's a lot more going on for Perl, which can mean either a lot of control for you, or a lot of confusion.

Here are links to some especially important components of your briefing:
Starting Perl
Variables
Control Structures
Subroutines
More...

Q
U
I
C
K

L
I
N
K
S
Starting Perl

Perl scripts are just ordinary text files. The compilation happens automagically behind the scenes. This is normally considered a good thing because it allows you, the programmer, to focus on things like the newest spy techniques rather than scratching your head at some obscure linking error. With that in mind, however, there is a step required to make it all happen (ironically, called invocation); the computer doesn't know what a Perl program is, but if you tell it where to find the Perl language interpreter, it will happily pass off the task of dealing with the file to it. In Unix, files are identified by special lines that start with #! at the start of the file. This is called the she-bang line and tells your shell what program to use to intrepret the file. For Perl scripts your line should look something like:

#!/usr/local/bin/perl

The location varies according to system, but on all of C&C's UA machines, that's where you find the Perl binaries. If you're totally against thos symbols, or just find it confusing, you can do it all manually by specifying the path to the perl interpreter on the command line and passing the script filename as an argument, like this:

homer01% perl yourscriptname

This could be very useful if you, for instance, wanted to test whether or not a script had syntactic or typographical errors without actually executing (which might be dangerous if you're not sure if it's right, or just time consuming). Perl has a nifty switch that you can use to do just that. If your script has no obvious errors, it works like this:

homer01% perl -c ascript
ascript syntax OK

On the other hand, if you made some sort of horrendous mistake, it might look like this:

homer01% perl -c badscript
some specific errors
...

badscript had compilation errors.

Perl can also be started with the -w flag for additional diagnostic information. Do this.

from the Perl man page:

BUGS
The -w switch is not mandatory.
A first Perl program

The canonical first task when learning any programming language is to output the text "hello world." No one really knows why, and everyone so inclined has simply stopped fighting it. Thankfully, in Perl this is a fairly simple task, so it won't be too painful.

Code

ResultDownload
#!/usr/local/bin/perl
print "hello world";
hello world


helloworld.pl

The first line is simple; it's no surprise that a perl script would include this information. The second, however, employs the print statement which is new. Like you might think, this displays the text following it on the screen. The text is quoted so that it won't be misunderstood as instructions to the interpreter.
Also worthy of note is that the print statement ends in a semicolon while the she-bang line doesn't. This is because the print statement is an actual instruction, while the she-band line is treated as a comment (and ignored) by the intperpreter because the line begins with a #. You could actually put anything you want after a # and (as long as it's not on the first line) it won't break anything.