SITEMAP HOME
SIGN IN | FAQs | CONTACT
4Domains Web Hosting and Domain Registration Secure Shopping
Account Login   WebMail   Live Chat

Web Hosting

Domain Names

FREE Toolbar

Domain Auction

E-Commerce

Internet Tools

Business Solutions

Web Hosting Packages   Web Hosting Comparison   Web Hosting Technical Support   Web Hosting Resellers   Domain Name Resellers   My Account
DOMAINS: WWW.
Domain Resellers
Tell a Friend Recent Articles Bookmark
Home > Ntguide > Chapter7




Previous Section
Table of Contents
Next Section

7 Perl

Perl is a high-level programming language written by Larry Wall, based upon the C programming language. Perl`s ability to process, file, and manipulate text make it well suited for tasks involving quick prototyping, system utilities, software tools, system management tasks, database access, graphical programming, networking, and World Wide Web programming. You can find more resources for ASP located at Chapter 22


7.1 Perl Basics

Perl is a scripting language that is similar to ASP. However, pure Perl does not allow you to just switch to outputting HTML as you can in ASP. We are going to create a Perl example that will create the same output as Example 1 Hello World. To do this, we would need to create the following Perl File.

Example 6 Perl Hello World


    print "Content-type: text/html\n\n";
    print "<html>\n";
    print "<head>\n";
    print "<title> BLUEHILL.com Hello World! Perl Example</title>\n";
    print "</head>\n";
    print "<body bgcolor=#FFFFFF>\n";
    print "<p>From all of BLUEHILL.com Staff we would like ";
    print "to say,</P>\n";
    print "<h2>welcome to BLUEHILL.com NT Solutions!</h2>\n";
    print "</body></html>";

We place the above code, named hello_world.pl, in our cgi-bin directory. It is important that the file ends in .pl and that it is placed in the cgi-bin directory. Unless you do both, you will not be able to execute the program. In Example 6 Perl Hello World, the first line is the most important one because it tells the client that the output is to be in HTML format. If this line were not included, we would get the following error:


    CGI Error
    The specified CGI application misbehaved by not returning a complete set of HTTP headers. The headers it did return are:
    bad header test!

We can also get an error that the page cannot be displayed. This error may make it appear that the server is not available, when in fact we simply forgot to return the http headers.

The other major difference is the use of the character combination \n. This character combination is used to output a new line. Now that we can output a simple script, let’s try to get data from our clients.


7.2 Getting HTML Form Data

We can use Perl to get the same type of data from our clients as we did in ASP. When we are allowed to interact with our clients, our site comes alive. We are going to now see how to get the same data as we did in Example 4 Name Demo HTML.

To accomplish this we need are going to need an HTML file that will submit data to our Perl file. We can create a similar one as before. We need to create the following, named name_demo_perl.html:


    <html>
    <head>
    <title>BLUEHILL.com ASP Name Demo</title>
    </head>
    <body bgcolor="#FFFFFF">
    <p>Welcome to the Name Demo</p>
    <form method="post" action="name_demo_perl.pl">
    <input type="text" name="inputname">
    <input type="submit" name="Submit" value="Submit">
    </form>
    </body>

    </html>

This file is very similar to the one that we previously created. We have made a few minor changes. We can just put this file in the same directory as the example above. Now that we have created this file, we are now ready to create our Perl script that will take the user’s data. To do this we need to create the following Perl file and place it in our cgi-bin directory.


  1. use CGI;
  2. $wp = new CGI;
  3. #Retrieve the submitted data and assign it to a variable
  4. $inputname = $wp->param (‘inputname’);
  5. print $wp->header;
  6. print "<html>\n";
  7. print "<head>\n";
  8. print "<title>Name Demo Results</title>\n";
  9. print "</head>\n";
  10. print "<body bgcolor=#FFFFFF>\n";
  11. print "<h1>Welcome $inputname !</h1>\n";
  12. print "<p>Click <a href=/examples/pl/name_demo_perl.html>here</a>";
  13. print " to try again.</p>\n";
  14. print "</body></html>";

We have created a new Common Gateway Interface (CGI) object that will allow us to get data from the client. In Line 1, we inform Perl that we will be using external objects. In Line 2, we create a new CGI object and assign it to the variable wp. All variables used by Perl begin with the $ character. Line 3 assigns the value passed from the form element named inputname and assigns its value to the variable inputname. Line 4 then prints out the html header that we had printed before. In Line 11, we see a normal string printing out, but the string contains a reference to our variable inputname. Perl allows us to directly write out our variables value inside the string. If we meant to print out “$inputname” then we would have to change line 11 to read:

    print "<h1>Welcome \$inputname !</h1>\n";

Perl requires us to put an escape character, ‘\’, in front of special Perl characters. When placed in front of the Perl variable character, it tells Perl to print the character, rather then try to find a variable with the name of the text following the character. Below is a table that contains all the Perl escape characters.

Table 7.1

Perl Escape Characters



7.3 Perl Platform Differences

As you can see in the Perl Hello world example, there are some subtle differences between the Win32 platform and the Unix platform. One of the main differences is that we no longer need to specify the location of our Perl program (for example, #!/usr/bin/perl) in the initial string of the Perl script; we only need to end our file with the .pl extension.

Some of the differences between the Win32 and Unix platforms may cause problems when running your scripts. Below are some steps you can take to avoid problems associated with platform differences.

  1. Make sure you are using the script or module in the way it was intended and that the syntax is correct. If you are running a Unix-based client, you can install a Perl package that will help troubleshoot these types of problems. Windows 95/98/2000/NT users can install a free Perl program from Active State software (http://www.activestate.com).

  2. Check for calls to Unix functions that are not implemented in Perl for Win32. For a list of Unimplemented Perl Unix Functions, please refer to Table 7-2.

  3. Check for calls to standard library modules or CPAN modules that aren`t available on Perl for Win32, or that were custom installations.

  4. Check for system or backtick (``) calls to system tools that aren`t available on Perl for Win32.

  5. Check for reading and writing files that may be binary. For binary files, you will need to specify that the file is opened for binary access.

  6. Check for file system dependent paths and path name parsing that do not exist on the Win32 Platform.

  7. Check whether the module or script requires a specific version of Perl or another module.

  8. Remember that everything you take out, you will have to put in a work-around.

After following the above steps, try running your script through the debugger to see if what you did helps.

Table 7.2

Unimplemented Perl Unix Functions




Previous Section
Table of Contents
Next Section


Sitemap | Domain Names | Web Hosting | Web Hosting Comparison | Expired Domain Names
Why Us? | Corporate | Support | Contact Us | Affiliates | Drop Names | Add Favorite : Web Hosting
Hosting Terms | Domain Terms | Privacy Policy   Copyright © 2007 - 4Domains.com Inc.
DOTPound - Domain Name Auctions     DNS Tools


web hostinghostingdomaindomain namedomain namescheap web hostingecommerce website designdomain registration

Web Monitoring | SEO Technology | Hacker Scanner | DNS Tools | Online Backup