e-mail   
 Menu
  Home
  Download
  Top 10 Downloads
  Last 15 New Files
  Web Links
  Tips
  Last 15 New Tips
  NLM Programming
  Admins Club





SUPLA System
Internet of Things




Installation and Administration






Polish Forum SUSE


 
Who's Online

 There are currently,
4 guest(s)
that is (are) online.
 


Technical Information

Back to List of Categories

Technical Information about
  A Little YaST Magic
  A Slideshow for Your KDE Wallpaper
  Add Novell Support to Your Firefox Search Engine
  Adding To The Panels In Gnome.
  Akregator - RSS News Reader
  An Introduction to WBEM and OpenWBEM in SUSE LINUX
  Authenticating SUSE LINUX to eDirectory via LDAP
  Basic Mounting Differences between Windows and Linux
  Beginner's Guide to Using Novell SUSE LINUX's Build Utility
  Browsing Files
  Card Readers on SUSE LINUX 9.2 Professional?
  Changing the Default Browser in Gnome
  Developing Perl CGI scripts on SUSE LINUX
  Do I Have Permission?
  Double SYNC -- Using RSYNC Simultaneously With and Without Branch Office for Data Backup
  Dumb Question: Mounting Linux CDs
  Easy Command-Line Introduction
  Easy Way to Debug SSL Connections
  Enabling an Interactive Startup in Novell/SUSE LINUX
  Exploring MySQL 4.1 and PHP 5 on SUSE LINUX
  Extend Your Panels Using Gnome Drawers.
  Firefox Speed Tip
  Fix Java Errors With ConsoleOne On Linux
  Fix Your Mouse Wheel...
  Gimp -- Closing In on Photoshop...
  Gimp Tips Part 1: Resizing Images
  Gimp Tips Part 2: Rounding Corners
  Gimp Tips Part 3: Fading Photography Edges
  Gimp Tips Part 4: Selective Colorization
  Gimp Tips Part 5: Plugins a Plenty - Adding Light
  Gimp Tips Part 6: Using The Clone Tool
  Google Auto-fill Replica for Linux
  How a Corrupted USB Drive Was Saved by GNU/Linux
  How to Implement Login Scripts into a Pure Linux Environment
  How to install and connect an External Modem on a Linux PC
  How to Install the Novell Client for Linux on SUSE Linux 10.0
  How to launch applications at login in Gnome
  How to Remote Control Linux
  How to setup a Linux application to automatically startup when you login.
  How to share Files and Printers Between Linux and MS Windows with Samba
  How to uninstall the Novell Client for Linux
  How to upgrade the Novell Client for Linux
  How to use SMB without turning off the firewall
  How's the Weather?
  HTML Development Tools in Linux
  Informative Bash Prompt
  Install and Use QEMU on SUSE 9.2
  Install Linux Frequently, Without the Hassle
  Installing Apache, PHP, and MySQL on SUSE LINUX Professional
  Installing RPM Files
  Integrating Novell Linux Desktop into a Novell NetWare network
  Introduction to Oracle 10g R1 (10.1.0.3) on SUSE LINUX Professional 9.3
  iPrint Printers on Novell Linux Desktop
  Linux Scheduling Priorities Explained
  Make a Misbehaving Program Quit
  Manually Mounting a USB Flash Drive in Linux
  Migrating To Linux, Advantages Over Windows 9.x And XP
  Minimize Your Maximizing with Multiple Desktops
  Mounting NetWare Volumes On NLD Illustrated
  Move Your /home to Another Computer
  Mozilla Mail Review
  Need a Backup?
  Novell Client Settings with Windows Terminal Server
  Novell Linux Desktop Equivalents of Windows Software - New and Improved
  Novell Linux Desktop System Upgrade How-To
  Partitioning Hard Drive for Linux Install
  Perform Remote SuSE Installations With Virtual Network Computing
  Quick Intro to Klipper
  Quick Shortcuts in The Gimp
  Reset Your Lost Root Password
  Resizing Your Gnome / KDE Panels
  Runlevels in SUSE LINUX
  Running Existing Win32 Applications on SUSE LINUX
  Service Command in /etc/init.d/
  Setting Shortcut Keys
  Setting up a Linux NFS Install Source for Your LAN
  Should I choose Novell Linux Desktop or SUSE LINUX Professional as my desktop?
  Starting Services From the Command Line On SuSE LINUX
  Sticky Notes for Gnome
  SUSE LINUX 9.3 Professional Review
  Sync Up The Taskbar and the Desktop in Gnome
  System Upgrade How-To
  Taking Screenshots in Linux
  The Basics of Messaging in the Cross-Platform GroupWise Client
  The Novell Client for Linux (beta).
  Transparent Panels in Gnome
  Trick Out Your KDE Desktop
  Understanding the YAST Software Module Options
  Updating Your Software with Red Carpet
  Using dsbk on Linux and Unix
  Using iFolder to Synchronize Firefox Bookmarks
  Using Multiple Swap Partitions In 2.4
  Using openSSH to Securely Access Remote Systems
  What FTP client should I use on Linux?
  What Is Logical Volume Management?
  What is so great about Firefox?
  Which Graphic Formats For What?
  Windows Shortcuts for Firefox Tabs on Linux
  Windows to Linux: A Beginner's Guide
  Windows Users Can Feel at Home Exploring Files in Linux
  WLAN interface as the Default
  Work With User Selection Icons In KDM
  YAST Online Update De-Mystified

Technical Information
 Developing Perl CGI scripts on SUSE LINUX

Printer-friendly version

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

”;
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 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

”;
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 http://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.






Since 2003

Portal posiada akceptację firmy Novell Polska
Wszystkie materiały dotyczące produktów firmy Novell umieszczono za zgodą Novell Polska
Portal has been accepted by the Novell Polska
All materials concerning products of Novell firm are placed with Novell Polska consent.
NetWare is a registered trademark of Novell Inc. in the United States and other countries.
Windows is a trademark or a registered trademark of Microsoft Corporation in the United States and other countries.
Sybase is a registered trademark of Sybase Inc. in the United States of America.
Other company and product names are trademarks or registered trademarks of their respective owners.