>>>>> "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. 


  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.

  DB>   }


  DB> And I say huh?  I follow until output line "7:".  I can even see why it
  DB> would run 3 more times (since the list fed to foreach was evaluated
  DB> before @body was modified).

  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.

uri

-- 
Uri Guttman  ------  [EMAIL PROTECTED]  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org
 
_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to