tags:

views:

542

answers:

18

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?

+4  A: 
  • What is the difference between my and our?
  • What is the difference between my and local?
  • For the above, when is it appropriate to use one over the other?
Greg Bacon
I don't think a good perl programmer needs to know a practice that is almost entirely evil, like `local`. There isn't but one or two sane applications of it.
Evan Carroll
Then what about subs that modify `$_`, `$/`, and friends?
Greg Bacon
There might be only one sane application of local, but it's the reason for being there. At some point you'll want to change something in the symbol table and restore it on the way out. There's nothing evil in limiting the scope of your changes. It's a lot better than the alternative.
brian d foy
Yea, like for when i get the urge to `$[` to 1.
Evan Carroll
gbacon: for $_ - "my $_;".
Alexandr Ciornii
@Evan, I like to sprinkle this in my code: `$[ = int rand(100);`
daotoad
+5  A: 

Questions

  • What is a reference?
  • How does Perl implement object orientation?
  • How does Perl's object orientation differ from other languages like C# and Java?
  • Traditional object orientation in core Perl has largely been superseded by what?
    • Why?
  • What is the difference between a package and a module?
  • What features were implemented in 5.10?
  • What is a Schwartzian transform?
  • 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?

  • What are some of Perl's greatest weaknesses?
  • Name several hallmarks of the "modern Perl" movement.
  • What does the binding operator do?
  • What does the flip-flop operator do?
  • What is the difference between for and foreach?
  • What makes Perl difficult to parse?
  • What are prototypes?
  • What is AUTOLOAD?
  • What is the Perl motto?
    • Why is this a problem?
  • What does use strict; do? Why is it useful?
  • What does the following block of code do?

    print (3 + 4) * 2;
    

Tests

  • Implement grep using map.
  • Implement and use a dispatch table.
  • Given a block of text, replace a word in that block with the return value of a function that takes that word as an argument.
  • Implement a module, including documentation compatible with perldoc.
  • Slurp a file.
  • Draw a table that illustrates Perl's concept of truthiness.
Mark Canlas
These are good, but some are more obscure than necessary. Certainly you can be a very good perl programmer without having memorized the new 5.10 change log, or even knowing what the flip-flop operator is. I'm reading this list and thinking it represents the result of an agenda instead of a helpful tutorial.
Andy Ross
If you continue to use an older Perl for most of your work, you aren't going to be able to name all the 5.10 goodies. The flip-flop is hardly an important tool. I've only seen it used a few times, and never needed it in 10 years. The main reason to know about it so that you don't do `sub { 1..10 };`.
daotoad
Unfortunately for you, you're not gbacon, you could have spammed the whole question with 3 times more answers than him.
Evan Carroll
The only one I didn't know here is flip-flop. Naturally, I don't think you have to answer that question to be a good Perl programmer :). There are few others I disagree with, but it's not a bad list. (Pretty sure you don't have to be up on 5.10 or Moose to be a good Perl programmer, since I personally know several individuals who both don't know 5.10 or Moose, and are great Perl programmers).
masonk
+13  A: 

Why is use strict helpful?

Greg Bacon
It increases your CPANTS ratings?
Evan Carroll
@Evan Carroll: if you are shooting for a minimum rating (e.g. FileKGlob), that's not helpful :)
ysth
+3  A: 
  • What are list context and scalar context?
  • What is the difference between my $x = ... and my($x) = ...?
  • What does my($x,undef,$z) = ... do?
  • Why is my(@a,@b) = (@list1, @list2) likely a bug?
  • How can a user-defined sub know whether it was called in list or scalar context? Give an example of when it makes sense for the same sub to return different values in one context or the other.
Greg Bacon
+3  A: 
my $a = 1;

if($a) {
    my $a = 2;
}

print $a;

What is the value of $a at the end?

Wes
+2  A: 

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?

Greg Bacon
+10  A: 

What is the difference between

if ($foo) { ... }

and

if (defined $foo) { ... }

and when should you use one over the other?

Greg Bacon
+4  A: 

What is the difference between /a(.*)b/ and /a(.*?)b/?

Greg Bacon
+2  A: 

For each of the following problems, how would you solve it using hashes?

  • Compute set relationships, e.g., union, intersection, mutual exclusion.
  • Find unique elements of a list.
  • Write a dispatch table.
Greg Bacon
+3  A: 

What's wrong with using a variable as a variable name?

Study guide: Part 1, Part 2, and Part 3.

Greg Bacon
+4  A: 

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";
}
kemp
It's funny: I would have thought it was obvious that you don't want to tinker with the array as you iterate over it, but the question has come up again and again here and on another forum (Linux Questions) in the last few months.
Telemachus
It's also an Item that Josh McAdams added to the upcoming new edition of Effective Perl Programming since people seem to do it so often. :)
brian d foy
Ow. My mind mentally shifted 'for' to 'while' and I didn't see it until I typed it in myself...
Robert P
+11  A: 

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.

brian d foy
So many people don't get this. But it is key.
daotoad
I'm still pretty horrible at scope. What are the various ways?
Mark Canlas
No spoilers please. Let people figure it out on their own. I'll make a blog post about it next week for those who are still stumped. :)
brian d foy
@Mark, it's in the perl FAQ, it is also covered here on SO. I've even sawed away at the importance of understanding this very fact in a number of posts.
daotoad
+1  A: 

How is $foo->{bar}[$baz]($quux) evaluated?

Greg Bacon
Wow, I've never seen this before. Where in perldoc is this explained?
Mark Canlas
perlreftut, it takes the hash that hashref `$foo` points to, gets the value for key `bar`, assumes the value is an array and retrieves the index stored in scalar `$baz`, and then assumes that array element is a subref and calls it with the scalar `$quux` argument.
Evan Carroll
Also in perlref, in the "Using References" section: "The arrow is optional between brackets subscripts …" http://perldoc.perl.org/perlref.html#Using-References
Greg Bacon
@Mark Canlas: also helpful: http://perlmonks.org/?node=References+quick+reference
ysth
+3  A: 

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.

Evan Carroll
+1  A: 

What is a lexical closure? When are closures useful? (Please, no counter-creators!)

Greg Bacon
+2  A: 

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.

Ivan Nevostruev
missing: `use autodie;`
Evan Carroll
It depends on your needs. You can write it as `if (open(...)) {...} else { print "file not found" }`
Ivan Nevostruev
+1  A: 

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?

dpb
A: 

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 are lookahead and lookbehind assertion /modifierq?
  • how do you check that two individual parts of a regex are identical ?
  • what means greedy ?
  • what are Posix character classes ?
  • what does \b match ?
  • what is the use of \c modifier ?
  • how do you precompile a regex ?
kriss
I feel bad, I can't even answer some of these questions, haha.
Mark Canlas
try perldoc perlre to feel better ;-)
kriss