Does perl optimize foreach over a range?

Viewed 566

Given a simple program:

use strict;
use warnings;

my $count = 0;

foreach ('a' .. 'zzzzzzzzzz') {
    $count++;
}

print "$count\n";

Will perl create entire range of about 1.4+e14 elements in memory and then iterate over them or does it have any internal optimizations to simply step over one state of range operator at a time?

Well, I've actually ran that, so empirical data from top shows it is the latter (and it didn't finish yet to see data from time as well), but is it documented anywhere or where to find confirmation for that in sources?

2 Answers
Related