Download presentation
Presentation is loading. Please wait.
Published bySolomon Stewart Modified over 9 years ago
1
2 1 Sending Data Using a Hyperlink CGI/Perl Programming By Diane Zak
2
2 2 Objectives In this chapter, you will: Create a hyperlink to a CGI script Append data to a URL Use the CGI.pm module to parse data Access data using the param function
3
2 3 Introduction Changes to static web pages can only be made when the page is updated on the server –Requires human intervention Changes to dynamic web pages can be made in response to information sent through a hyperlink or form
4
2 4 Creating a Link to a Script Most web pages contain text, images and hyperlinks –Hyperlinks or links appear as different colored text, underlined text, or clickable images –The HTML anchor tag with the HREF property is used to create a link HREF = Hypertext Reference Syntax: hyperlinktext hyperlinktext is the clickable text is the syntax for a clickable image
5
2 5 Creating a Link to a Script
6
2 6 When creating a script, remember to: –Enter the shebang line (required for UNIX) –Enter a comment that includes the name of the script and the script’s purpose –Enter the Content-type line: print “Content-type: text/html\n\n”;
7
2 7 print Function Perl can generate HTML output in various ways: –print –printf –“here” documents Syntax for print: –print output; Output can be strings, functions, variables Output can be an expression or a list of expressions, separated by commas
8
2 8 print Function Using the newline character (\n) can make HTML code easier to read when viewing the HTML source in a browser –Without \n, the HTML code appears all on one line, and is difficult to read It is good practice to include all of the HTML tags - HTML, HEAD, and BODY
9
2 9 Completing a Script Remember that the following needs to be done for each Perl/CGI script that is created: 1. Save the document 2. cd path If UNIX - chmod 755 filename 3. perl -c filename 4. perl -w filename 5. Open the script in a web browser
10
2 10 Sending One Item of Data Using a Link Append the data to the URL –Use a question mark (?) to separate the URL from the data –Use an equal sign (=) to separate the key from the value –Syntax: hyperlinkText –key is the variable name One-word name –value is the value that is assigned to the key
11
2 11 Sending One Item of Data Using a Link When you have clicked on a link that is sending data, the data is viewable in the browser’s address box
12
2 12 Parsing Data Data can be sent through a URL, but the script has to be able to separate the key from the value This ability is part of the CGI.pm module –module = collection of prewritten code stored in a file –Most modules use the file extension.pm, which stands for perl module
13
2 13 Parsing Data Can find if you have cgi.pm by: –whereis cgi.pm (UNIX) –Start --> Search --> cgi.pm (Windows) –If not, can download from: http://www.genome.wi.mit.edu/ftp/pub/ software/WWW/cgi_docs.html
14
2 14 Parsing Data To use CGI.pm, you need to include the statement: –use CGI qw(:standard); qw = quote words Same as the statement - use CGI(‘:standard’); :standard is an import tag –Tells the Perl interpreter to allow the use of the standard features of the CGI.pm module Can find out more about CGI.pm, by typing: –perldoc CGI.pm
15
2 15 Parsing Data The param function accesses the value associated with a key –param is part of the standard features of CGI.pm –Syntax: param(key) where key is in single or double quotation marks Example: –param(‘state’); –param(“state”);
16
2 16 Parsing Data When checking for errors, using perl -w scriptname, you can: –Type key=value after the scriptname –Example: perl -w jackson.cgi state=Alabama
17
2 17 Parsing Data You can also type in key=value on a separate line –Example: perl -w jackson.cgi “(offline mode: enter name=value pairs on standard input)” message should appear Type in key=value pairs Type Ctrl+d (UNIX) or Ctrl+z (Windows) when done
18
2 18 Parsing Data If the offline mode message does not appear, the use CGI statement needs to be changed: –use CGI qw(:standard -debug); –This is including the -debug pragma, and tells CGI.pm to pause to let you type in the data pragma = special type of Perl module.
19
2 19 Parsing Data
20
2 20 Parsing Data
21
2 21 Sending Multiple Items of Data Using a Link When sending more than 1 item of data through a link, use the following syntax: <A HREF=URL?key1=value1&key2= value2...&keyN=valueN>hyperlinkText –An ampersand (&) is used to separate each key and value pair –Some browsers limit the amount of information that can be attached to a URL
22
2 22 Sending Multiple Items of Data Using a Link When sending a value that contains more than one word, substitute a plus sign (+) for a space –Example: Arkansas The jackson.cgi script, when sent that data can get the values of the state and cap keys using param –print “The capital of “, param(‘state’), “ is “, param(‘cap’), “.\n”; –The capital of Arkansas is Little Rock.
23
2 23 Sending Multiple Items of Data Using a Link
24
2 24 Summary The syntax for creating a hyperlink is: hyperlinkText –hyperlinkText is the clickable link The syntax for creating an image link is: –imagefile contains the path and name of the image file print output; can be used to generate HTML instructions. –output is an expression or list of expressions separated by commas
25
2 25 Summary To pass one item of data to a script, using a hyperlink, use the syntax: hyperlinkText –key is the variable name associated with the value To pass more than one item of data to a script, using a hyperlink, separate the key and value pairs with a &, with the syntax: <A HREF=URL?key1=value1&key2=value2...& keyN=valueN>hyperlinkText To pass a value that has a space in it, replace the space with a plus sign (+)
26
2 26 Summary The CGI.pm module can be used to parse data –use CGI qw(:standard); When testing the script from the command line, with perl -w, you can either: –Enter the data on the same line as the command –Enter each item of data on a separate line –You may need to use the -debug pragma You can use CGI.pm’s param function to access the value of a key that was passed to the script
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.