Jump to content
Sign in to follow this  
jeffman

A basic perl query script

Recommended Posts

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...