Brian McCauley said:
I think you really meant to ask why can't I say:
.... if m/str/ for @arr; [...]
All I can find are:
[1] Because you can't, so there!
[2] Because you could abuse it to produce unreadable code
[3] Because using for without an explicit loop variable is bad
[4] For well known reasons I won't repeat
You'll note that [1] and [4] are no sort of explaintion. [2] applies
to pretty much everyting in Perl. [3] is an argument against the
"for" statement modifier per-se, not an argument against being able to
chain statement modifiers.
Ok, so here are some more:
I'm not convinced that these are more.
1. It brings up the arguments that happened in p6l over
if/unless/for/foreach/while/until/map/grep/do
Isn't your [1] my [4]?
Do you mean _real_ parser problems as in "it would be difficult to
make the Perl parser cope"? People seem to claim these exist these
exist but I've never seen anyone present hard facts. Whenever they
are challenged to explain what they mean they end up falling back on
my [2,3].
3. postfix if is much less flexible than low-precedence logic anyway
(and, or)
This is true, but it is often more readable. Being able to choose a
more readable way to say something is good.
Number 2 breaks down like this:
If your grammar is generic, you want to be able to string:
print if /\d/ for @{$_} for @stuff;
Yes I do often want to do things like that because it is clear and
concise. At the moment I usually write the above as:
print for grep { /\d/ } map { @$_ } @stuff;
This is just as clear, almost as concise, but more memory hungy.
but then you lose the ability to access the $_ from the outer loop.
That is not a _real_ parser problem. Perl would have no problem
knowing which $_ was in scope at any point. If the code them
complicated enough the programmer could loose track, but them
In this case, that's not too bad, but there are plenty of cases
where you would want to.
So, like I said above, whenever people put forward your [2] usually
when they try to explain it fall back on some varient of my [2,3] as
you have done.
You'll also note the fact that chaining or nesting multiple map and
grep within a single expression there are also multiple different $_
and you can only ever get at the innermost one at any point. The
problem that you say would be introduced by allowing multiple for
postfix already exists. If this ever becomes an issue I usually stop
trying to do the whole lot in one statement.
So, you have to restrict for to a single instance,
No you don't.
and that seems non-intuitive to many users
So don't do it.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\