http://qs321.pair.com?node_id=11114843


in reply to declaring lexical variables in shortest scope: performance?

As well as what others have said, remember that Perl does have optimizations in place for common idioms. For example

my @things = ( ... ); # some list, whatever @things = sort @things;

You may think that sort gets passed @things, then returns the sorted list of things, and that gets assigned back to the @things array as a list assignment. But you'd be wrong. Perl notices that you're sorting an array and assigning it back to itself, and uses an optimized code path that doesn't involve having to build a new list and do list assignment; it does an in-place sort.

Common idioms do get optimized for when possible, so there are benefits to sticking with them.

With for my $var (...) {...}, Perl knows that $var won't be leaking outside the body of the loop, so can at least potentially optimize based on that.