|
|
Choosing A Webhost: |
Re: [Boston.pm] Odd behaviour in foreach: msg#00016lang.perl.perl-mongers.boston
>>>>> "DB" == Dan Boger <dan-rlx3YLNxYWXQT0dZR+AlfA@xxxxxxxxxxxxxxxx> 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 ------ uri-X/06uaNR9nFmbZtjAW+qKA@xxxxxxxxxxxxxxxx -------- http://www.stemsystems.com --Perl Consulting, Stem Development, Systems Architecture, Design and Coding- Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
|
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | Re: [Boston.pm] emma's pizza, Tolkin, Steve |
|---|---|
| Next by Date: | Re: [Boston.pm] Odd behaviour in foreach, Dan Boger |
| Previous by Thread: | [Boston.pm] Odd behaviour in foreach, Dan Boger |
| Next by Thread: | Re: [Boston.pm] Odd behaviour in foreach, Dan Boger |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
Free MagazinesCisco NewsReceive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business. subscribe Systems Management News, the newspaper for IT systems administration and data center managers! Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field. subscribe The Enterprise Newsweekly eWeek is the essential technology information source for builders of e-business. subscribe Oracle Magazine Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company. subscribe Total Telecom Total Telecom is "The Economist of the communications industry". subscribe |