[comp.lang.perl] Recursive use of iterators

worley@compass.com (Dale Worley) (04/04/91)

I was writing a recursive function that contained a foreach statement,
so I made sure to localize the array it was iterating over:

	local(@dependencies) = ...;
	foreach (@dependencies) {...}

Somewhat to my surprise, the recursive execution of the loop
interfered with the proper operation of the outer execution.  I found
that I have to localize the symbol table entry (if that's how you say
it):

	local(*dependencies);
	@dependencies = ...;
	foreach (@dependencies) {...}

This is probably not a bug, but it led me to wonder how you deal with
recursive loops like:

	foreach $i ($a .. $b) {...}

Dale

Dale Worley		Compass, Inc.			worley@compass.com
--
And then there is the scientific problem known as "Schroedinger's pussy"...

lwall@jpl-devvax.jpl.nasa.gov (Larry Wall) (04/09/91)

In article <1991Apr3.233616.11680@uvaarpa.Virginia.EDU> worley@compass.com writes:
: I was writing a recursive function that contained a foreach statement,
: so I made sure to localize the array it was iterating over:
: 
: 	local(@dependencies) = ...;
: 	foreach (@dependencies) {...}
: 
: Somewhat to my surprise, the recursive execution of the loop
: interfered with the proper operation of the outer execution.  I found
: that I have to localize the symbol table entry (if that's how you say
: it):
: 
: 	local(*dependencies);
: 	@dependencies = ...;
: 	foreach (@dependencies) {...}
: 
: This is probably not a bug, but it led me to wonder how you deal with
: recursive loops like:
: 
: 	foreach $i ($a .. $b) {...}

That's handled by translating internally to

	local(@_GEN_1) = ($a .. $b);
	foreach $i (@_GEN_1) {...}

Only when you use a bare array do you get the direct references into the array.
That still doesn't say why your original didn't work though...

Larry