Clint Olsen <> wrote:
> LOOP: for ($scalar) {
>
> /whitespace/ and next LOOP;
> /pattern/ and this;
> /patern1/ and that;
> }
>
> What exactly is the for() loop doing that's different than $_ = $scalar?
1. Giving you a loop to 'next' to,
2. localising $_,
3. aliasing $_ rather than simply assigning it: ie. modifying $_ in
the loop will modify $scalar outside it.
The loop could be rewritten
LOOP: {
local *_ = \$scalar;
...
}
but that's a little *too* incomprehensible, even for me

.
This is quite a common idiom in Perl: it's sort-of like the 'with'
keyword in VB et al.
> The documentation is pretty scant on this subject - conditions under which
> pos() will be reset.
From perlop:
| A failed match normally resets the search position to the beginning
| of the string, but you can avoid that by adding the "/c" modifier
| (e.g. "m//gc"). Modifying the target string also resets the search
| position.
> Generally a for loop consists of an initialization, a test, and an
> optional chunk to do at the end of the code BLOCK.
That is a C-style for() loop, which is not much used in Perl and is
actually a while() loop in disguise

. The for() loop above is the
*other* type of for() loop, which some people call a 'foreach' loop to
disambiguate.
Ben
--
I've seen things you people wouldn't believe: attack ships on fire off the
shoulder of Orion; I've watched C-beams glitter in the darkness near the
Tannhauser Gate. All these moments will be lost, in time, like tears in rain.
Time to die. |-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-|