Database Reference
In-Depth Information
• If the library file is to be used only by your web server, install it in a server library
directory and make it owned by and accessible only to the server user ID. You may
need to be root to do this. For example, if the web server runs as wwwusr , the
following commands make the file private to that user:
# chown wwwusr mylib
# chmod 600 mylib
• If the library file is public, you can place it in a location that your programming
language searches automatically when it looks for libraries. (Most language pro‐
cessors search for libraries in some default set of directories, although this set can
be influenced by setting environment variables as described previously.) You may
need to be root to install files in one of these directories. Then you can make the
file world readable:
# chmod 444 mylib
Now let's construct a library for each API. Each section here demonstrates how to write
the library file itself and discusses how to use the library from within programs.
Perl
In Perl, library files are called modules and typically have an extension of .pm (“Perl
module”). It's conventional for the basename of a module file to be the same as the
identifier on the package line in the file. The following file, Cookbook.pm , implements
a module named Cookbook :
package Cookbook ;
# Cookbook.pm: library file with utility method for connecting to MySQL
# using the Perl DBI module
use strict ;
use warnings ;
use DBI ;
my $db_name = "cookbook" ;
my $host_name = "localhost" ;
my $user_name = "cbuser" ;
my $password = "cbpass" ;
my $port_num = undef ;
my $socket_file = undef ;
# Establish a connection to the cookbook database, returning a database
# handle. Raise an exception if the connection cannot be established.
sub connect
{
my $dsn = "DBI:mysql:host=$host_name" ;
my $conn_attrs = { PrintError => 0 , RaiseError => 1 , AutoCommit => 1 };
Search WWH ::




Custom Search