jeffman Posted February 17, 2008 Share Posted February 17, 2008 To inaugurate the new CGI forum, I thought I would post an extremely basic perl script. This one barfs back the values of a query string. Along the way, we see how to use the perl module CGI, how to parse a query string when the names of the parameters are known in advance, and how to print a response. I suggest calling it query.cgi . Bon appetit! #!/usr/bin/perluse CGI; # the object-oriented configuration of CGI.pm$query = new CGI;for (@NAMES = $query->param) { if ( $_ eq "a") {$A = $query->param( $_ );} # $_ is basic to perl scripting elsif ( $_ eq "b") {$B = $query->param( $_ );} elsif ( $_ eq "c") {$C = $query->param( $_ );}}print $query->header(); # A shortcut to the normal content headerprint <<END_HTML; # The 'here-doc' method of quoting formatted text<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>Parsing a query string</title> </head> <body> a = $A <br> b = $B <br> c = $C </body></html>END_HTML# To test the script, paste the query string below to the end of the script's url# ?a=apples&b=bananas&c=carrots#end query.cgi Link to comment Share on other sites More sharing options...
jeffman Posted February 17, 2008 Author Share Posted February 17, 2008 Remember: perl scripts do not usually copy and paste very well. The usual villain is newline/carriage return characters. If you do want to paste this one, if you get an error, you might try deleting all the end-of-lines and then simply hitting your return key to reinsert the proper end-of line code. Or, you could just retype the thing. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.