CSE 102 Introduction to Web Design and Programming Perl & CGI
What is Perl? The Practical Extraction & Report Language Open Source (1987) freely available for downloading from the Comprehensive Perl Archive Network www.perl.com it is already installed on Sparky The Perl scripting language has many uses, for us: Web CGI programming (like we’ve been doing)
What can you do with Perl Lots: scalar variables arrays mathmatical arithmetic string operations boolean arithmetic conditional statements standard I/O file I/O interprocess I/O iteration functions
Script File Issues When transfering files from Windows to Unix, some encoding issues So, we can use Pico in Unix, to use: Open SSH Terminal Window and login cd www cd cgi-bin ls pico filename Once pico opens, you may begin typing Commands: CTRL-O to save (look at bottom of screen) CTRL-X to exit Also, make sure all files have 755 file permissions
CookieMonster.html <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Domain Name Availability Check</title> </head> <body> <h2>A Cookie Demo</h2> <h3>The Cookie Monster wants to know</h3> <form method="post" action="http://www.sinc.sunysb.edu/cgi-bin/cgiwrap/aesmaili/cookiemonster.pl" enctype="application/x-www-form-urlencoded"> <p>What sort of cookies do you like? <input name="cookie_type" size="16" type="text" /> <input type="submit" value="Submit" /> </p> </form> </body> </html>
cookiemonster.pl #!/usr/local/bin/perl -T use CGI qw(:standard); $ENV{'PATH'}='/usr/sbin:/sbin:/bin:/usr/bin'; delete @ENV{qw(IFS CDPATH ENV BASH_ENV)}; ## script values REPLACE aesmaili with YOUR USERNAME $cookie_path = '/cgi-bin/cgiwrap/aesmaili/cookiemonster.pl'; $whois="/usr/bin/whois"; ##### in-script configuration values END $cookie_type=param('cookie_type'); ## set cookie $the_cookie = cookie(-name=>'TheCookieMonsterCookie!!!', -value=>$cookie_type, -expires=>'+100000h', -path=>$cookie_path); # Print the cookie header with expiration date print header(-cookie=>$the_cookie, -charset=>'UTF-8'); ##### send HTML page outputFile(); ################# subroutines ##################### sub outputFile { print <<END; <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head><title>Cookie Response</title></head> <body style="background-color:blue; color:white"> <h3>The Cookie Monster knows you like $cookie_type cookies</h3> <p>I put a cookie on your computer</p> </body> </html> END } cookiemonster.pl
FileUploader.html <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>File Uploader</title> </head> <body> <p style="font-weight: bold; font-size: larger"> Upload a picture</p> <form method="post" action="http://www.sinc.sunysb.edu/cgi-bin/cgiwrap/aesmaili/fileuploader.pl" enctype="mulipart/form-data"> <input type="file" name="yourpic" /><br /> <input type="submit" value="Submit" /> </form> </body> </html>
fileuploader.pl #!/usr/bin/perl use CGI qw(:standard); ## cgi perl module $query = new CGI; var $name = param('yourpic'); $fh = $query->upload($name); binmode(fh); binmode(OUT); while ( <$fh> ) { print OUT; } close OUT; print "Content-type: text/html\r\n\r\n"; print <<END; <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head><title>File Uploading</title></head> <body> <h3>File Uploaded: $name</h3> <p><img src="$name" /></p> </body> </html> END fileuploader.pl