Jon (j_b) wrote,

$sos = 1_1_1 x x x 1_1_1 ... 1_1_1 x x x 1_1_1;

Perl Puzzles

When discussing the relative merits of Python versus Perl, one argument I hear repeatedly in favor of Perl is that there are a lot more Perl programmers than Python programmers and that it's therefore a lot easier to hire or replace programmers as needed if one uses Perl.

One answer to this point is that if we all thought that way, we'd all be using Windows right now. But I have another answer, which is:
Almost nobody really knows Perl.

That's certainly an odd thing to assert considering how many people call themselves Perl programmers. What I mean by the statement is that Perl is such a large, arcane, quirky, enigmatic, equivocal language that very few people have sufficient mastery of it to avoid getting into trouble on a regular basis. By "trouble", I mean writing programs that don't really mean what the author thinks they mean, or writing programs with subtle, catastrophic failure modes.

Is there any truth to this assertion? As an unscientific test I've put together this collection of Perl puzzles. Try your hand at them and send me an email (anonymously if you like) or post back to the list describing your level of Perl experience and how you did with the puzzles. I will report on the results (in aggregate only).

Briefly answer each question from memory, without consulting any book, reference, Perl interpreter, etc. You must address the salient point of each puzzle in order for your answer to be considered correct; a sentence or two is sufficient in each case. Consider all of the possibilities. Assume a recent version of Perl 5, where it matters. (If you've already seen some of these puzzles at my recent talk or on a list, please answer as you would have previous to that exposure.)
The Puzzles

What does this do?

$foo = $foo[1]

What does this print?

@a = ();
$h{'a'} = 'b';
push @a, %h;
print "@a";

In this code

@x = ('a', 'b', 'c');
$y = scalar ('a', 'b', 'c');
$z = scalar @x;
what are $y and $z set to?

What does this do?

$c = 'foo';
$a = "$c='\'n";
$b = '$c='\'n';

What does this do?

while (<STDIN>) {
unless ($_) {
print "empty linen";

Characterize the possible outcomes of this code

@s = sort foo (1, 4, 3, 2);
assuming that foo is a user-defined subroutine.

(This question is semi-obsolete. It was prompted by an issue that existed up to version 5.004 but probably doesn't exist in versions 5.005 and up. See the answer key for more info.)

What does this do?

$[ = 42;

What does this do?

$x = (sort => (4, 8, 6));
$y = sort => (4, 8, 6) ;

What does this do?

print (1+2)+3;
print +(1+2)+3;

What does this do

$mt = stat($file)[8];
assuming that $file is a valid file handle?

What will this Perl regular expression match?

Characterize all of the possibilities.

Given this subroutine

sub somefunc {
keys %somehash, 0;
and assuming that %somehash is defined, what is the value of somefunc()?

What does this do?

$SIG{PIPE} = handler;

How might this expression be evaluated?

x +2
Enumerate all of the possibilities.

What does this do?

@foo[1] = <STDIN>;

What does this do?

$i = 0;
do {
$done = foo();
if ($done) { last; }
print "x";
} while ($i < 10);

Explain when each of "exists" or "defined" will be printed.

print "exists" if exists $foo{$bar};
print "defined" if defined $foo{$bar};

What does this do?

for ($i=0; $i<10; $i++) {
if (<STDIN>) { print; }

What does this do?

$f = <foo[bar]>;

What does this print?

$_ = "foo bar baz";
print %b;

What does this print?

print -0.5 ** -0.5;

What does this print?

@a = (0 => 0);
print ~$a[0], " ", ~$a[1];

What does this print?

$a[0] = 7;
$a[1] = 8;
@b = (5 x @a);
@c = ((5) x @a);
print "@b @c";

What does this do?

$print_blanks = 1;
while (<>) {
next unless length & $print_blanks;

What does this do?

$n = 5;
while (<>) {
if (1 .. $n) { print; }

What does this do?

$sos = 1_1_1 x x x 1_1_1 ... 1_1_1 x x x 1_1_1;
Here is the answer key.

I've also taken a cut at a similar set of Python puzzles.
Acknowledgements: Most of these puzzles are inspired by or lifted from Programming Perl by Larry Wall et al. Kudos to them for pointing out the weaknesses as well as strengths of Perl. Also, thanks to AP for pointing out several errors.
© 2001 Mike Coleman
Last modified: Fri Jun 13 23:43:54 CDT 2003
  • Post a new comment


    default userpic

    Your reply will be screened

    Your IP address will be recorded