标 题: (zz)Perl Puzzles

  : (zz)Perl Puzzles
发信站水木社区 (Sat May 21 01:21:09 2005), 转信

http://www.mathdogs.com/people/mkc/perl-puzzles.html

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).

Rules

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

   1. What does this do?

        $foo = $foo[1]

   2. What does this print?

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

   3. In this code

        @x = ('a', 'b', 'c');
        $y = scalar ('a', 'b', 'c');
        $z = scalar @x;

      what are $y and $z set to?

   4. What does this do?

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

   5. What does this do?

        while (<STDIN>) {
            chomp;
            unless ($_) {
                print "empty line/n";
            }
        }

   6. 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.)
 
   7.  What does this do?

        $[ = 42;

   8.  What does this do?

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

   9.  What does this do?

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

  10.  What does this do

        $mt = stat($file)[8];

      assuming that $file is a valid file handle?

  11.  What will this Perl regular expression match?

        /$foo[bar]/

      Characterize all of the possibilities.

  12.  Given this subroutine

        sub somefunc {
            keys %somehash, 0;
        }

      and assuming that %somehash is defined, what is the value of somefunc()?

  13.  What does this do?

        $SIG{PIPE} = handler;

  14.  How might this expression be evaluated?

        x +2

      Enumerate all of the possibilities.

  15.  What does this do?

        @foo[1] = <STDIN>;

  16.  What does this do?

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

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

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

  18.  What does this do?

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

  19.  What does this do?

        $f = <foo[bar]>;

  20.  What does this print?

        $_ = "foo bar baz";
        --$b{(/(/w+)/)[0]};
        --$b{/(/w+)/};
        print %b;

  21.  What does this print?

        print -0.5 ** -0.5;

  22.  What does this print?

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

  23.  What does this print?

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

  24.  What does this do?

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

  25.  What does this do?

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

  26.  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.
        http://www.mathdogs.com/people/mkc/perl-puzzles-key.html

I've also taken a cut at a similar set of Python puzzles.
        http://www.mathdogs.com/people/mkc/python-puzzles.html

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.

Copyright 2001 Mike Coleman

Last modified: Fri Jun 13 23:43:54 CDT 2003 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值