![]() |
![]() Spring Semester 2007 |
What does the simplest script look like? |
There's more than one answer to this question, but here's a pretty simple script:
#!/usr/bin/perl use CGI; $q = new CGI; print $q->header, $q->start_html; $time = localtime; print $time; print $q->end_html; |
This script uses the Perl library CGI.pm
|
It does. Let's develop it further:
#!/usr/bin/perl use CGI; $q = new CGI; print $q->header, $q->start_html; print $q->Dump; print $q->end_html; |
Call this script by adding this to its URL:
?one=10&two=23 | What does it print? |
It prints a list of entries, as in a hashtable. | That's how parameters are being passed to the script. |
With this we can write a script that counts the number of times you've called it. | How? |
By constantly sending itself a bigger value:
#!/usr/bin/perl use CGI; $q = new CGI; print $q->header, $q->start_html; $number = $q->param('number'); $number += 1; print qq{ You have called me $number times. <p> <a href="?number=$number">Click here</a> to call me again. }; print $q->end_html; | Very good. Can you add a link to reset the counter? |
Sure. | I'll let you make that change. |
No problem. | Can you write this program now? |
Just by looking at the HTML interface I think I can. | I thought so; you just have to pass a bigger state. |
What do you mean by that? | More variables: the user score, computer score, and the (already selected) computer choice. |
In addition you need to allow the user to enter a choice. |
That's why we can encode it as part of the link. |
What happens if the user needs to enter a number? | Like in this program? |
Yes. | Well, in that case we need to understand HTML forms. |
Let's work out an example. | Good idea; let's do the calculator. |
Here's a program that only prints the interface: | Note however that the browser is already set to let you use that interface. |
#!/usr/bin/perl use CGI; $q = new CGI; print $q->header, $q->start_html; print qq{ <form> Your current balance is: ... <p> Please enter an amount: <input type="text" name="arg"> <p> Then select a type of operation: <select name="fun"> <option value="non"> Click me! <option value="add"> Deposit <option value="sub"> Withdraw </select> <p> When ready, please press <input type="submit" value="Proceed"> </form> }; print $q->end_html;
Interesting: the browser is collecting the values we type, and it does it in the format we discussed earlier. |
Precisely. Typing a 45 and choosing Deposit turns the URL into
?fun=45&arg=add |
All of this is user input, though. |
Where could we store our state (the current balance for example)>
|
We could set up a text field, initialize it and tell the user to never touch it. |
You can hide a textfield that you initialize, too? | How do you do that? |
Just turn the textfield attribute into hidden ...
|
Well, than that settles it. Here's the code:
#!/usr/bin/perl use CGI; $q = new CGI; print $q->header, $q->start_html; $balance = $q->param('balance'); # state $arg = $q->param('arg'); # input $balance += $arg; print qq{ <form> Your current balance is: $balance <p> Please enter an amount: <input type="text" name="arg"> <p> Then select a type of operation: <select name="fun"> <option value="non"> Click me! <option value="add"> Deposit <option value="sub"> Withdraw </select> <p> When ready, please press <input type="submit" value="Proceed"> <input type="hidden" name="balance" value="$balance"> </form> }; print $q->end_html; |
This always adds the input to the balance, but it's a start. | I am sure you can finish it. |
Same here. | Now we can look forward to better, bigger programs. |