14

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?

Community
  • 1
  • 1
Mark Canlas
  • 9,385
  • 5
  • 41
  • 63
  • 4
    Note of warning to all who think such questions are a good idea: some of the other authors who have been creating these questions have had their accounts suspended. – Ether Jan 22 '10 at 21:21
  • 1
    @Ether I actually agree with a total deletion or negative-xp for something like this. Look at it, half of the answers are by one person, even - writing to the same topic on basic perl 101 questions. – Evan Carroll Jan 22 '10 at 21:44
  • You can get a lot out of these types of threads, the problem is I think a lot of people are more out to get a lot of exp. – Evan Carroll Jan 22 '10 at 21:52
  • 3
    “(Note that votes for any posts marked ‘community wiki’ do not generate reputation.)” http://stackoverflow.com/faq#reputation – Greg Bacon Jan 22 '10 at 21:59

22 Answers22

15

Why is use strict helpful?

Greg Bacon
  • 134,834
  • 32
  • 188
  • 245
12

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
  • 129,424
  • 31
  • 207
  • 592
  • 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 Jan 22 '10 at 21:51
  • @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 Jan 25 '10 at 08:05
11

What is the difference between

if ($foo) { ... }

and

if (defined $foo) { ... }

and when should you use one over the other?

Greg Bacon
  • 134,834
  • 32
  • 188
  • 245
5

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
  • 9,385
  • 5
  • 41
  • 63
  • 4
    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 Jan 22 '10 at 19:41
  • 1
    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 Jan 22 '10 at 20:29
  • 1
    Unfortunately for you, you're not gbacon, you could have spammed the whole question with 3 times more answers than him. – Evan Carroll Jan 22 '10 at 21:37
  • 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 Jul 10 '10 at 23:20
4
  • 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
  • 134,834
  • 32
  • 188
  • 245
  • 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 Jan 22 '10 at 21:58
  • 1
    Then what about subs that modify `$_`, `$/`, and friends? – Greg Bacon Jan 22 '10 at 22:04
  • 2
    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 Jan 22 '10 at 22:06
  • Yea, like for when i get the urge to `$[` to 1. – Evan Carroll Jan 22 '10 at 22:40
  • @Evan, I like to sprinkle this in my code: `$[ = int rand(100);` – daotoad Jan 25 '10 at 08:07
4
  • 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
  • 134,834
  • 32
  • 188
  • 245
4

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

Greg Bacon
  • 134,834
  • 32
  • 188
  • 245
4

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";
}
Matteo Riva
  • 24,728
  • 12
  • 72
  • 104
  • 3
    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 Jan 22 '10 at 20:32
  • 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 Jan 22 '10 at 20:58
  • Ow. My mind mentally shifted 'for' to 'while' and I didn't see it until I typed it in myself... – Robert P Jan 22 '10 at 22:31
3
my $a = 1;

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

print $a;

What is the value of $a at the end?

Wes
  • 1,834
  • 3
  • 15
  • 26
3

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

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

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
Greg Bacon
  • 134,834
  • 32
  • 188
  • 245
3

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.

Community
  • 1
  • 1
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
2

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
  • 134,834
  • 32
  • 188
  • 245
2

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
  • 134,834
  • 32
  • 188
  • 245
2

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
  • 28,143
  • 8
  • 66
  • 82
2

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:

  1. What’s the difference between accessing an array element with $items[$index] and @items[$index]?

  2. What’s the difference between == and eq?

  3. How do you load and import symbols from a Perl 5 module?

  4. What is the difference, on the caller side, between return; and return undef;?

  5. What is the difference between reading a file with for and with while?

For complete details read How to Identify a Good Perl Programmer.

Greg Bacon
  • 134,834
  • 32
  • 188
  • 245
Nikhil Jain
  • 8,232
  • 2
  • 25
  • 47
1

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

Greg Bacon
  • 134,834
  • 32
  • 188
  • 245
  • Wow, I've never seen this before. Where in perldoc is this explained? – Mark Canlas Jan 22 '10 at 21:18
  • 3
    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 Jan 22 '10 at 21:26
  • 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 Jan 22 '10 at 21:45
  • 2
    @Mark Canlas: also helpful: http://perlmonks.org/?node=References+quick+reference – ysth Jan 22 '10 at 22:21
1

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

Greg Bacon
  • 134,834
  • 32
  • 188
  • 245
1

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
  • 1,205
  • 2
  • 9
  • 20
1

How to swap the values of two variables without using a temporary variable?

Nikhil Jain
  • 8,232
  • 2
  • 25
  • 47
0
  • 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} @_})

Brad Gilbert
  • 33,846
  • 11
  • 78
  • 129
Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
0

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
  • 23,497
  • 17
  • 97
  • 116
-1

What is the correct way to initialize a empty string?

 my $str = q{};

or

 my $str = "";

or

 my $str = '';
ppant
  • 752
  • 9
  • 19