develooper Front page | perl.perl5.porters | Postings from November 2000

for (max..min) optimized to WHAT?

Thread Next
From:
Jeff Pinyan
Date:
November 27, 2000 06:21
Subject:
for (max..min) optimized to WHAT?
Message ID:
Pine.GSO.4.21.0011270915080.2522-100000@crusoe.crusoe.net
It's evident that a range for loop is optimized to something more like a
C-style loop:

  for (1 .. 1000) {
    # ...
  }

becomes something LIKE (not equivalent to)

  for (local $_ = 1; $_ <= 1000; $_++) {
    # ...
  }

which is the reason why you can modify $_ in the loop

  for (1..10) {
    $_ = 2**$_;
    print;
  }

but you CAN'T do it here

  for (0, 1..10) {
    $_ = 2**$_;
    print;
  }

However, it is evident that the optimization is NOT explicitly a for loop,
because if it was:

  for (1..5) { $_-- }

would become

  for (local $_ = 1; $_ <= 5; $_++) { $_-- }

and thus it would never finish (but it does).  That leads me to believe
there's some MORE work afoot, like:

  for (local $_ = 1; $_ <= 5; $_++) {
    local $_ = $_;
    # ...
  }

Could someone explain to me the true translation, or point me to the
proper magick in the source?

-- 
Jeff "japhy" Pinyan     japhy@pobox.com    http://www.pobox.com/~japhy/
CPAN - #1 Perl Resource  (my id:  PINYAN)       http://search.cpan.org/
PerlMonks - An Online Perl Community          http://www.perlmonks.com/
The Perl Archive - Articles, Forums, etc.   http://www.perlarchive.com/


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About