Inspired by the original thread and the up and coming clones, here's one for the Perl community.
What are questions a good Perl programmer should be able to respond to?
Inspired by the original thread and the up and coming clones, here's one for the Perl community.
What are questions a good Perl programmer should be able to respond to?
Why is use strict
helpful?
My bellweather question is What's the difference between a list and an array?.
I also tend to like asking people to show me as many ways as they can to define a scope. There's one that people almost always forget, and another that most people think provides a scope but doesn't.
What is the difference between
if ($foo) { ... }
and
if (defined $foo) { ... }
and when should you use one over the other?
Questions
Explain the difference between these lines of code and the values of the variables.
my $a = (4, 5, 6);
my @a = (4, 5, 6);
my $b = 4, 5, 6;
my $c = @a;
What are some of Perl's greatest strengths?
use strict;
do? Why is it useful?What does the following block of code do?
print (3 + 4) * 2;
Tests
grep
using map
.perldoc
.my
and our
?my
and local
?my $x = ...
and my($x) = ...
?my($x,undef,$z) = ...
do?my(@a,@b) = (@list1, @list2)
likely a bug?What's wrong with this code?
my @array = qw/a b c d e f g h/;
for ( @array ) {
my $val = shift @array;
print $val, "\n";
}
I think brian d foy's approach is an ingenious tactic to test knowledge, understanding, and partiality about the language and the programming craft in general: What are five things you hate about your favorite language?. If they can't name 5 they probably aren't great with the language, or are totally inept at other approaches.
He applies this to people trying to a push a language: I would extend that and say it is just as applicable here. I would expect every good Perl programmer to be able to name five things they don't like. And, I would expect those five things to have some degree of merit.
Write code that builds a moderately complex data structure, say an array of hashes of arrays. How would you access a particular leaf? How would you traverse the entire structure?
For each of the following problems, how would you solve it using hashes?
My favourite question. What is following code missing:
open(my $fh, "<", "file.txt");
while (<$fh>) {
print $_;
}
close($fh);
This question should open discussion about error handling in perl. It also can be adopted to other languages too.
Some months ago, chromatic—author of Modern Perl—wrote a similar nice article “How to Identify a Good Perl Programmer,” which contains a list of questions that every good Perl programmer should be able to answer effectively.
Some of those nice questions are given below:
What’s the difference between accessing an array element with
$items[$index]
and@items[$index]
?What’s the difference between
==
andeq
?How do you load and import symbols from a Perl 5 module?
What is the difference, on the caller side, between
return;
andreturn undef;
?What is the difference between reading a file with
for
and withwhile
?
For complete details read How to Identify a Good Perl Programmer.
How is $foo->{bar}[$baz]($quux)
evaluated?
What is a lexical closure? When are closures useful? (Please, no counter-creators!)
What is the difference between list context and scalar context. How do you access each? Is there such a thing as Hash context? Maybe a little bit?
How to swap the values of two variables without using a temporary variable?
What does this one-liner print and why :
perl -pe '}{$_ = $.' file
answer: number of lines in the file, similar to wc -l
.
What's wrong with this code :
my $i;
print ++$i + ++$i;
answer: modifying a variable twice in the same statement leads to undefined behaviour.
Simple one: will the if block run :
my @arr = undef;
if (@arr) { ... }
answer: yes
How would you code a reverse()
perl builtin yourself ? You can use other perl functions.
answer: many ways. A short one: sub my_reverse {sort {1} @_})
I would also probably dig on regex, as I expect every good Perl programmer to master regex (but not just that). Some possible questions:
What is the correct way to initialize a empty string?
my $str = q{};
or
my $str = "";
or
my $str = '';