The answers for all exercises can be found in Section A.6.
Using the glob operator, a naive sort of every name in the /bin directory by their relative sizes might be written as:
my @sorted = sort { -s $a <=> -s $b } glob "/bin/*";
Rewrite this using the Schwartzian Transform technique.
If you don't have many files in the /bin directory, perhaps because you don't have a Unix machine, change the argument to glob as needed.
Read up on the Benchmark module, included with Perl. Write a program that will answer the question, "How much does using the Schwartzian Transform speed up the task of Exercise 1?"
Using a Schwartzian Transform, read a list of words, and sort them in "dictionary order." Dictionary order ignores all capitalization and internal punctuation. Hint: The following transformation might be useful:
my $string = "Mary-Ann"; $string =~ tr/A-Z/a-z/; # force all lowercase $string =~ tr/a-z//cd; # strip all but a-z from the string print $string; # prints "maryann"
Be sure you don't mangle the data! If the input includes the Professor, and The skipper, the output should have them listed in that order, with that capitalization.
Modify the recursive directory dumping routine so it shows the nested directories through indentation. An empty directory should show up as:
sandbar, an empty directory
while a nonempty directory should appear with nested contents, indented two spaces:
uss_minnow, with contents: anchor broken_radio galley, with contents: captain_crunch_cereal gallon_of_milk tuna_fish_sandwich life_preservers
Copyright © 2003 O'Reilly & Associates. All rights reserved.