Web Solutions-Open Source CPAN 561
Lecture 1: An introduction to Perl:
Practical Extraction and Reporting Language is an object-oriented language with similar syntax to C/ C++ or JAVA. It was designed as a tool for writing programs in the UNIX environment.
Perl is an interpretive language; it does not require a special compiler and linker. We have to write the program and tell Perl to run it.
The Perl interpreter executes the Perl programs you write. If an error was deducted in the program, then Perl interpreter displays an error message.
Perl programs consist of a series of statements; one is executed at a time. Each statement must end with (;) and consists of a collection of tokens, which can be separated by white space.
The first line in a Perl program should indicate to the system that this is a Perl program. Under UNIX this line look as:
#! /usr/local/perl/bin
The first character in the line, the # character, is the Perl comment character. It tells the system that this line is not an executable instruction. The ! character is a special character; it indicates what type of program this is. The path /usr/local/bin/perl is the location of the Perl executable on your system. This executable interprets your program.
Under Windows this line look as:
#! C:/perl/bin/perl
Assuming that you have installed Perl on “C:”.
Perl has library in functions that perform certain predefined tasks. print function for example writes to the standard output file. Library functions are passed chunks of information called arguments; these arguments tell a function what to do.
If you are developing a standalone Perl application then <STDIN> is used to get user input from the standard input (the keyboard) as shown in the following line of code:
$var=<STDIN>;
But if you are developing a Perl script then you have to use CGI standard input in your script to be able to access data sent from the browser as described in the following example:
use CGI qw(:standard);
$name=param(‘username’);
Where username is a string passed to the script from an HTML form.
Scalar Variable:
A scalar variable is memory storage for exactly one item of data, it can store any standard Perl type: a number, a character string or a reference. Items that can be stored in scalar variables are called scalar values. The name of a scalar variable consists of the character $ followed by at least one letter, which is followed by any number of letters, digits, or underscore characters.
Perl variables are case-sensitive. The $ character is necessary because it ensures that the Perl interpreter can distinguish scalar variables from other kinds of Perl
Scalar variables have no fixed or static type associated with them. There type depends on current values they store. It is not necessary to pre declare scalar variable, but we may do through my qualifier. For example:
my($var1,$var2);
If no value is assigned to a scalar variable, Perl gives it the value of undef, which is represented by a zero when used as a number and “” when used as a string.
A Perl character string type can be single or double quoted, double quoted characters strings are subject to string substitution. For example:
$var1=’John’;
$var2=’hello\n $var1n’;
$var3=”hello\t $var1 ”;
the content of $var2 would be:
hello $var1\n
whereas the content of $var3 would be
hello John
Escape sequences:
Following are the escape characters used with double quoted strings:
Escape character / Meaning\n / New line
\r / Return
\t / Tab
\f / Form feed
\b / Backspace
\v / Vertical tab
\a / Bell
\e / Escape
\\ / Backslash
\” / Double quote
\l / Make the next letter lowercase
\L / Make all the next letters lowercase until \E
\u / Make the next letter uppercase
\U / Make all the next letters uppercase until \E
\E / Terminate \L or \L
Numeric operators:
Operator / Action+ / Addition
- / Subtraction
* / Multiplication
/ / Division
** / Exponentiation
% / Modulus
< / Less that
<= / Less that or equal to
== / Equal
>= / Greater that or equal to
> / Greater than
!= / Not equal to
String operators:
Operator / Action. / Concatenate
eq / Equal
ne / Not equal
lt / Less than
le / Less than or equal
gt / Greater than
ge / Greater than or equal to
x / String repetition
As an example of string repetition :
$var=”t” x 5;
this is the same as :
$var=”ttttt” ;
To run Perl script , you need to install Apache under Windows. See Module 1-2 for details on how to download and install Apache web server under Windows.
Examples:
Ex1: first.cgi
#! C:/perl/bin/perl
print "Content-type: text/html\n\n";
print "Hello World";
Then we can run this program either from the command line or from a browser.
To run it from the command line under Windows:
perl first.cgi
Following is a screenshot of the output:
To run it from the a browser:
http://www.localhost/cgi-bin/first.cgi
where first.cgi must be saved under cgi-bin folder of Apache.
Following is a screenshot of the output:
Ex2: mathEx.cgi
#!c:/perl/bin/perl
print "Content-Type: text/html\n\n";
my ($val1, $val2,$result);
$val1 = 10;
$val2 = 4;
print "<html>\n <title> \n math example \n </title>\n";
print "<body>\n";
$result = $val1 + $val2;
print "<br> $val1+ $val2= $result <br>";
$result = $val1 - $val2;
print "<br> $val1- $val2= $result <br>";
$result = $val1 * $val2;
print "<br> $val1* $val2= $result <br>";
$result = $val1 / $val2;
print "<br> $val1/ $val2= $result <br>";
$result = $val1 ** $val2;
print "<br> $val1 to the power $val2= $result <br>";
$result = $val1 % $val2;
print "<br> $val1 % $val2= $result <br>";
print "<br>\n";
print "</body>";
print "</html>";
Following is a screenshot of the output:
Ex3:strings.cgi
#!c:/perl/bin/perl
print "Content-Type: text/html\n\n";
$firstStr ="John";
$secondStr="Smith";
$thirdStr=$firstStr ." ". $secondStr;
print "<html<head<title>strings</title</head<body>";
print "$thirdStr\n";
print "<br>";
print "name in upper case :\U $thirdStr ";
print "<br>";
print "name in lower case :\L $thirdStr ";
print "</body>";
print "</html>";
Following is a screenshot of the output:
Ex4: env.cgi
#! c:/perl/bin/perl
# this example will print a list of the environment variables
print "Content-type: text/plain\n\n";
foreach $var (%ENV)
{
print "$var\n";
}
The following screenshot shows part of the output:
Ex5: envdetails.cgi
#! c:/perl/bin/perl
print "Content-type: text/html\n\n";
print "<html<head<title>environment variables </title</head<body>";
print "Here is a detailed list of the environment variable <br>";
print "<pre<b>QUERY_STRING</b> :$ENV{'QUERY_STRING'}<br>";
print "<b>SERVER_ADDR</b> :$ENV{'SERVER_ADDR'}<br>";
print"<b>HTTP_ACCEPT_LANGUAGE</b>:$ENV{'HTTP_ACCEPT_LANGUAGE'}<br>";
print "<b>SERVER_PROTOCOL</b> :$ENV{'SERVER_PROTOCOL'}<br>";
print "<b>HTTP_CONNECTION</b> :$ENV{'HTTP_CONNECTION'}<br>";
print "<b>SERVER_SIGNATURE</b> :$ENV{'SERVER_SIGNATURE'}<br>";
print "<b>REMOTE_PORT</b> :$ENV{'REMOTE_PORT'}<br>";
print "<b>REMOTE_ADDR</b> :$ENV{'REMOTE_ADDR'}<br>";
print "<b>HTTP_ACCEPT</b> :$ENV{'HTTP_ACCEPT'}<br>";
print "<b>HTTP_USER_AGENT</b> :$ENV{'HTTP_USER_AGENT'}<br>";
print "<b>GATEWAY_INTERFACE</b> :$ENV{'GATEWAY_INTERFACE'}<br>";
print "<b>HTTP_HOST</b> :$ENV{'HTTP_HOST'}<br>";
print "<b>SERVER_SOFTWARE</b> :$ENV{'SERVER_SOFTWARE'}<br>";
print "<b>SERVER_PORT</b> :$ENV{'SERVER_PORT'}<br>";
print "<b>SCRIPT_FILENAME</b> :$ENV{'SCRIPT_FILENAME'}<br>";
print "<b>PATH</b> :$ENV{'PATH'}<br>";
print "<b>REQUEST_METHOD</b> :$ENV{'REQUEST_METHOD'}<br>";
print "<b>REQUEST_URI</b> :$ENV{'REQUEST_URI'}<br>";
print "<b>DOCUMENT_ROOT</b> :$ENV{'DOCUMENT_ROOT'}<br>";
print "<b>SERVER_ADMIN</b> :$ENV{'SERVER_ADMIN'}<br</pre>";
print "</body>";
print "</html>";
The following screenshot shows part of the output:
Ex6: form example:
In this example we will process an HTML form using perl. The HTML form has one text field to accept a value from the user.
condition.html:
<html>
<head>
<title> conditions </title>
</head>
<body>
<form name="myform" action="http://localhost/cgi-bin/condition.cgi">
Please enter a value
<input type="text" name="val1" size="25"<br>
<input type="submit">
<input type="reset">
</form>
</body>
</html>
/html>
Then, condition.cgi will process this value passed from the HTML form to determine if it is positive, negative, or zero.
condition.cgi:
#! C:/perl/bin/perl
print "Content-Type: text/html\n\n";
#use CGI standard input to access the form data
use CGI qw(:standard);
$val1 = param('val1');print "<html>\n <title> \n condition \n </title>\n";
print "<body>\n";
if($val1 ==0)
{
print "your value is zero <br>";
}
elsif($val1<0)
{
print "your value is negative <br>";
}
else
{
print "your value is positive <br>";
}
print "</body>";
print "</html>";
To run this example, save condition.html under htdcos folder of apache server and save condition.cgi under cgi-bin folder.
Following are screenshots of the output:
Ex7: Another form example:
In this example we will perform the basic mathematical operations on two numeric values passed from an HTML form to mathForm.cgi.
mathform.html:
<html<head>
<title> an example of form </title>
</head>
<body>
<form name="myForm" action="http://localhost/cgi-bin/mathForm.cgi">
Enter first value<input type="text" name="val1" size="25"<br>Enter second value<input type="text" name="val2" size="16"<br>
<input type="submit">
<input type="reset">
</form>
</body>
</html>
mathform.cgi:
#!c:/perl/bin/perl
print "Content-Type: text/html\n\n";use CGI qw(:standard);
use strict;
my ($val1, $val2,$result);
$val1 = param('val1');$val2 = param('val2');
print "<html>\n <title> math from a form </title>\n";print "<body>\n";
$result = $val1 + $val2;
print "<br> $val1+ $val2= $result <br>";
$result = $val1 - $val2;
print "<br> $val1- $val2= $result <br>";
$result = $val1 * $val2;
print "<br> $val1* $val2= $result <br>";
$result = $val1 / $val2;
print "<br> $val1/ $val2= $result <br>";
$result = $val1 ** $val2;
print "<br> $val1 to the power $val2= $result <br>";
$result = $val1 % $val2;
print "<br> $val1 % $val2= $result <br>";
print "<br>\n";
print "</body>";
print "</html>";
Following are screenshots of the output:
14