(I was going to call this post “How well do you understand context?” but I think this title is more accurate).
I just saw someone recommending this code:
$reversed = reverse(split //, $string);
Looks sensible enough, doesn’t it? But it isn’t. What’s the hidden inefficiency?
This generated a lot of discussion in a training course that I ran this week so I thought it was worth sharing more widely.
I think you can say that you understand the concept of context in Perl if you know what these four statements will print and (more importantly) can explain why they don’t all produce the same thing.
print reverse 1 .. 5;
print scalar reverse 1 .. 5;
print reverse 12345;
print scalar reverse 12345;