HTML Perl 2008-09
HTML Perl 2008-09
Shankar @ ISE
Introduction HTML
<html>
<head>
<title>Title of page</title>
</head>
<body> This is my first homepage.
<b>This text is bold</b>
</body>
</html>
20 August 2008 shankar@ISE
HTML Elements are HTML Tags
Preformatted Text
<pre>this is
an example of a preformatted text tag</pre>
Comments
<!-----This comment will not appear in the browser----->
20 August 2008 shankar@ISE
HTML Basics Cont…
Lists
Unnumbered lists
<ul>
<li> list item 1
<li> list item 2
<li> list item 3
</ul>
Numbered lists
<ol>
<li> list item 1
<li> list item 2
<li> list item 3
</ol>
20 August 2008 shankar@ISE
HTML Tables
td table data
tr table row
th table header
check boxes and radio buttons: allow the user to say yes/no to a choice, or one
of several choices.
– eg: <input type="checkbox" checked/> Would you like monthly newsletter?
radio buttons: can be used to give an exclusive choice among several possible
options
– eg: <input type="radio" name="gender1" checked/> Male
<input type="radio" name="gender2" /> Female
20 August 2008 shankar@ISE
HTML Forms Cont…
selection lists: these give a choice through a popup or scroll list
– eg: <select> <option value="1">dog</option>
<option value="2">cat</option>
<option value="3">cow</option> </select>
Button as hyperlink
– eg: <button onclick="location='http://www.google.com'">Go to Google</button>
Image as hyperlink
– eg: <imgsrc="yahoo_out.gif" onclick="location='http://www.yahoo.com'"
onmouseover="src='yahoo_over.gif'" onmouseout="src='yahoo_out.gif'" />
Perl is by far the most widely used language for CGI programming. It contains
many powerful features, and is very easy for the novice programmer to learn.
– There are numerous extensions built on top of Perl for specialized functions;
for example, there is oraperl (or the DBI Extensions), which contains functions for
interfacing with the Oracle database.
Variables
$foo # a scalar
@foo # a list
%foo # a hash
Eg: $n = 42; $name = “shan"; $color = 'red'; print $name + $color;
$real_result= $boolean_result? 1 : 0;
@scores = (32, 45, 16, 5);
%favorite = (shan=> 'red', sam=> 'blue');
$scores[2] # an element of @scores
$favorite{shan} # a value in %favorite
20 August 2008 shankar@ISE
Perl Cont…
scalar values
$days # the simple scalar value "days"
$days[28] # the 29th element of array @days
$days{'Feb'} # the 'Feb' value from hash %days
entire hashes
%days # (key1, val1, key2, val2 ...)
hash values
$abc{'December'} = 12;
$month = $abc{'December'};
20 August 2008 shankar@ISE
Perl Example
#!/usr/local/bin/perl
# addition.pl
# A simple addition program
print "Please enter first number:\n";
$number1 = <STDIN>;
chomp $number1;
print "Please enter second number:\n";
$number2 = <STDIN>;
chomp $number2;
$sum = $number1 + $number2;
print "The sum is $sum.\n";
20 August 2008 shankar@ISE
Perl (Operators)
# logical_operators.pl
print "truth value of (2 == 2) = ", (2 == 2), "\n"; ;
print "truth value of (1 == 2) OR ( 1==1) = ", (1 == 2 or 1==1), "\n";
The ? Operator
eg: $val= 5; print ( $val== 5 ? "valequals 5\n" : "valdoesn't equal 5\n" );
File operators:
-f filename # tests if file exists
-d , -x, -w
Eg: # file.pl
print "Enter file/directory name:\n";
$filename = <STDIN>;
chomp $filename;
print "$filename ", -f $filename ? "IS a file\n" : "is NOT a file\n";
Assignment operators:
$a += 2; is equivalent to $a = $a + 2;
**= += *= &= <<= &&= -= /=
|= >>= ||= .= %= ^= x=
20 August 2008 shankar@ISE
Perl (Operators) Cont…
numeric comparisons:
< > <= >= == != <=>
<=> returns -1, 0, or 1 depending on whether the left argument is numerically less
than, equal to, or greater than the right argument
string comparsions:
lt gt le ge eq ne cmp
cmp returns -1, 0, or 1 depending on whether the left argument is stringwise less
than, equal to, or greater than the right argument
Bitwise operators: shift left: << , shift right: >>, AND: &, OR: |, XOR: ^, negation: ~
Index count: $#
Eg: @nums=(…);
print $#nums;
push(@array, x, y)
pop(@array)
sort(@array)
lc($var)
split
Eg: # spliting.pl
$alphabet = "ABCDEF";
@words = split(//, $alphabet); # @words = ('A', 'B', 'C', 'D', 'E', 'F')
Join
Eg: # joining.pl
@words = ('The', 'quick', 'brown', 'fox', 'jumped');
print join("+", @words), "\n“; # The+quick+brown+fox+jumped
20 August 2008 shankar@ISE
Perl (Hashes)
# hashes.pl
# create a lookup table of Ciphertext
# keyed by Cipher ID
%accession_hash=
( "BACR01A01" => "AC005555",
"BACR48E02" => "AC005577",
"BACR24K17" => "AC005101", );
# get all the keys in the hash (hash is keyed by Cipher ID)
@cipher = keys %accession_hash;
print “Cipher IDs: @cipher\n";
# prints BACR01A01 BACR48E02 BACR24K17
# get all the values in the hash (hash is a lookup for accessions)
@accs= values %accession_hash;
print “Ciphertexts: @accs\n";
# prints AC005555 AC005577 AC005101
20 August 2008 shankar@ISE
Perl (Files)
open (HANDLE, "/file/path") -open for reading
open (HANDLE, "< /file/path") -open for reading
open (HANDLE, "> /file/path") -open for writing
open (HANDLE, ">> /file/path") -open for appending
open (HANDLE, "| shell command") -open pipe for writing
open (HANDLE, "shell command |") -open pipe for reading
Eg:#openfile.pl
$data_file=“shank.cgi";
open (F, $data_file) || die ("Could not open file!");
@raw_data=<F>;
close(F);
GET
– Your entire form submission can be encapsulated in one URL
– You can access the CGI program with a query without using a form
– Fully includes it in the URL:
http://myhost.com/mypath/myscript.cgi?name1=value1&name2=value2
– Is how your browser downloads most files
– Don't use GET if you want to log each request
– Is used to get a file or other resource
GET:
<FORM NAME="myform" ACTION=“http://localhost/cgi-bin/prog.cgi" METHOD="GET">
First Name: <INPUT TYPE="TEXT" NAME="fname" SIZE="20“><BR>
Last Name: <INPUT TYPE="TEXT" NAME="lname" SIZE="20"><BR>
<INPUT TYPE="SUBMIT" VALUE="SUBMIT">
</FORM>
#Html program
<HTML>
<HEAD><TITLE>Simple Form!</TITLE></HEAD>
<BODY> <H1>Simple Form!</H1> <HR>
<FORM ACTION="/cgi-bin/perl_prog.pl" METHOD="GET">
Command: <INPUT TYPE="text" NAME="command" SIZE=40>
<P>
<INPUT TYPE="submit" VALUE="Submit Form!">
<INPUT TYPE="reset" VALUE="Clear Form">
</FORM> <HR> </BODY> </HTML>
Eg: # hello.html
<html> “Hello World” </html>
http://localhost/hello.html
Execution permission
# chmod +x hello.cgi
Browser
http://localhost/cgi-bin/hello.cgi
20 August 2008 shankar@ISE
Programs
Html: To display UNIX output
//prog1b.html
<html>
<head>
<title>Command Execution</title>
</head>
<body bgcolor=\"red\">
<form action="http://localhost/cgi-bin/prog1b.pl" method="GET">
<h1>Command Execution:</h1><hr>
Enter the Command: <input type="text" name="cmd" ><br>
Submit: <input type="submit "value="submit">
</form>
</body>
</html>
//prog1b.pl
#!/usr/bin/perl
print"Content-Type:text/plain","\n\n";
print"Result of the Command:","\n\n";
$query_string = $ENV{'QUERY_STRING'};
($field_name,$command) = split(/=/,$query_string);
print `$command`;