Приглашаем посетить
Брюсов (bryusov.lit-info.ru)

15.1 Using server environment and system functions

Table of Contents

Previous Next

15.1 Using server environment and system functions

15.1.1 Environment variables

We have examined some Perl scripts and their applications in Chapters 13 and 14. In this chapter, we will concentrate on how Perl can be applied to some popular interactive CGI applications including "Online Examinations and Marking," "Seat Reservations," "Search Engine," and "Database." In fact, the structure and scenario of the study is not limited to Perl script. The idea of the Common Gateway Interface or CGI, roughly speaking, is an abstraction about Web applications on a server. Perl is just a fashionable operating process to achieve that purpose. In parallel to the study and examples of this chapter, other CGI scripting languages such as PHP and ASP can also be implemented in a similar manner.

As a quick reminder of using Perl, the first line of each Perl script in this book is



#!/usr/bin/perl

This is a classical header for Perl script in the UNIX/LINUX environment. The Perl interpreter is assumed to be stored in the directory /usr/bin. If your Perl interpreter is located in another directory, you may need to change the header to reflect the location of the interpreter. If you are using a Microsoft system with IIS server and a Perl interpreter, you may use the line above as it is. For a Microsoft system with the Apache server, you may need to include the drive and location of the interpreter.

In the study of CGI applications, environment variables are an important subject. They are variables defined by the operating system for any application and program at any time. Usually, they hold information that may be useful to commands or programs running within the system.

Whether you are working in the Microsoft or UNIX/LINUX server environment, you can use Perl script to access environment variables with the statement $ENV{'variable'}. For example, the following Perl script fragment can be used to locate the window's directory in a Microsoft system:



#!/usr/bin/perl
print "Content-type:text/html\n\n";
print "The window directory is $ENV{windir} \n";
print "if you are using a Microsoft system";

If you are using a UNIX/LINUX system, the variable $ENV{'pwd'} will give you the current working directory. The entity %ENV is a collection of all environment variables.

Different systems may have different environment variable names. Furthermore, some applications such as Web server software can define their own environment variables. To see the environment variables on your server, consider the following Perl script:



Example: ex15-01.pl - Environment Variables With Perl

 1: #! /usr/bin/perl
 2: print ("Content-type:text/html\n\n");
 3:
 4: print << "mypage";
 5:  <?xml version="1.0" encoding="iso-8859-1"?>
 6:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 7:  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 8:  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 9:  <head><title>Example: ex15-01.pl</title></head>
10:   <style>.txtSt{font-size:14pt;color:#ffff00;font-family:arial}</style>
11:   <body style="background:#000088">
12: <table width="550" cellspacing="5" align="center" class="txtSt">
13: mypage
14:
15: foreach $var (sort keys %ENV)
16: {
17:    print "<tr>";
18:    print "<td>$var</td>";
19:    print "<td>$ENV{$var}<td>";
20:    print "</tr>";
21: }
22: print "      </table>";
23: print "   </body></html>";

After the CGI header, lines 413 define a here document to display part of an XHTML document. The collection of all environment variables %ENV is implemented as a hash function (or hash table). A hash function contains keys and values. For example, a typical %ENV on a system with Apache is represented internally by a hash function that looks like this:



%ENV = (
 DOCUMENT_ROOT     =>"/apache/htdocs"
 GATEWAY_INTERFACE =>"CGI/1.1"
 ...         ...         ...
 SERVER_SIGNATURE  =>"Apache/1.3.23 Server at www.pwt-ex.com Port 80
 )

The left hand side of the arrow => is the key and the right hand side contains the value. For example, the variable $ENV{'DOCUMENT_ROOT'} stores the value "/apache/htdocs." Therefore the phase (sort keys %ENV) in line 15 can be used to sort all the keys in %ENV. The statement in line 19 displays the value of the associated keys. A simple foreach loop is used to complete the XHTML table. A screen shot of the page running on the Apache system is shown in Fig. 15.1. A screen shot of the same page running on Windows XP with IIS is shown in Fig. 15.2. Note that in some earlier Windows systems, IIS is referred to as the Internet Information Server.

Figure 15.1. Environment variable on LINUX system with Apache

graphics/15fig01.jpg


Figure 15.2. Environment variables on Windows XP systems

graphics/15fig02.jpg


Another feature of Perl is the ability to call system functions. Almost any system functions can be called directly.

15.1.2 Calling system functions

In general, Perl allows you to call and run system functions, commands, and even other scripts. For example, you can run a command and send the result to the screen by



print 'command';

where the command is the name of the executable command or program to be executed by the Perl script. This feature is useful to run programs including shell commands as well as other Perl or shell scripts. For example, the following is a Perl script fragment to display the current date and time and no external function call is needed:



#!/usr/bin/perl
print "Content-type:text/html \n\n";
print "The Date and Time is =";
print scalar(localtime());

In a UNIX/LINUX system, the date function is usually located in the directory /bin/date. In this case, you can replace the last line by



print '/bin/date';

This will call the system function date and display the result in the browser window.

One of the important applications of system functions is to manipulate files. For example, you can print out the contents of a file in an UNIX/LINUX environment by



cat filename

where filename is the name of the file you want to print. For a Microsoft system, the command is type filename. The Perl statement



print 'cat filename';

will cause the contents of the file name to be sent to the browser screen. For example, suppose you have header and signature files named



/home/public_html/head.htm
/home/public_html/sig.htm

You can assign the contents of the header and signature files to variables such as $head and $sig and then use the variables in documents. Consider the following example:



Example: ex15-02.pl - Calling System Functions

 1: #! /usr/bin/perl
 2: print ("Content-type:text/html\n\n");
 3: $head = 'cat /home/public_html/head.htm';
 4: $sig = 'cat /home/public_html/sig.htm';
 5:
 6: print << "mypage";
 7: $head
 8:   Thank you very much for returning the form. <br />
 9:   Your name will be added into our database and <br />
10:   we will send you our newsletter every week!
11: $sig
12:
13: mypage

This Perl script contains two variables, $head and $sig. The system function in line 3, 'cat/home/public_html/head.htm';, reads the contents of the file and stores it in $head. This variable is then used in line 7 to be displayed on the browser screen.

This script also contains a signature file attached at the end of the page. Using variables allows you to make changes relatively easily. In particular, if many documents refer to the same $head and $sig, only the referencing file needs to be changed or modified. After execution of the statement in line 3, the variable $head contains the following XHTML page:



Listing: ex15-01.txt - Web Page : head.htm

 1:  <?xml version="1.0" encoding="iso-8859-1"?>
 2:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 3:  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 4:  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 5:  <head><title> </title></head>
 6:  <style>.txtSt{font-size:14pt;color:#ffff00;font-family:arial}</style>
 7:  <body style="background:#000088;font-family:arial;
 8:      font-size:18pt;color:#ffff00">
 9:  <div style="text-align:center">Header Page: head.htm</div><br />
10:  <img src="line1.gif" width="500" heiht="6"><br /><br />

Similarly, the variable $sig contains the following XHTML page:



Listing: ex15-02.txt - Web Page : sig.htm

1: <br /><br />
2: <img src="line1.gif" width="500" height="6"><br /><br />
3: From www.pwt-ex.com
4: </body>
5: </html>

When these two files are embedded into the Perl script as listed in ex15-02.pl, they form a completed XHTML document which is returned to the browser. In practice, you can put whatever you like in the header file representing the nature of your Web site. This is a handy technique to run a Web site with a large number of pages or directories sharing the same header and logo.

In order to use system function calls, you need to know what kind of system is on the server and what kinds of system functions are available. For example, the Perl script ex15-02.pl will return errors if run on a Microsoft server system such as Windows NT/2000/XP with IIS. For such a system to work, you may need to change the system call to



$head = 'type head.htm';
$sig = 'type sig.htm';

The command type is a proper function to display the contents of a file on Windows systems. A screen shot of this example is shown in Fig. 15.3.

Figure 15.3. ex15-02.p1

graphics/15fig03.jpg


This technique will be used to build a guest book feature later in this chapter.

    Table of Contents

    Previous Next