Perl resources, modules, and sample code

How to Identify a Good Perl Programmer, Answer Key

Monday, 17 January 2011—subject to ongoing, unnanounced updates

Modern Perl Modern Perl at Powell’s, Modern Perl at Barnes and Noble, Modern Perl at Amazon.com, or download Modern Perl for free and then recommend it to your friends, write a great review, or otherwise spread the word.

Re: How to Identify a Good Perl Programmer

These aren’t authoritative. Just felt like trying my hand since chromatic said a good Perl 5 programmer should know 80%. Woo-woo.

  • What do variable sigils indicate in Perl 5?
    • The underlying content. % ➙ hash, @ ➙ array, $ ➙ scalar. The trick perhaps being that a scalar variable can hold just about anything as a reference; from an object to a regular expression.
  • What's the difference between accessing an array element with $items[$index] and @items[$index]?
    • The first is right. :) The second is a single item array slice and is deprecated.
  • What's the difference between == and eq?
    • Numeric vs stringwise equality comparison in that order. The stringwise operators are spelled with letters; e.g., lt, ge, cmp.
  • What do you get if you evaluate a hash in list context?
    • All its elements—in key, value pairs—in whatever order perl thinks they are best kept.
  • How do you look up keywords in the Perl documentation?
  • What is the difference between a function and a method in Perl 5?
    • Well… there isn’t any exactly. A method expects, or needs, the first argument to be a class or object. The calling class or object is passed as the first argument which looks like: Class::Name->method_name($arg) or $object->method_name($arg). This does make the following the same in some cases: method_name($object, $arg).
  • When does Perl 5 reclaim the memory used by a variable?
    • When it and all of its referents go out of scope.
  • How do you ensure that the default scope of a variable is lexical?
    • Declare it with my.
  • How do you load and import symbols from a Perl 5 module?
    • use Module::Name ( $optional, $args )
    • or require + import.
  • How can you influence the list of directories from which perl attempts to load modules?
    • By altering @INC. There are quite a few ways to do that. The lib pragma and PERL5LIB environment variables being the two most straightforward; well, that and execution directory.
  • How do you look up error messages in the Perl 5 documentation? (Award bonus points for knowing how to enable explanations of all error messages encountered.)
    • Um… I’m not sure. perldoc -q error points to perlfaq8. You can use diagnostics to make your warnings and errors much more verbose.
  • What happens when you pass an array into a function?
    • It’s available in @_.
  • How do you pass one or more distinct arrays into a function?
    • Array references like function(\@one,\@two) or I think you could do it by using a prototype, which is almost always a bad idea.
  • What is the difference, on the caller side, between return; and return undef;?
    • The first unrolls by context to an undef scalar or an empty list (). The second, always to undef.
  • Where do tests go in a standard CPAN distribution?
    • t/.
  • How do you run tests in a standard CPAN distribution?
    • prove -l t/ (and I like the -v as well).
  • What command do you use to install new distributions from the CPAN?
    • cpanm, believe it.
  • Why would you use the three-argument form of the open builtin?
    • Security, robustness, and quoting.
  • How do you detect (and report) errors from system calls such as open? (Award bonus points for knowing how to enable automatic detection and reporting of errors.)
    • They return true on success. use autodie to turn the failures into fatals.
  • How do you throw an exception in Perl 5?
    • die, well croak or confess if you please.
  • How do you catch an exception in Perl 5?
    • eval {} or maybe use Try::Tiny is the right choice at this point.
  • What is the difference between reading a file with for and with while?
    • All at once or one record at a time.
  • How do you handle parameters within a method or function in Perl 5?
    • Uh… what? You take them or copy them out of @_.
  • What do parentheses around the variable name in my ($value) = @_; mean, and what would happen if you removed them?
    • They keep the list context. Without them you get the size of @_.
  • Is new a builtin function/keyword?
    • No.
  • How do you read the documentation of a core library? A CPAN module?
    • perldoc Module::Name.
  • How do you access only the values of a Perl 5 hash?
    • values %hash.

So… Did I get the part?