Sign in to follow this  
Followers 0
jeffman

A basic perl query script

2 posts in this topic

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0