Writing Your First Perl Program

In this section, we’ll take a very quick look at the Perl language. A Perl script is simply a text file containing statements that the Perl interpreter reads and executes. As with most things, the best way to learn is by doing, so we’ll walk you through your very first Perl script. Open a text editor following the instructions in Using a Text Editor” in Chapter 2 and create a text file containing the following lines:

#!/usr/bin/perl
print "Hello,\nworld!\n1\t2\t3\n";

The first two characters on the first line should be the pound or hash symbol (#) followed by the exclamation mark symbol (!). Together, these two characters form the shebang or hash-bang marker that tells the shell how to run the script. Immediately after these two characters, specify the path to the location of the Perl interpreter (called perl) on your system. If you’re not sure where this is, check the instructions in Checking Your Existing Setup” in Chapter 2. Save this text file as HelloWorld.pl in your current directory; you can exit your editor if you wish.

On a Linux or Mac OS X system, add the executable permission for the user who owns the file (you) using the chmod program. Here, we grant read, write, and executable permissions for the owning user, and no permissions to the group or other users:

$ chmod u=rwx,g=,o= HelloWorld.pl

You need to do this only once for a file; the permissions don’t change if you edit or move the file. We discuss permission settings in Restricting ...

Get Learning MySQL now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.