PERL Journal Clock Example

From XPUB & Lens-Based wiki
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

PERL Journal Article intro to CGI programming

Original PERL code by Lincoln Stein:

#!/usr/bin/perl                                                                 

print "Content-type: text/html\r\n";
print "\r\n";
chomp($time = `date`);
print<<EOF;
<HTML><HEAD>
<TITLE>Virtual Clock</TITLE>
</HEAD>
<BODY>
<H1>Virtual Clock</H1>
At the tone, the time will be
<STRONG>$time</STRONG>.
</BODY></HTML>
EOF

Translated to python (nb check_output was introduced to python in version 2.7):

#!/usr/bin/python                                                               

from subprocess import check_output

print ("Content-type: text/html\n")

time = check_output(["date"]).strip().decode("utf-8")

print ("""                                                                      
<HTML><HEAD>                                                                    
<TITLE>Virtual Clock</TITLE>                                                    
</HEAD>                                                                         
<BODY>                                                                          
<H1>Virtual Clock</H1>                                                          
At the tone, the time will be                                                   
<STRONG>{0}</STRONG>.                                                           
</BODY></HTML>                                                                  
""".format(time))