Name email Question 1: What's output by this Perl code? @days = ("Partridges","Turtle Doves","French Hens","Calling Birds"); ($one) = @days; $two = @days; print "$two $one\n";
4 partridges (Partridges,Turtle Doves,French Hens,Calling Birds) PartridgesTurtle DovesFrench HensCalling Birds PartridgesTurtle DovesFrench HensCalling Birds PartridgesTurtle DovesFrench HensCalling Birds PartridgesTurtle DovesFrench HensCalling Birds Partridges Turtle Doves French Hens Calling Birds An error message Question 2: Which of these is a variable name that you should not use? $b $d $do $f $if $fi Question 3: Which of these is incorrect syntax? if ($n == 1) { print "Just one in stock\n"; } $n == 1 and print "Just one in stock\n"; print "Just one in stock\n" if ($n==1); if ($n == 1) print "Just one in stock\n"; ($n == 1) && print "Just one in stock\n"; Question 4: Which of these is not a correct statement? Using a my variable supresses errors if you're using strict My variables limit the reach of a variable's name to the block in which they're declared It's good practise to declare almost all variables as my My declarations only make a difference if you're using strict Question 5: How many times does this code print out "Hello friend"? $name = "Graham"; if ($name == "Lisa") { print "Hello friend\n"; } if ($name eq "Lisa") { print "Hello friend\n"; } if ($name ~~ "Lisa") { print "Hello friend\n"; } if ($name = "Lisa") { print "Hello friend\n"; } if ($name =~ /Lisa/) { print "Hello friend\n"; }
0 1 2 3 4 5 Question 6: What will this print out? open source,"railstats.xyz"; @data = <source>; print $#data+1,"\n";
0 if the file railstats.xyz can't be read, otherwise the number of lines in the file 1 if the file railstats.xyz can't be read, otherwise the number of lines in the file an error message if the file railstats.xyz can't be read, otherwise the number of lines in the file an error message if the file railstats.xyz can't be read, otherwise the first line in the file an error message if the file railstats.xyz can't be read, otherwise the second line in the file Question 7: What is the difference between these statements: die ("Unable to read file"); die ("Unable to read file\n");
The first one doesn't print a new line at the end of the message, but the second one does They produce identical output as die adds a new line if you don't supply one The first one tells you the line number your code failed on (and other debug info), the second does not. Question 8: What might the following command line generate? perl -na -F'\t' -e 'print "$F[2], " if /Farnborough/' railstats.xyz
syntax error at -e line 1, at EOF; Execution of -e aborted due to compilation errors. aa Farnborough North Farnborough (Main) GU14 2PL, GU14 7NL, Question 9: In a Perl regular expression, "p*" means match one or more p's match zero or more p's match zero or one p match p followed by anything match p followed by a * Question A: Which of these will NOT print "10kgs"? $k = 10; print "$kkgs\n"; $k = 10; print "${k}kgs\n"; $k = 10; print $k."kgs\n"; $k = 10; print $k,"kgs\n"; Question B: How have you found these questions? Your answer to this question will help YOU to decide whether you should join the Perl course for the lead in day that covers the fundamentals, or start with the quick reminder of the basics on the second morning. I found them all straightforward; most of the answers were obvious to me. They were mostly quite easy, but a few got me thinking / used things I hadn't come across I knew some, but others were a struggle. A few were clear to me, but most of them were headscratchers. I found that I had to make far too many guesses for my liking ... When done, please