| Home | General Help | Email Help | Web Help | Software | FAQ's | Misc | Contact | ||
| Home » Web Help » Perl/CGI on Unix | ||
|
Perl/CGI on Unix
Can I use perl? Yes. Perl files must end in the ".pl" extension. Furthermore, these scripts must be placed in a directory in your public_html directory called cgi-bin. Click here for instruction on creating the cgi-bin. The cgi-bin directory must have its permissions set to 755 and the perl scripts contained within must also be set to 755. Click here for an explanation of this Here is an example Perl/CGI script #!/usr/local/bin/perl -w use CGI qw(:standard); print header(); print "<center><b>Hello World!</b></center>";Click Here to see the output from the above script Click Here to view some Perl/CGI tutorials NOTE: Perl/CGI scripts will only run from stuweb.cms.gre.ac.uk Perl Example using MySQL Below is a Perl/CGI script which accesses the MySQL database and displays some information contained in a table in the database.
#!/usr/local/bin/perl -w
use CGI qw(:standard);
use DBI;
my $serverName = "studb.cms.gre.ac.uk";
my $serverPort = "3306";
my $serverUser = "wug01";
my $serverPass = "******";
my $serverDb = "mdb_wug01";
my $serverTabl = "test";
$dbh = DBI->connect("DBI:mysql:database=$serverDb;host=$serverName;
port=$serverPort",$serverUser,$serverPass);
my $SQL = <<"EOT";
select * from test
EOT
my $cursor = $dbh->prepare($SQL);
$cursor->execute;
print "
<html>
<head>
<title>Perl MySQL Example</title>
</head>
<body>
<p> Here are the machines in the DB </p>
<hr>
<table>
<tr><th>Machine Names</th></tr>\n";
my @columns;
while ( @columns = $cursor->fetchrow ) {
print ( " <tr>",( map { "<td>$_</td>" }
@columns ) , "</tr>\n");
}
print " </table>
<hr>
<p> ... and that's it!</p>
</body>
</html>
";
$cursor->finish;
$dbh->disconnect;
Click Here to see the output from this script I want to use a perl module that is not installed in the system Perl If you want to use a Perl module that is not installed in the system Perl, what you have to do is install it locally in your home area and then tell the perl executable where to find that module in your Perl script. The standard way to do this is to include the following line at the beggining of your Perl script use lib "/path/to/perl/module"; Here is an example of the standard procedure to install Perl modules locally in your home area. Create a directory in your home area called perllib % mkdir ~/perllibIn the directory where you have downloaded the Perl module, unzip and untar the source file. % gzip -d PerlMod-1.0.tar.gzChange into the untarred directory. % cd PerlMod-1.0In this directory, there should be a README file and an INSTALL file along with the source for the actual Perl Module. Read the README and INSTALL files before carrying out the following steps! To make the makefile you must do % perl Makefile.pl PREFIX=~/perllibThe PREFIX specifies where the Perl Module will be installed (i.e. The perllib directory you created in your home area) Once this has completed successfully, you can run % makeYou will now be able to call this module by inserting the use lib statement in your Perl scripts. Here is an example Perl script for the user wug01 using the Module we have just compiled. #!/usr/local/bin/perl -w use lib "/home/wug01/perllib"; use PERLMOD::Hello; my $mod = new PERLMOD::Hello; my $message = $mod->myMethod(); print "The output was $message\n"; Example Perl/CGI program taking data from an html form Below is an example of how to take data from an html form and send it to a perl script for processing. Here is the code for the form. Here is the code for the perl script.<form method="post" action="/cgi-bin/perlForm.pl"> Please Enter A Fruit: <input type="text" name="fruit"> <input type="submit" value="submit" name="submit"> </form>
Fill in the box below with a fruit to see this script in action.
I want to debug my Perl/CGI script. Can I look in the Apache error logs? The best way to debug your Perl/CGI scripts is to run them from the Unix command line. Any errors in your script will be output to the screen giving a more detailed analysis than would be contained in the Apache error logs. So, for a script named myScript.pl you would type the following command at the Unix shell prompt. % perl myScript.plHere is an example using the helloworld.pl script shown above. This time we will insert an error into the code by mis-spelling the print statement on the last line. Here is what we get when running this script on the command line.#!/usr/local/bin/perl -w use CGI qw(:standard); print header(); prit "<center><b>Hello World!</b></center>"; % perl badworld.pl
Unquoted string "prit" may clash with future reserved word at badworld.pl line 7.
String found where operator expected at badworld.pl line 7,
near "prit "<center><b>Hello World!</b></center>""
(Do you need to predeclare prit?)
syntax error at badworld.pl line 7, near "prit "<center><b>Hello World!</b></center>""
Execution of badworld.pl aborted due to compilation errors.
So, here we can see that there is an error on line 7 (including the blank lines) and that the perl
executable doesn't understand the word "prit".
If you need to simulate passing parameters to the script - such as would be collected from an
HTML form for example, you can do so by doing the following.
% perl myScript.pl arg1=foo arg2=barNOTE: A very common error in Perl/CGI programs is not including a correct content header for the browser to parse. One way to make sure you are printing out the correct content header (for html) is to import the CGI module as above and do a print header() statement. E.g. #!/usr/local/bin/perl -w use CGI qw(:standard); print header(); # IMPORTANT!! print "<center><b>Hello World!</b></center>"; I need to test my code to see if it sends mail. Click Here to read the FAQ |
|||||||||||||||||||||||||||||||||
| Home » Web Help » Perl/CGI on Unix | ||
| Home | General Help | Email Help | Web Help | Software | FAQ's | Misc | Contact | ||