The documentation "The range operator (in list context) makes use of the magical auto-increment algorithm if the operands are strings." didn't quite shed any light on it, as I'm already familiar with the magical auto-increment feature. (But I think hbm got it below, which clarified something that the documenation could possibly use better wording for).
As to the second, however, you say the workaround is to use a "modifiable copy":
for ('a'..'f') { my $letter = $_; # Make modifiable copy ... }
But why isn't the my that I'm using here:
foreach my $letter ('a' ... 'c', 'd' ... 'f') { print " $letter"; $letter = uc $letter; }
doing exactly that??
Update: Actually, I see why. Of course, because it's assigning to a modifiable lvalue of the variable. Never mind.
But I'm still curious why this:
#!/usr/bin/perl -w use strict; use warnings; sub letters { foreach my $letter ('a' ... 'c') { print " $letter"; $letter = uc $letter; } print "\n"; } letters(); letters(); letters();
Doesn't exhibit the same behavior? Is it simply that it doesn't undergo the same optimization step you referred to?
In reply to Re^2: Two Range Operator Anomalies
by liverpole
in thread Two Range Operator Anomalies
by liverpole
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |