|
CGI - Variables |
tutorial by Matt |
This section is a very short section because varibles for perl are very simple. Variables are
used to store information for later use in a program such as saving the users name or
their email address so later you can send them a friendly email. You declare a variable
by first typing $ and then typing the variable name.
$uname = $query->param('uname');
This line of code calls the param function of the CGI.pm Module. Param then gets the
value in the field labeled with a name of "uname" and adds it to the $uname variable.
I'll explain this idea better with a full example.
First open NotePad and enter the HTML form in listing 2.1
Listing 2.1
Example 2
Now enter the code in Listing 2.2 into a new NotePad document
Listing 2.2
#!/usr/bin/perl
use CGI;
$query = new CGI;
$uname = $query->param('uname');
print $query->header;
print "\n\nWelcome\n\n\n";
print "hi, $uname!";
print "\n
-----------------------------------------------
This script first, as with all scripts, calls the perl interpreter
with the Shebang line. It then calls the CGI.pm module and set
a variable for it. after that it gets the value of the name
field from the form and stores it in the variable $uname.
Then we print the header for the page then the starting
html and then we finally print the message hi, and then
the value of $uname. Then we add the ending html and we are done.
Thats it for this section you now have enough knowledge
to move onto the next section Adding HTML.
|
|
|
|