CGIScriptsandPerl

CGIScriptsandPerl - Lecture Some CGI Scripts and More on...

Info icon This preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Perl Copyright © Ellis Horowitz 1999-2010 1 Lecture Some CGI Scripts and More on Perl
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Perl Copyright © Ellis Horowitz 1999-2010 2 CGI Scripts in Perl • In this section we offer many, relatively small, cgi scripts. Each has a specific purpose and can be used as a tool box to create more ambitious server- side programs • go to http://www-scf.usc.edu/~csci571/Special/perlexamples.html to see these examples actually working
Image of page 2
Perl Copyright © Ellis Horowitz 1999-2010 3 Encoded Data When data is sent certain characters must be encoded, e.g. ", /, blank Each character has a hexadecimal equivalent, as shown previously The browser transforms special characters into their hexadecimal equivalents and the cgi script must transform back from hexadecimal to the character. Example: here is a form with a single text box; encoding.pl does the transformation <HTML><HEAD><TITLE>Encoding</TITLE></HEAD> <BODY><H1>Please enter some text</H1> <FORM ACTION=/cgi-bin/encoding.pl METHOD=POST> Enter text here: <INPUT NAME=input><BR> <INPUT TYPE=submit> <INPUT TYPE=reset></FORM> </BODY></HTML>
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Perl Copyright © Ellis Horowitz 1999-2010 4 Decoding Perl Script #!/usr/usc/bin/perl $size_of_form_info =$ENV{‘CONTENT_LENGTH’}; read(STDIN, $form_info, $size_of_form_info); $form_info =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg; #s is substitute, \dA-Fa-f looks for hex number and stores it in $1 #pack and hex convert the value in $1 to ASCII, e evaluates second part #of the substitute command as an expression, g replaces all occurrences ($field_name, $data) = split (/=/, $form_info); print "Content-type: text/plain", "\n\n"; print "Your input is: $data, right?", "\n"; exit(0);
Image of page 4
Perl Copyright © Ellis Horowitz 1999-2010 5 Using Extra Path Info to Print File Contents Consider the URL: http://mydomain/cgi-bin/display.pl/myfile.txt where myfile.txt is stored in the document root. This program displays the contents of myfile.txt #!/usr/usc/bin/perl #displays a text file located in document root hierarchy $plaintext_file = $ENV{‘PATH_TRANSLATED’}; print "Content-type: text/plain", "\n\n"; if ($plaintext_file =~ /\.\./) {#prevent .. from being used print "Invalid characters in filename", "\n"; } else { if (open (FILE, "<", $plaintext_file)) { while (<FILE>) { print; } close(FILE); } else { print "Sorry, file cannot be read", "\n"; } } exit(0); open() defines a filehandle FILE and < denotes input <FILE> is a filehandle that reads a line
Image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Perl Copyright © Ellis Horowitz 1999-2010 6 Program to Return a GIF Image Invoke this program as: <a href=cgi-bin/displaygif.pl>Click Here</a> #!/usr/usc/bin/perl $gif_image = join("/", $ENV{‘DOCUMENT_ROOT’}, "icons/tiger.gif"); if (open (IMAGE, "<", $gif_image)) { $no_bytes = (stat ($gif_image))[7]; #stat returns a 13 element array whose # 8th element is the file size print "Content-type: image/gif", "\n"; print "Content-length: $no_bytes", "\n\n"; print <IMAGE>; } else { print "Content-type: text/plain", "\n\n"; print "Sorry, cannot open the file $gif_image", "\n"; } exit(0); IMAGE is a filehandle which opens for reading
Image of page 6
Perl Copyright © Ellis Horowitz 1999-2010 7 Example: Redirection <html><head><title>Test Redirection</title></head> <body> <H2>Test Redirection</H2> <FORM method="post" action="redirect.pl"> <SELECT name="url"> <OPTION selected=selected value="">Select a Site</option> <OPTION value=http://www.usc.edu>USC</option>
Image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern