Databases Reference
In-Depth Information
are distributed with MySQL are written in Perl. Over the next three chapters, you'll
learn how to write simple Perl scripts that can be run from the command line, such as
the Linux or Mac OS X shell, the Windows command prompt, and CGI scripts that
run on a web server.
Command-line scripts are typically used to import data from other software or export
data from the database. For example, you can import data from a spreadsheet program
or export data from the database to a spreadsheet program.
You can also write command-line join applications to run reporting queries. For ex-
ample, using the GeoIP ( http://www.maxmind.com/app/perl ) database, you can write a
script that takes an IP address of a computer and looks up the country the computer is
located in.
You'll also see how to use Perl to access to your database from the Web. For example,
you can use the techniques you learn to design a music store application in Perl that
reads product data from a backend MySQL database and generates a web page con-
taining this information. Note that PHP is a more appropriate choice for new large-
scale web database applications.
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 ( # ) fol-
lowed 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 “Check-
ing 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 access to files and direc-
tories” in Chapter 2.
 
Search WWH ::




Custom Search