kenics.net

Technical notes on perl, python, php, sql, cgi, c/c++, q/kdb+, unix/shell, revision control tools, data structures & algorithms, and their applications into web services and other various forms of software engineering.

perl cgi

 
######################### 
###      perl cgi     ### 
######################### 
 
Perl based CGI is becoming obsolete as more specialized languages emerged, e.g. php, javascript, ruby. 
For performance, these days it often makes more sense to run scripts on client-side browser. 
 
 
 
#### 
####  generating an html page from perl cgi 
#### 
 
we just need to print proper html tags. 
writing many print statements is painful and prone to human error, thus a heredoc is recommended. 
 
e.g. 
-----------------------------// example.cgi 
#!/usr/bin/perl 
 
print << "ANCHOR_0"; 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<title> 
This is a sample perl cgi generated html page. 
</title> 
</head> 
<body> 
<br> 
<font size="6">My name is Ken Sugimoto</font> <br> 
<br> 
whatever content goes in here. 
 </body> 
 </html> 
 
ANCHOR_0 
 
----------------------------- 
 
 
 
#### 
####  checking user ipaddr and hostname 
#### 
 
there are many user supplied data that can be accessed via env vars. here is an example. 
 
-----------------------------// ipaddr2hostname.cgi 
#!/usr/bin/perl 
 
my $ipaddr = $ENV{'REMOTE_ADDR'}; 
my $hostname = gethostbyaddr(pack("C4",split('\.',$ipaddr)),2) || $ipaddr; 
 
 
print "Content-type: text/plain\n\n"; 
 
print "ip addr :".$ipaddr."\n"; 
print "hostname:".$hostname; 
 
exit(0); 
----------------------------- 
 
 
 
#### 
####   input parameter retrieval 
#### 
 
http://foobar.cgi?hello_world 
 
---> inside foobar.cgi 
print $ENV{QUERY_STRING}    ## prints "hello_world" 
                            ## for multiple input args, often we specify a demimeter and split(delim,$ENV{QUERY_STRING}) 
 
 
 
 
#### 
####  page hit counter 
#### 
 
a classic example. this is the cgi equivalent of hello world. 
 
 
-----------------------------------// counter.cgi 
#!/usr/bin/perl 
 
$title=$ENV{QUERY_STRING}; 
 
if($title !~ /^[a-z_]+$/){         # this type of sanity check is crucial in practice. 
    print "Error code 12"; 
    exit(0); 

 
&counter($title); 
 
sub counter{ 
    $data_file="./log/".$_[0]."_counter"; 
    open(DATA,"+< $data_file") or die print "Error code 14"; 
    flock(DATA,2); 
    seek(DATA,0,0); 
    $data=<DATA>; 
    $data++; 
    seek(DATA,0,0); 
    print DATA $data; 
    close(DATA); 
 
    print "Content-type: text/plain\n\n"; 
    printf "Page View Count: %08d \n",$data; 

 
----------------------------------- 
 
 
 
 
#### 
####  random exercise 
#### 
 
---------------------------------------// random_vocab.cgi 
#!/usr/bin/perl 
 

# takes a file name as input arg, and access the file in /hoge/foo/bar/ and randomly selects & prints one line. 

 
my $note=$ENV{QUERY_STRING}; 
 
print "Content-type: text/plain\n\n"; 
 
if($note eq ""){ 
        print "no content available"; 
        exit; 

 
open(IN,"</hoge/foo/bar/".$note); 
@content = <IN>; 
close(IN); 
 
my $len = @content; 
if($len == 0){ 
        print "no content available"; 

my $random_int = int(rand($len)); 
if($random_int == $len){$random_int--;} 
my @words = split(/;/,$content[$random_int]); 
 
print << "ANCHOR_0"; 
 
$words[0] : $words[1] 
 
ANCHOR_0 
------------------------------ 

  1. 2015-01-05 22:38:30 |
  2. Category : perl
  3. Page View:

Google Ads