标 题: (zz)Perl Puzzles

:(zz)PerlPuzzles
发信站:水木社区(SatMay2101:21:092005),转信

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

PerlPuzzles

WhendiscussingtherelativemeritsofPythonversusPerl,oneargumentIhearrepeatedlyinfavorofPerlisthattherearealotmorePerlprogrammersthanPythonprogrammersandthatit'sthereforealoteasiertohireorreplaceprogrammersasneededifoneusesPerl.

Oneanswertothispointisthatifweallthoughtthatway,we'dallbeusingWindowsrightnow.ButIhaveanotheranswer,whichis:

AlmostnobodyreallyknowsPerl.

That'scertainlyanoddthingtoassertconsideringhowmanypeoplecallthemselvesPerlprogrammers.WhatImeanbythestatementisthatPerlissuchalarge,arcane(
神秘),quirky,enigmatic(费解),equivocal(模棱两可)languagethatveryfewpeoplehavesufficientmasteryofittoavoidgettingintotroubleonaregularbasis.By"trouble",Imeanwritingprogramsthatdon'treallymeanwhattheauthorthinkstheymean,orwritingprogramswithsubtle,catastrophic(灾难性的)failuremodes.

Isthereanytruthtothisassertion?AsanunscientifictestI'veputtogetherthiscollectionofPerlpuzzles.Tryyourhandatthemandsendmeanemail(anonymouslyifyoulike)orpostbacktothelistdescribingyourlevelofPerlexperienceandhowyoudidwiththepuzzles.Iwillreportontheresults(inaggregateonly).

Rules

Brieflyanswereachquestionfrommemory,withoutconsultinganybook,reference,Perlinterpreter,etc.Youmustaddressthesalient(
显著)pointofeachpuzzleinorderforyouranswertobeconsideredcorrect;asentenceortwoissufficientineachcase.Considerallofthepossibilities.AssumearecentversionofPerl5,whereitmatters.(Ifyou'vealreadyseensomeofthesepuzzlesatmyrecenttalkoronalist,pleaseanswerasyouwouldhaveprevioustothatexposure.)

ThePuzzles

1.Whatdoesthisdo?

$foo=$foo[1]

2.Whatdoesthisprint?

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

3.Inthiscode

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

whatare$yand$zsetto?

4.Whatdoesthisdo?

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

5.Whatdoesthisdo?

while(<STDIN>){
chomp;
unless($_){
print"emptyline\n";
}
}

6.Characterizethepossibleoutcomesofthiscode

@s=sortfoo(1,4,3,2);

assumingthatfooisauser-definedsubroutine.

(Thisquestionissemi-obsolete.Itwaspromptedbyanissuethatexisteduptoversion5.004butprobablydoesn'texistinversions5.005andup.Seetheanswerkeyformoreinfo.)

7.Whatdoesthisdo?

$[=42;

8.Whatdoesthisdo?

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

9.Whatdoesthisdo?

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

10.Whatdoesthisdo

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

assumingthat$fileisavalidfilehandle?

11.WhatwillthisPerlregularexpressionmatch?

/$foo[bar]/

Characterizeallofthepossibilities.

12.Giventhissubroutine

subsomefunc{
keys%somehash,0;
}

andassumingthat%somehashisdefined,whatisthevalueofsomefunc()?

13.Whatdoesthisdo?

$SIG{PIPE}=handler;

14.Howmightthisexpressionbeevaluated?

x+2

Enumerateallofthepossibilities.

15.Whatdoesthisdo?

@foo[1]=<STDIN>;

16.Whatdoesthisdo?

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

17.Explainwheneachof"exists"or"defined"willbeprinted.

print"exists"ifexists$foo{$bar};
print"defined"ifdefined$foo{$bar};

18.Whatdoesthisdo?

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

19.Whatdoesthisdo?

$f=<foo[bar]>;

20.Whatdoesthisprint?

$_="foobarbaz";
--$b{(/(\w+)/)[0]};
--$b{/(\w+)/};
print%b;

21.Whatdoesthisprint?

print-0.5**-0.5;

22.Whatdoesthisprint?

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

23.Whatdoesthisprint?

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

24.Whatdoesthisdo?

$print_blanks=1;
while(<>){
nextunlesslength&$print_blanks;
print;
}

25.Whatdoesthisdo?

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

26.Whatdoesthisdo?

$sos=1_1_1xxx1_1_1...1_1_1xxx1_1_1;

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

I'vealsotakenacutatasimilarsetofPythonpuzzles.
http://www.mathdogs.com/people/mkc/python-puzzles.html

Acknowledgements:MostofthesepuzzlesareinspiredbyorliftedfromProgrammingPerlbyLarryWalletal.KudostothemforpointingouttheweaknessesaswellasstrengthsofPerl.Also,thankstoAPforpointingoutseveralerrors.

Copyright2001MikeColeman

Lastmodified:FriJun1323:43:54CDT2003

你可能感兴趣的:(C++,c,python,C#,perl)