in reply to for loops and 'and'

for loops have absolutely nothing to do with your problem. Your problem is context. *Lots* of things in Perl behave differently in scalar versus list context. Arrays and array slices report their cardinality in scalar context (except in interpolative context, which is special); in list context they dump their whole contents. List construction operators (the comma list operator and the .. range operator) only *exist* in list context, because Perl doesn't see any point in constructing a list in scalar context. "Why should I construct a list here?", it says, "The very next thing I'd have to do is change it to a scalar somehow. It's utterly pointless to construct a list." So in scalar context these operators do not construct a list; they do Something Else Different.

as far as for (1..10 or 1..11) versus for (1..10 ? 1..10 : 1..11), the leftmost 1..10 evaluates the same way in both cases; the difference is that if it's true, the latter then evaluates 1..10 in list context; whereas, the former takes the true value and just uses it, which is probably not what you want. Actually, the whole thing is probably not what you want if you didn't specifically intend to use the .. operator in scalar context. Frankly, I'm not sure why you would want to do that; the scalar .. operator is sufficiently obscure that the Camel Book (as of the 2nd edition; I haven't read the 3rd yet) doesn't even *mention* it. It's not a terribly common operator.

Now, I'm guessing that what you're really attempting in your code is along the lines of for (@a or @b) or maybe for (@a ? @a : @b). The former doesn't do what you want, but you *think* the latter does. But that's an accident; as it happens, the latter isn't working the way you think it is either. The first @a is getting evaluated in boolean context (which is scalar), and so it is returning its *cardinality*. It is not dumping the whole list of its contents like you think. It just happens that a non-empty array or slice will always have a true cardinality (because of the nature of the way cardinality works) and so the second @a will get used when you think it will -- but not for the reason you were thinking, and that's why the simpler or construction doesn't do what you want.

If you want to be really efficient programming in Perl, if you want to *think* in Perl, you have got to get comfortable thinking in terms of context. Lists and list constructors are not by any means the only things that behave according to context. Many functions (including builtins, object methods in modules from CPAN, and your own functions if you write them that way), certain special variables, and various operators all do different things in different contexts. It's the richness of the context that makes Perl such a flexible and useful language, but if you don't think in terms of context it will confuse you sometimes.

When you think in terms of context, you won't make these sorts of mistakes. When I first saw your print for (1..10 or 1..11) I wasn't sure exactly what it would do (since I'm not very familiar with the scalar .. operator), but I for darn sure didn't think it would print "12345678910"; it would have surprised me a *great* deal if it had done that. My immediate thought was, "What on earth is he trying to do?" When I read the text of your post and determined that you thought it would print "12345678910", I knew you didn't understand context, because there's no such thing as a list in scalar context; it doesn't even make sense.


$;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/