The first step in learning to program is to teach the computer to say hello. With Perl, this is incredibly straightforward: you simply tell perl to execute the following code:
say "Hello World!"
The obvious question is: “How do I ‘tell perl’ to execute this code?” Depending on your operating system, you might need to download and install the ‘perl’ interpreter — a program which executes Perl code. (Note: uppercase “Perl” refers to the programming language while the lowercase “perl” means the interpreter itself.)
First, you should see if you already have perl installed and what version it is.
You’ll need to open a command-line terminal. On OS X, use the finder to launch Applications -> Utilities -> Terminal. On Ubuntu, this is Applications -> Accessories -> Terminal. On Windows, click Start -> Run, type cmd into the prompt, and click OK.
At the command-line, type
perl --version, hit Enter, and see if you get some output with a version number. If you get something like ‘perl’ is not recognized… or perl: command not found, or your version is less than 5.10.0, then you’ll need to download and install perl.
With perl 5.10 installed, you may now rush headlong into programming by running:
perl -E "say 'Hello World!'"
If your computer is a Mac or an older Linux distribution, you might already have perl 5.8.8 installed. This is a fine version of perl, but lacking some nice features which are only available in 5.10, starting with the
sayfunction and the
Before 5.10, this sort of thing would have been done with
perl -e 'print "Hello World!\n"'
This will still work in perl 5.10 (try it!) because new versions of Perl 5 never remove features. It also introduces the newline character and an important detail about quoting strings.
Now run the same command without the
\n and see what happens.
perl -e 'print "Hello World!"'
The output gets crowded by the new prompt because it has no final newline. (Tip: Hit Ctrl+C to get a fresh, uncrowded prompt in your terminal.)
This is called string interpolation, where double-quoted strings do nifty things like translating backslashed characters into some special kind of output.
Another fun example (until your coworkers get upset) is
print "\a" — the
\a is the “bell” character (it makes your terminal beep.)
Notice that the
say command in 5.10 is the same as
-E (instead of
-e) tells perl that you intend to use
say and other 5.10-only features in your program.