On Wed, Jan 10, 2007 at 01:14:43PM -0500, Uri Guttman wrote:
> >>>>> "DB" == Dan Boger <[EMAIL PROTECTED]> writes:
> 
>   DB>   @headers = ("header 1","header 2","header 3");
>   DB>   @body = ("body A","body B","body C");
>   DB>   $h = 4;
>   DB>   foreach (@headers,"",@body) {
> 
> the list of aliases is created there. it can't be changed by clearing
> @body. the aliases refer to the elements passed to foreach and not
> where they came from. it is like keeping a ref to a lexical var after
> it leaves scope. the var is still around due to ref counting and you
> can access it via the ref but the original access is lost. 

Yup, that's what I thought.

>   DB>         print "$h: $_\n";
>   DB>             @body = ();
> 
> here you lose the original access but the foreach alias is still around.
> 
>   DB>                 push @headers,"header ".$h++;
> 
> that will be called 7 times so you see that in @headers.

Great.  But according to what you say here (and what I thought would
happen), the output would be:

  4: header 1
  5: header 2
  6: header 3
  7:
  8: body 1
  9: body 2
  10: body 3

>   DB> What I don't get is how the next 3 elements of @headers get in there?
>   DB> Is this part of the "don't mess with the loop vars while looping"
>   DB> clause?
> 
> well, that rule is still good but doesn't apply here. it is just ref
> counting at work here and nothing wacky. but why would you (or your
> friend) run code like the above anyway? i like to keep my loop
> controls very clean so i don't have to think about odd cases like
> this. hertz my branes two mutch.

This code was just written to see how things work.  I find that
understanding these kind of edge cases helps overall.

-- 
Dan Boger
[EMAIL PROTECTED]
 
_______________________________________________
Boston-pm mailing list
Boston-pm@mail.pm.org
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to