The current documentation for the [Range operator `..`](http://perldoc.perl.org/perlop.html#Range-Operators) states that it will not burn up memory for [counting loops](http://www.perlmonks.org/?node_id=743655):

> 
> ... The range operator is useful for writing `foreach (1..10)` loops and for doing slice operations on arrays. **In the current implementation, no temporary array is created** when the range operator is used as the expression in foreach loops, but older versions of Perl might burn a lot of memory when you write something like this:
> 
> 
> 
> ```
>     1.   for (1 .. 1_000_000) {
>     2.       # code
>     3.   }
> 
> ```
> 
> 

Because of the aforementioned early implementations of `for (MIN .. MAX)`, I still come by experts who are wary of using counting loops because they believe it is equivalent to:

```
my @temp_array = (MIN .. MAX);       # Needlessly using up memory
for (@temp_array) {

```

Versus the more logical and memory efficient:

```
for ($_ = MIN; $_ <= MAX; $_++) {    # Logical counting from MIN to MAX

```

Questions: 

* Is there a way that one could go about proving that a counting loop does not waste memory?
* Does anyone know which versions of Perl had the memory issue and when it was fixed?

I'm able to prove to myself that counting loops don't waste memory using the below one-liner which would certainly crash my system if it was actually creating a temporary array. However, it would be nice if there was more conclusive information on the subject so that we could put this old-wives tale to rest.

```
$ perl -e 'for (1 .. 1_000_000_000_000_000) { print "$_\n"; last if $_ == 5 }'
1
2
3
4
5

```

---

### Solution

Each of the three below answers go part of the way to explaining this issue:

1. [ikegami](https://stackoverflow.com/users/589924/ikegami)'s [answer](https://stackoverflow.com/a/25214988/1733163) thoroughly breaks down different types loops and demonstrates how counting loops differ on the front and back end. ☆
2. [friedo](https://stackoverflow.com/users/20745/friedo)'s [answer](https://stackoverflow.com/a/25214811/1733163) shows how to use `top` to monitor memory usage.
3. [Borodin](https://stackoverflow.com/users/622310/borodin)'s [answer](https://stackoverflow.com/a/25221144/1733163) addresses the second part of my query about when this no longer became an issue:
	* [`perlop v5.4_68`](https://metacpan.org/pod/release/GSAR/perl5.004_68/pod/perlop.pod#Range-Operators) (released on 23 June 1998) warns a temporary array is used.
	* [`perlop v5.4_69`](https://metacpan.org/pod/release/GSAR/perl5.004_69/pod/perlop.pod#Range-Operators) (released on 29 June 1998) states a temporary array is no longer used.
	* [`perldelta v5.4_71`](https://metacpan.org/pod/release/GSAR/perl5.004_71/pod/perldelta.pod#foreach-1..1000000-optimized) (released on 9 July 1998) states that counting loops are optimized.

I might do some specific version testing at some point, but given this is apparently a 16 year old issue, I'm confident that the warning in `perlop` can be put to rest.