Scripting Languages G22.3033-002 Summer 2008
Scripting Languages G22.3033-002 Summer 2008 Example solutions for quiz2
Thursday 6/26/2008. 30 points.
http://www.cs.nyu.edu/courses/summer08/G22.3033-002/
Instructions for example solutions
These are example solutions. Please keep in mind that often, there is
not just one correct solution to a question. If you come up with
different answers, then it may be that both your answers
and these answers here are correct. Of course, these answers here may
also contain mistakes. If you spot a mistake, please let me know so I can
correct it.
Example solutions for Quiz 2
solutions-quiz2-1 Regular expressions
"3.141", "1000"
"3.1a", "12.34.56"
^(0|[1-9][0-9]*)(\.[0-9]*)?$
solutions-quiz2-2 Server-side scripting
- The user enters input in an HTML form. When the form is submitted, the
input turns into arguments of an HTTP GET or POST message. When the
server runs the PHP script in response to the HTTP method, it makes the
arguments available in the PHP superglobals
$_GET or
$_POST.
- When the server runs the PHP script, it replaces each snippet of PHP
code embedded in HTML with the output (
echo, print()) of
running the PHP snippet.
- A self-processing page is a PHP script that serves both as input (via a
form) and output. In other words, when the form is submitted, the same
PHP script runs again, this time showing the output as well as the
form. A good example is the Google search engine: the query input box
appears again alongside the search results.
solutions-quiz2-3 Context
3 7 8 9 9
- A list literal in scalar context gets converted to its last element. For
example, the assignment
$y = (7, 8, 9) provides a scalar
context to the list literal (7, 8, 9), and therefore assigns
9.
- An array in scalar context gets converted to its length. For example,
the addition
@x + 0 provides a scalar context to the array
@x, and therefore returns 3.
- A scalar in list context gets converted to a list of one element. For
example, the loop
for $i ($y) { print $i } provides a list
context to the scalar $y, and therefore uses it as a list of one
element, 9.