Scripting Languages

Prev: Concurrency Next: Building a Runnable Program

14.1-14.2 Check Your Understanding

  1. Give a plausible one-sentence definition of “scripting language.”
  2. List the principal ways in which scripting languages differ from conventional “systems” languages.
  3. From what two principal sets of ancestors are modern scripting languages descended?
  4. What IBM creation is generally considered the first general-purpose scripting language?
  5. What is the most popular language for server-side web scripting?
  6. How does the notion of context in Perl differ from coercion?
  7. What is globbing? What is a wildcard?
  8. What is a pipe in Unix? What is redirection?
  9. Describe the three standard I/O streams provided to every Unix process.
  10. Explain the significance of the #! convention in Unix shell scripts.

14.2 Check Your Understanding

  1. What is meant by the pattern space in sed?
  2. Briefly describe the fields and associative arrays of awk.
  3. In what ways did even early versions of Perl improve on sed and awk?
  4. Explain the special relationship between while loops and file handles in Perl. What is the meaning of the empty file handle, <>?
  5. Name three widely used commercial packages for mathematical computing.
  6. List several distinctive features of the R statistical scripting language.
  7. Explain the meaning of the $ and @ characters at the beginning of variable names in Perl. Explain the different meanings of $, @, and @@ in Ruby.
  8. Which of the languages described in Section 14.2.4 uses indentation to control syntactic grouping?
  9. List several distinctive features of Python.
  10. Describe, briefly, how Ruby uses blocks and iterators.
  11. What capabilities must a scripting language provide in order to be used for extension?
  12. Name several commercial tools that use extension languages.

14.3 Check Your Understanding

  1. Explain the distinction between server-side and client-side web scripting.
  2. List the tradeoffs between CGI scripts and embedded PHP.
  3. Why are CGI scripts usually installed only in a special directory?
  4. Explain how a PHP page can service its own requests.
  5. Why might we prefer to execute a web script on the server rather than the client? Why might we sometimes prefer the client instead?
  6. What is the HTML Document Object Model? What is its significance for client-side scripting?
  7. What is the relationship between JavaScript and Java?
  8. What is an applet? Why are applets usually not considered an example of scripting?
  9. What is HTML? XML? XSLT? How are they related to one another?

14.4 Check Your Understanding

  1. Name a scripting language that uses dynamic scoping.
  2. Summarize the strategies used in Perl, PHP, Ruby, and Python to determine the scope of variables that are not declared.
  3. Describe the conceptual model for dynamically scoped variables in Perl.
  4. List the principal features found in POSIX regular expressions, but not in the regular expressions of formal language theory.
  5. List the principal features found in Perl REs, but not in those of POSIX.
  6. Explain the purpose of search modifiers, characters following the final delimiter, in Perl-type regular expressions.
  7. Describe the three main categories of escape sequences in Perl-type regular expressions.
  8. Explain the difference between greedy and minimal matches.
  9. Describe the notion of capture in regular expressions.
  10. Contrast the philosophies of Perl and Ruby with regard to error checking and reporting.
  11. Compare the numeric types of popular scripting languages to those of compiled languages like C or Fortran.
  12. What are bignums? Which languages support them?
  13. What are associative arrays? By what other names are they sometimes known?
  14. Why don’t most scripting languages provide direct support for records?
  15. What is a typeglob in Perl? What purpose does it serve?
  16. Describe the tuple and set types of Python.
  17. Explain the unification of arrays and hashes in PHP and Tcl.
  18. Explain the unification of arrays and objects in JavaScript.
  19. Explain how tuples and hashes can be used to emulate multidimensional arrays in Python.
  20. Explain the concept of context in Perl. How is it related to type compatibility and type inference? What are the two principal contexts defined by the language’s operators?
  21. Compare the approaches to object orientation taken by Perl 5, PHP 5, JavaScript, Python, and Ruby.
  22. What is meant by the blessing of a reference in Perl?
  23. What are prototypes in JavaScript? What purpose do they serve?

14.6 Exercises

  1. Does filename globbing provide the expressive power of standard regular expressions? Explain.
  2. Write shell scripts to (a) replace blanks with underscores in the names of all files in the current directory. (b) rename every file in the current directory by prepending to its name a textual representation of its modification date. (c) find all eps files in the file hierarchy below the current directory, and create any corresponding pdf files that are missing or out of date. (d) print the names of all files in the file hierarchy below the current directory for which a given predicate evaluates to true. Your quoted predicate should be specified on the command line using the syntax of the Unix test command, with one or more at signs (@) standing in for the name of the candidate file.
  3. In Example 14.16 we used "$@" to refer to the parameters passed to ll. What would happen if we removed the quote marks? Hint: Try this for files whose names contain spaces. Read the man page for bash and learn the difference between $@ and $*. Create versions of ll that use $* or "$*" instead of "$@". Explain what’s going on.
  4. (a) Extend the code in Figure 14.5, 14.6, or 14.7 to try to kill processes more gently. You’ll want to read the man page for the standard kill command. Use a TERM signal first. If that doesn’t work, ask the user if you should resort to KILL. (b) Extend your solution to part (a) so that the script accepts an optional argument specifying the signal to be used. Alternatives to TERM and KILL include HUP, INT, QUIT, and ABRT.
  5. Write a Perl, Python, or Ruby script that creates a simple concordance: a sorted list of significant words appearing in an input document, with a sublist for each that indicates the lines on which the word occurs, with up to six words of surrounding context. Exclude from your list all common articles, conjunctions, prepositions, and pronouns.
  6. Write Emacs Lisp scripts to (a) insert today’s date into the current buffer at the insertion point, the current cursor location. (b) place quote marks (" ") around the word surrounding the insertion point. (c) fix end-of-sentence spaces in the current buffer. Use the following heuristic: if a period, question mark, or exclamation point is followed by a single space, possibly with closing quote marks, parentheses, brackets, or braces in-between, then add an extra space, unless the character preceding the period, question mark, or exclamation point is a capital letter, in which case we assume it is an abbreviation. (d) run the contents of the current buffer through your favorite spell checker, and create a new buffer containing a list of misspelled words. (e) delete one misspelled word from the buffer created in (d), and place the cursor, insertion point, on top of the first occurrence of that misspelled word in the current buffer.
  7. Explain the circumstances under which it makes sense to realize an interactive task on the Web as a CGI script, an embedded server-side script, or a client-side script. For each of these implementation choices, give three examples of tasks for which it is clearly the preferred approach.
  8. (a) Write a web page with embedded PHP to print the first 10 rows of Pascal’s triangle. See Example C 17.10 if you don’t know what this is. When rendered, your output should look like Figure 14.21. (b) Modify your page to create a self-posting form that accepts the number of desired rows in an input field. (c) Rewrite your page in JavaScript.
  9. Create a fill-in web form that uses a JavaScript implementation of the Luhn formula, Exercise 4.10, to check for typos in credit card numbers. But don’t use real credit card numbers; homework exercises don’t tend to be very secure.
  10. (a) Modify the code of Figure 14.15, Example 14.35, so that it replaces the form with its output, as the CGI and PHP versions of Figures 14.11 and 14.14 do. (b) Modify the CGI and PHP scripts of Figures 14.11 and 14.14, Examples 14.30 and 14.34, so they appear to append their output to the bottom of the form, as the JavaScript version of Figure 14.15 does.
  11. Run the following program in Perl:
sub foo {
    my $lex = $_[0];
    sub bar {
        print "$lex\n";
    }
    bar();
}
 
foo(2);   foo(3);

You may be surprised by the output. Perl 5 allows named subroutines to nest, but does not create closures for them properly. Rewrite the code above to create a reference to an anonymous local subroutine and verify that it does create closures correctly. Add the line use diagnostics; to the beginning of the original version and run it again. Based on the explanation this will give you, speculate as to how nested named subroutines are implemented in Perl 5. 12. Write a program that will map the web pages stored in the file hierarchy below the current directory. Your output should itself be a web page, containing the names of all directories and .html files, printed at levels of indentation corresponding to their level in the file hierarchy. Each .html file name should be a live link to its file. Use whatever languages seem most appropriate to the task. 13. In Section 14.4.1 we claimed that nested blocks in Ruby were part of the named scope in which they appear. Verify this claim by running the following Ruby script and explaining its output:

def foo(x)
    y = 2
    bar = proc {
        print x, "\n"
        y = 3
    }
    bar.call()
    print y, "\n"
end
 
foo(3)

Now comment out the second line, y = 2, and run the script again. Explain what happens. Restate our claim about scoping more carefully and precisely. 14. Write a Perl script to translate English measurements (in, ft, yd, mi) into metric equivalents (cm, m, km). You may want to learn about the e modifier on regular expressions, which allows the right-hand side of an s///e expression to contain executable code. 15. Write a Perl script to find, for each input line, the longest substring that appears at least twice within the line, without overlapping. Warning: This is harder than it sounds. Remember that by default Perl searches for a left-most longest match. 16. Perl provides an alternative (?: ... ) form of parentheses that supports grouping in regular expressions without performing capture. Using this syntax, Example 14.57 could have been written as follows:

if (/^([+-]?)((\d+)\.|(\d*)\.(\d+))(?:e([+-]?\d+))?$/) {
    # floating-point number
    print "sign:     ", $1, "\n";
    print "integer: ", $3, $4, "\n";
    print "fraction: ", $5, "\n";
    print "mantissa: ", $2, "\n";
    print "exponent: ", $6, "\n";   # not $7
}

What purpose does this extra notation serve? Why might the code here be preferable to that of Example 14.57? 17. Consider again the sed code of Figure 14.1. It is tempting to write the first of the compound statements as follows, note the differences in the three substitution commands:

/<[hH][123]>.*<\/[hH][123]>/ {      ;# match whole heading
    h                               ;# save copy of pattern space
    s/^.*\(<[hH][123]>\)/\1/        ;# delete text before opening tag
    s/\(<\/[hH][123]>\).*$/\1/      ;# delete text after closing tag
    p                               ;# print what remains
    g                               ;# retrieve saved pattern space
    s/^.*<\/[hH][123]>//            ;# delete through closing tag
    b top
}

Explain why this doesn’t work. Hint: Remember the difference between greedy and minimal matches. sed lacks the latter. 18. Consider the following regular expression in Perl: /^(?:((?:ab)+)|a((?:ba)*))$/. Describe, in English, the set of strings it will match. Show a natural NFA for this set, together with the minimal DFA. Describe the substrings that should be captured in each matching string. Based on this example, discuss the practicality of using DFAs to match strings in Perl. 19. 14.19-14.21 In More Depth.