perl annoyance

February 18th, 2004 at 7:09 pm

Guess what the following code prints. Quick !

print foo();

sub foo
{
  return reverse "simpson";
}

Did you guess correctly ? Because I didn’t and it cost me a half an hour of debugging (my example was much more complicated, with foo reversing some arguments, and being pushed to an array).

Why does this print “simpson” ? I guess there is an explanation, because of the list-context-behavior of reverse. In any case, this is quite unexpected and hence annoying

Related posts:

  1. a “reverse” caveat
  2. The Jotto word game - analysis and a Perl implementation
  3. c/c++ annoyance - unsigned iteration
  4. becoming unhappy with use.perl.org

Leave a Reply

To post code with preserved formatting, enclose it in `backticks` (even multiple lines)