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?
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?
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?
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?
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.
II. Install tools.
(1 point) Perl is already installed
on the CIMS Linux machines, and if you have access to other Linux
machines, it is likely that it already pre-installed there too.
To answer this question, you will need to know where it is and
how to use 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.htmlRun 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).
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?
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.
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.
(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.