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?

4
  • 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, 2010 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. Jan 22, 2010 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. Jan 22, 2010 at 21:52
  • 3
    “(Note that votes for any posts marked ‘community wiki’ do not generate reputation.)” stackoverflow.com/faq#reputation
    – Greg Bacon
    Jan 22, 2010 at 21:59

22 Answers 22

15

Why is use strict helpful?

3
  • 1
    It increases your CPANTS ratings? Jan 22, 2010 at 21:59
  • @Evan Carroll: if you are shooting for a minimum rating (e.g. FileKGlob), that's not helpful :)
    – ysth
    Jan 22, 2010 at 22:15
  • Really?? This question should be correctly answered by ANY Perl programmer. I mean, if someone doesn't have a full correct answer, he is simply NOT a Perl programmer, let alone good or bad. Sep 23, 2015 at 7:27
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.

2
  • 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. :) Jan 22, 2010 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, 2010 at 8:05
11

What is the difference between

if ($foo) { ... }

and

if (defined $foo) { ... }

and when should you use one over the other?

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.
4
  • 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, 2010 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, 2010 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. Jan 22, 2010 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, 2010 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?
5
  • 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. Jan 22, 2010 at 21:58
  • 1
    Then what about subs that modify $_, $/, and friends?
    – Greg Bacon
    Jan 22, 2010 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. Jan 22, 2010 at 22:06
  • Yea, like for when i get the urge to $[ to 1. Jan 22, 2010 at 22:40
  • @Evan, I like to sprinkle this in my code: $[ = int rand(100);
    – daotoad
    Jan 25, 2010 at 8: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.
4

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

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";
}
3
  • 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, 2010 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. :) Jan 22, 2010 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, 2010 at 22:31
3
my $a = 1;

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

print $a;

What is the value of $a at the end?

3

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

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

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.

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?

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.
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.

1
  • 2
    It depends on your needs. You can write it as if (open(...)) {...} else { print "file not found" } Jan 22, 2010 at 23:07
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.

1

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

4
  • Wow, I've never seen this before. Where in perldoc is this explained? Jan 22, 2010 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. Jan 22, 2010 at 21:26
  • Also in perlref, in the "Using References" section: "The arrow is optional between brackets subscripts …" perldoc.perl.org/perlref.html#Using-References
    – Greg Bacon
    Jan 22, 2010 at 21:45
  • 2
    @Mark Canlas: also helpful: perlmonks.org/?node=References+quick+reference
    – ysth
    Jan 22, 2010 at 22:21
1

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

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?

1

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

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} @_})

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 ?
2
  • I feel bad, I can't even answer some of these questions, haha. Jul 13, 2010 at 15:06
  • try perldoc perlre to feel better ;-)
    – kriss
    Jul 13, 2010 at 16:45
-1

What is the correct way to initialize a empty string?

 my $str = q{};

or

 my $str = "";

or

 my $str = '';

Not the answer you're looking for? Browse other questions tagged or ask your own question.