Novell is now a part of Micro Focus

Developing Perl CGI scripts on SUSE Linux

Novell Cool Solutions: Feature
By Gordon Mathis

Digg This - Slashdot This

Posted: 23 Mar 2005
 

Introduction

Perl is a scripting language that was developed by Larry Wall which is available on virtually every computer platform today from Apple to Linux. Perl is an acronym for "Practical Extraction and Report Language". It is an interpreted language that is optimized for string manipulation, I/O, and system tasks. It has gained recent attention in the explosion of the World Wide Web as a quick and effective way to develop applications that provide much of the web's interactivity. Because there are so many tutorials on the web about Perl the focus of this article will be how to configure SUSE Linux for Perl CGI scripts and teaching you the basics of developing and debugging a Perl script.

Setting up Apache and MySQL

(Note: This section requires root user privileges)

Basically the only required setup for CGI scripts on SUSE Linux is installing Apache. MySQL is not required for Perl scripts, but because the sample I will have you do later in the article requires MySQL, I am going to have you install it at this time. If Apache or MySQL was not installed as part of the original install you will need to install them using the YaST installer. If you are not sure if they are installed you may want to use the Runlevel Editor under the System option in YaST and see if they are available to start. If you determine they are not installed, select Install and Remove Software from the Software option and then you can either select Selections from the Filter, and check Simple Webserver which will install Apache2 , PHP, MySQL, along with many other packages or you can select Search from the Filter and select the products shown below:

Search for apache. Select the following packages:

    apache2 Apache web server (version 2.0)
  apache2-prefork     Apache 2 "prefork" MPM (Multi-Processing Module)

Search for mysql and selecting the following packages:

    mysql SQL Database Server
  mysql-client       MySQL Client

If you had to install Apache2 and MySQL make sure you go to the Runlevel Editor under the System option and enable apache and mysql, or if you prefer you can start them manually by issuing the start command for the following applications:

Apache
/etc/init.d/apache2 {start | stop | restart}

MySQL
/etc/init.d/mysql {start | stop | status | reload | restart | try-restart | force-reload}

Disable the firewall

While you are in the YaST utility you need to disable the firewall so that the Apache Webserver can be seen by your browser. Select the Firewall under the Security and Users option and select the Stop Firewall and Remove From Boot Process radio button, then click the next buttons and then the OK button. You can re-enable the firewall later if you want.

Create file directories and grant permissions

For security reasons the Apache documentation recommends that you don't grant write permissions to any user or group of users to the /srv/www/htdocs or /srv/www/cgi-bin directories. Instead it recommends that you create a subdirectory and grant write permissions to that directory. Using the YaST utility select Edit and create groups under the Security and Users option. Click the Set Filter button and select System Groups. Select the www group from the list of groups and click the Edit button. Select the check box for your <user name> from the Members of this Group list. Click the Next button, and then the Finish button. From a Terminal Window switch to the root user and create a misc directory under the /srv/www/cgi-bin directory (mkdir /srv/www/cgi-bin/misc). Change the group ownership of the /srv/www/cgi-bin/misc directory from root to the www group (chgrp www /srv/www/cgi-bin/misc). Assign read/write/execute permissions for the owner and group of the misc directory (chmod 755 /srv/www/cgi-bin/misc). Now do the same for the /srv/www/htdocs directory.

mkdir /srv/www/htdocs/misc
chgrp www /srv/www/htdocs/misc
chmod 755 /srv/www/htdocs/misc

Creating your first Perl CGI script

At this point you are ready to begin scripting and we will begin by creating the equivalent of a hello world program. Each Perl script starts with the shebang #!<location of the Perl executable> which in the case of SUSE is /usr/bin/perl. This is the location of the Perl executable that will be used to execute the script. Another important command is the Content-type which in the case of a CGI script needs to indicate that this is a text/html script. If this were just a script that you intended to run at a command prompt on the server this would not necessarily be required. Using a text editor create a test.pl file in the /srv/www/cgi-bin/misc directory and add the following content:

#!/usr/bin/perl

print "Content-type: text/html\n\n";
print "< html> ";
print "< body> ";
print "< h2> I just wrote my first web page using Perl!< /h2> ";
print "< /body>";
print "< /html> ";

Now set the file permissions so that it is executable (chmod 755 test.pl).

We want to run our script from a browser, but before doing so it is a good idea to check your script for syntax errors. Like any other programming language something as simple as forgetting a semicolon at the end of a line could generate an error message. In almost all cases that syntax error message displays as a Error 500 message in the browser window which is not much to go on. If you run Perl with the -wc option (-w for enable warnings and -c for check syntax only) it will check your script for syntax errors and give you the line numbers of any syntax errors. The window below shows a typical error message for a simple syntax error encountered in a Perl script.

The following is the output received from a test.pl script that was missing a semicolon:

perl -wc test.pl
syntax error at test.pl line 7, near "print"
test.pl had compilation errors.

Then once the semicolon was added the script was run again with the following output:

perl -wc test.pl
test.pl syntax OK

Once the syntax is OK it is also a good idea to run the script again without -wc and look for any dependency errors. Just because the syntax in the one file is OK doesn't mean the script will run. Missing scripts (.pl or .cgi files) or dependencies will cause a 500 Error message on the the output of the browser as well. If all syntax and dependencies are OK you should see the source of HTML scroll across the screen much the same as if you were to view the source on a page in the web browser.

Once everything tests OK from a command prompt window go ahead and open a web browser and browse to the following URL http://localhost./cgi-bin/misc/tes.pl.

If you view the source for this page you will notice that everything is on one line which can be difficult to read especially when your script gets much larger. You could edit each individual print statement and add a \n at the end which would make it more readable, but would also require a lot of work. Another option in Perl is to place static HTML between a special print command that outputs HTML as is. This can save you a lot of time by not requiring you to escape quote marks, and other special characters. Basically this command allows you to output your HTML without making any changes.

print <<ENDHTML;
....your HTML code here.....
ENDHTML

Whenever Perl encounters a print <<[some tag name] statement it simply outputs everything as is including line feeds and carriage returns until it encounters the closing tag name. Using this special print command our first script would look something like this:

#!/usr/bin/perl

print "Content-type: text/html\n\n";
print <<ENDHTML;
<html> 
    <body> 
        <h2> I just wrote my first web page using Perl!</h2>
    </body>
</html>
ENDHTML

Notice that there are a lot less print commands and if you view the source in the browser it is easy to read. Using this special print command it is extremely easy to take existing static web page and add dynamic content to it by simply outputting the majority of the page without making any changes.

Video Store example

To help demonstrate how easy Perl scripting is I have written a little video store example called SampleFlicks that accesses a MySQL database and allows you to navigate through a small selection of videos. Image files included with this sample are from AMG and are being used for training purposes only, see the terms of use at www.allmovie.com.

To install this on your SUSE Linux system I will walk you through creating a movies database and copying a few files to the correct locations. Required files for this project are located at http://forge.novell.com/modules/xfref_library/detail.php?reference_id=1884 in a sampleflick.zip file.
Simply unzip the contents of this file to a directory (unzip sampleflick.zip) and copy files to the following locations:

flick.pl file to /srv/www/cgi-bin/misc
cgi-lib.pl to /srv/www/cgi-bin/misc
graphics directory to /srv/www/htdocs/mics/
pictures directory to /srv/www/htdocs/mics/

Notice that the graphics and pictures directories are copied to the /srv/www/htdocs as opposed to the /srv/www/cgi-bin directory. Apache only looks for the image and supporting files in the htdocs directory. flick.pl is the main script, but it requires the cgi-lib.pl for parsing HTML form parameters instructing the script what action to take next.

Database

Obviously the MySQL database which is named movies plays a very important part in this project and consists of the following tables:

Table Fields Description
movies id, title, rating, minutes, year, actors, genre, media, available, picture, link, synopsis, id, name The main table
genre id, name Drama, Action, Comedy
media id, name VHS, DVD, VHS and DVD
ratings id, name G, PG, PG13, R, NR
new id The movie id's of new releases
coming   id The movie id's of movies coming soon (not used)
sale id The movie id's of movies for sale (not used)

Installing the movies database

Using the SUSE Linux YaST utility select the Runlevel Editor from the System menu item and ensure that MySQL is enabled.

From a command prompt window make sure that the MySQL database service is running by typing mysql -u root at a Linux command prompt (by default the root user does not have a password). This command will execute the MySQL monitor. You can type show databases; to see the available databases. By default you should see a mysql, and a test database. Type exit to exit the MySQL monitor.

Because this article is focused on Perl, and not necessarily on MySQL, I am going to provide you with the complete database (movies.sql) which is simply a dump of my database and will allow you to create your own movies database in one simple step. From a command prompt enter the following and the database will be created and populated. In addition it also creates a new MySQL user named admin with a password of novell and grants this user all rights to the movies database.

Server@Linux:~> mysql -u root < movies.sql

If you don't see any error messages after issuing this command the movies database is created and you can use the MySQL monitor to verify that the database and tables have been created, or if you have followed the instructions and are feeling lucky you can simply open a web browser and browse to http://localhost/cgi-bin/misc/flick.pl.

Perl Script

Notice that the flick.pl script is not very long and the majority of the script is just plain HTML. Also notice that I used the special print command so that I could just cut and paste from the tool I used to help design the HTML. However, I do break the script at several points to retrieve data from the database. Because the MySQL functions are such an important part of this script it may be good to review some of the functions used.

MySQL Functions

Notice that before you use any of these functions, you need the line:

use mysql; 

in your script, above the use of the functions. Usually this is at the top of the script.

Connecting to the MySQL Server

$db = mysql->connect($host, $database, $username, $password);

$db is the pointer to the specific MySQL connection for the duration of the script.

Notice $db is treated as a global and I only do this once for the entire script. You can have multiple calls to mysql->connect, but you must use a different variable each time.

Selecting the Database

$db->selectdb($database);

Notice that we are using the $db since it is our link to the database connection. $database is the name of the database that we are accessing. $db->selectdb requires a string, which can be directly inserted or put into a variable.

Querying the Database

$query = $db->query($sql_string);

The variable $sql_string contains the SQL syntax for the database query. Here is a simple query from the script which simply gets the id and name from the genre table and sort it by the name.

$query = $db->query("SELECT id, name FROM genre ORDER BY name");

The variable $query now contains the identifier for the query, and will be used from now on for accessing that query's data.

Retrieving the data

There are several ways to have the data from the query returned.
while(@array = $query->fetchrow) {
       print "<A classs=side_nav_link href='movie.pl?genre_id=".@hash
       [0]."'>".@hash[1].
               "</A>";
}

The fetchrow function returns a single row of data from your query in an @array variable, starting at an offset of 0. Calling fetchrow again would return the values of the next row, and so on.

while (%hash = $query->fetchhash) {
        print "<A classs=side_nav_link href='movie.pl?genre_id=".$hash
        {'id'}."'>".$hash
                {'name'}."</A>";
}

Notice that you use the prepare function to build a statement variable and then the execute function to execute the SQL string. Although the movie sample didn't insert any data into the database another good little exercise would be to create another script to add additional movies in which case you would use the prepare and execute functions with a insert statement that looks something like this.

$st = $db-prepare("INSERT INTO Movies VALUES
(0,'$title','$rating','$minutes','$year','$actors','$genre','$media',
 1, '$picture', '', '$synopsis') ");

$st->execute();

Conclusion

Well there you have it, everything you need to start developing Perl scripts on SUSE Linux. As you can see SUSE Linux comes pre-installed with Perl and creating a CGI script is really quite easy. If you have existing static web pages that you want to add dynamic content to, Perl can be a great way to go. Obviously this short article is not intended to make you an expert in programming Perl, but instead to get your system configured and teach you a few basics of writing a Perl script. At this point you should refer to the hundreds of tutorials on the web for help with Perl scripting language and learn as you develop.

SampleFlick's Novell Forge Porject
http://forge.novell.com/modules/xfref_library/detail.php?reference_id=1884


Novell Cool Solutions (corporate web communities) are produced by WebWise Solutions. www.webwiseone.com

© Copyright Micro Focus or one of its affiliates