Chapter 12: CGI/Perl Part II
pages 1 - 25
Scalar Variables
- Must be preceded by $.
- Do not need to be declared, unless use strict;
appears in the script.
- Are case sensitive, but lowercase is conventional, e.g.
$name
- annuity.cgi example
and source code.
Assignment Statement
- $payment = 2000.00;
- $payment = $payment + 10.00;
- $name = "Rob";
- $message = "His name is $name"; (Interpolation)
- $message = 'His name is $name'; (No Interpolation)
Arithmetic Operators
- ()
- **
- - (unary)
- *
- /
- %
- +
- - (binary)
Connecting an HTML form to a CGI Script
- View the source of this web page.
- Notice the form tag with its action attribute.
- Notice the form tag with its method attribute
(the other value is "get").
- Notice the input tag with its name attribute.
- Notice the input tag with its type="submit" attribute-value pair.
- Notice the input tag with its type="reset" attribute-value pair.
- Here is the CGI source
print Statement
- To print a dollar sign, e.g. print "\$100.00";
printf Statement
- printf formatstring, list;
- formatstring: % [modifier] [minimum field width] [precision] format type
- modifier: + (display a + or - before the number) or
0 (pad the left side with 0s if necessary)
- minimum field width: a number
- precision: .number
- format type: d (decimal) or f (floating point) or s (string)
- Example: printf "The value of %s is %+4.2f", $stock, $value;
- To print a percent sign, e.g. printf "%%";
Pattern Matching
Wednesday Exercise
10 points. Work a modified version of exercise 12-4 from the text.
The inputs are
- a name
- a real numbered value for the hours worked
The outputs are
- the name of the worker
- the number of 40 hour work weeks worked
- the number of 8 hour days worked (after all of the full weeks
are accounted for)
- the number of hours worked (after all of
the full days are accounted for)
- the number of minutes worked
Thursday Exercise (Regular Expressions)
10 points. Complete the perl script below such that
it adds / to the end of the hr, img, br
and input tags if the ending slash is missing.
When you are finished, copy your solution onto
Jack's memory stick.