Scripting Languages CS 5142, Fall 2013 hw03


Reading Assignments

Concept Questions

hw03_1 Static and dynamic scoping

You can answer the “what does it print” questions below by actually running the code and checking the output. However, before you do that, it is recommended that you first try to figure them out by thinking.
1a.
(2 points) Consider the following Perl script:
my $a = 1;
sub callee { print "$a\n"; }
sub outer { my $a = 2; callee(); }
outer();
What does it print? How does the print statement look up variable $a?
1b.
(2 points) Consider the following Perl script:
local $a = 1;
sub callee { print "$a\n"; }
sub outer { local $a = 2; callee(); }
outer();
What does it print? How does the print statement look up variable $a?
1c.
(2 points) Consider the following Perl script:
our $a = 1;
sub outer { our $a = 2; }
outer();
print "$a\n";
What does it print? How does the print statement look up variable $a?
1d.
(2 points) Consider the following Perl script:
my $a = 1;
sub outer {
  my $a = 2;
  sub inner { print "$a\n"; }
  inner();
}
outer();
What does it print? How does the print statement look up variable $a?

hw03_2 Regular expressions

2a.
(4 points) Consider the following Perl regular expression: ([ab]c?[d-f])+. Rewrite this regular expression using only the “essential” features of formal regular expressions. For example, a bracketed character classes such as [ab] is not an essential feature, and should be expressed using only the essential features, such as (a|b). The essential features are concatenation, choice (|), Kleene start (*), and grouping.
2b.
(4 points) Write a regular expression that captures the PID and CMD from a line of output of the ps command on Linux. For example, consider the following output:
energon1:~> ps
  PID TTY          TIME CMD
 4702 pts/2    00:00:00 bash
 4739 pts/2    00:00:00 ps
The PID of bash is 4702, and the PID of ps is 4739.

hw03_3 Learning how to learn a language

This question walks you through the first few steps on Slide 23 (“How to learn a language”) from the lecture on 9/6/2013.
3a.
I. Use peers & gurus.
Ask around among your friends to find out who knows Perl. Try learning things for yourself, but if you get stuck, ask someone for help.

II. Install tools.
(1 point) To answer this question, you will need to know where Perl is installed, and how to run it. Here is an example script:

#!/usr/bin/env perl
use strict;
use warnings;
my %ext2names;
while (my $line = <>) {
  chomp $line;
  next if $line =~ /^total/ || $line =~ /^[^ ]+-{6}/;
  my ($name) = $line =~ /(\S+)$/;
  next unless $name;
  my ($ext) = $name =~ /([^.]+)$/;
  $ext = 'no extension' if $ext eq $name;
  $ext2names{$ext} = [ ] unless $ext2names{$ext};
  push @{$ext2names{$ext}}, $name;
}
for my $ext (sort keys %ext2names) {
  print "$ext\n";
  for my $name (sort @{$ext2names{$ext}}) {
    print "  $name\n";
  }
}
The input data is a directory listing as produced by the ls -l command, for example:
total 7376
rw-r--r--  1 bobby  team   684533 May 29 07:21 Alfa.pdf
rw-r--r--  1 bobby  team   400525 May 31 14:14 Bravo.pdf
rw-r--r--  1 bobby  team     4304 May 23 11:14 Charlie.html
rw-r--r--  1 bobby  team    16502 May 30 10:34 Delta
rw-------  1 bobby  team     2257 Jun  5 21:53 Echo.html
rw-r--r--  1 bobby  team     9659 Jun  5 07:05 index.html
Run the script. What does it print? (To answer this question, you need to put the script in a file, for example, test.pl, and the input into another file, for example, input.txt. Then, you do perl test.pl < input.txt).
3b.
III. Read tutorial.
This was last week's reading assignment: Kirrily “Skud” Robert. perlintro(1) man page. Perl 5 documentation. Available on machines that have Perl properly installed, and also available online at http://perldoc.perl.org/perlintro.html

IV. Find language & library reference.
(3 points) The easiest place to look are the man pages, including perlsyn, perlop, perlfunc, perlre. See the perl man page for an overview. Which man page describes next? Which man page describes the \S character class? Which man page describes the unless statement modifier?

3c.
V. Read example programs.
(4 points) Read the example Perl script from question 3a in detail. What does the code in the while loop do? What does the code in the for loops do? Keep your answer brief.

Programming exercises

VI. Write example programs: I/O, types, control flow, libraries.
Questions hw03_4 and hw03_5 together cover this step.

VII. Understand error messages.
While you solve the programming questions, you are likely to encounter some error messages from the Perl interpreter. To improve your learning experience, you might want to write them down, and try to really understand what they mean.

hw03_4 Listing files by month

(8 points) Write a Perl script that groups file names in a directory listing by month. Given the example input data from question 3a, your program should print the following output:
Jun
  Echo.html
  index.html
May
  Alfa.pdf
  Bravo.pdf
  Charlie.html
  Delta
The output should be alphabetical per month (note that Jun comes before May, and Alpha.pdf comes before Bravo.pdf). The output should include all files, irrespective of permissions (note that Echo.html is included even though it is private). You do not need to do error-checking for whether the input is in the correct format. Hint: you can use the program from question 3a as a starting point.

hw03_5 Adding arrays

(8 points) One of the features of Perl for which you have not yet written an example program is subroutines. Write a subroutine addArrays that adds two arrays. The parameters are the references to two arrays. After the subroutine returns, the first array should contain the element-by-element sum, and the second array should be unmodified. For example, consider the following test driver:

#!/usr/bin/env perl
use warnings;
use strict;
# --- define sub addArrays here ---
sub printArray {
  my ($label, $r) = @_;
  print "$label =";
  for my $val (@$r) { print " $val"; }
  print "\n";
}
my @a = (2,3,4);
printArray "a", \@a;
addArrays \@a, [1,0,2];
printArray "a + [1,0,2]", \@a;
addArrays \@a, [2,2];
printArray "a + [1,0,2] + [2,2]", \@a;
addArrays \@a, [0,0,3,2,3];
printArray "a + [1,0,2] + [2,2] + [0,0,3,2,3]", \@a;
Your program should print the following output:
a = 2 3 4
a + [1,0,2] = 3 3 6
a + [1,0,2] + [2,2] = 5 5 6
a + [1,0,2] + [2,2] + [0,0,3,2,3] = 5 5 9
Hint: @$r turns reference $r into an array, and \@a turns array @a into a reference. Note that your subroutine should be robust to mismatched array sizes, by only adding up to the largest index that both arrays have in common.

VIII. Practice.
If you want to become an expert Perl programmer, write lots of Perl programs on your own, outside of the homework assignments. For example, you could check a matrix for symmetry. Or you could read a CSV file, and compute per-column statistics such as average or standard deviation.


http://www.cs.cornell.edu/Courses/cs5142/2013fa/hw03.html