Please take our Survey
logo       

Choosing A Webhost:
A web hosting service is a type of Internet hosting service that allows individuals and organizations to provide their own website accessible via the World Wide Web. Web hosts are companies that provide space on a server they own for use by their clients as well as providing Internet connectivity, typically in a data center. Web hosts can also provide data center space and connectivity to the Internet for servers they do not own to be located in their data center, called colocation. more...

Re: [Boston.pm] Odd behaviour in foreach: msg#00016

lang.perl.perl-mongers.boston

Subject: Re: [Boston.pm] Odd behaviour in foreach

>>>>> "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>
Google Custom Search

Recently Viewed:
qnx.openqnx.dev...    gcc.libstdc++.c...    solaris.opensol...    information-ret...    misc.misterhous...    web.catalyst.ge...    apache.webservi...    redhat.release....    hardware.lirc/2...    kernel.autofs/2...    technology.sust...    linux.vdr/2003-...    editors.lyx.gen...    org.user-groups...    netbsd.devel.pk...    xdg.devel/2004-...    version-control...    jakarta.slide.d...    debian.packages...    creativecommons...    ports.ppc.embed...    bug-tracking.bu...   
Home | blog view | USPTO Patent Archive | advertise | OSDir is an inevitable website. super tiny logo

Free Magazines

Cisco News
Receive 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