in reply to for loops and 'and'

Ooh, that's weird. The problem is clearly with precedence and context.

$ perl -e'print for (1..10 or a..z);' abcdefghijklmnopqrstuvwxyz$
I have no idea why that doesn't mean print for 1..10;. I can make a case for that meaning print for 10;, but it is crazy that 1..10 evaluates false on the left of low precedence or.

I'm eagerly anticipating an explanation.

Update: This prints nothing,

$ perl -e'print scalar(1..10)' $
This must be hooked up with the odd special case mentioned by davido that $. is hooked up with flipflops when thay have constant arguments. Dwimmerie gone berserk.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: Re: for loops and 'and'
by ysth (Canon) on Nov 30, 2003 at 08:18 UTC
    This must be hooked up with the odd special case mentioned by davido that $. is hooked up with flipflops when thay have constant arguments. Dwimmerie gone berserk.
    Not gone berserk, just an awkism. Convenient for processing on ranges of lines, e.g. (1..2)&&/foo/||(3..eof)&&/bar/. What sometimes surprises is that it applies to any constant, even nonnumeric. For instance:
    perl -we'$. = "a"; "b"..die Useless use of range (or flop) in void context at -e line 1. Argument "b" isn't numeric in range (or flip) at -e line 1. Argument "a" isn't numeric in range (or flip) at -e line 1. Died at -e line 1.
    Here it happily compares int("a") to int("b"), giving warnings on both, and then (since both are 0, hence equal) dies. (Though older perls had a bug where it assumed not-equal if no reading of input had ever been done and $. was manually set.)
Re: Re: for loops and 'and'
by Anonymous Monk on Nov 30, 2003 at 07:19 UTC
    I too noticed that, before I posted the question. What's even wierder:
    print "With Parens[", scalar 1..10, "]"; print "\n"; print "Without Parens[",scalar(1..10), "]";
    Perl's range operator has wierd behavior, methinks... but why? Is there anything to fix that in my first case? I'm assuming it'd be a pretty large optimization over ternary operators...

    The problem also existed with arrays that represented ranges, which was even wierder...  do_something $_ for ( @_[1..$#_] || 0..$#{$_[0]}) is the code that I was initially working with to no avail... I guess one could say the problem also is that in the case of for loops, @_[1..$#_] is being used in two contexts at the same time, and perl can't deal with that... The code was basically saying- if it it's there (in scalar context), use it in list context, and if it isn't there, use a diff. list.

    Edit: BazB, added extra code tag.

Re: Re: for loops and 'and'
by Roger (Parson) on Dec 01, 2003 at 00:11 UTC
    I have constructed a more elaborate test -
    use strict; use warnings; use Data::Dumper; # test 1 print for (1..10 or 'a'..'k'); # line 6 print "\n"; # test 2 my @range = 1..10 or 'a'..'k'; # line 10 print for (@range); print "\n"; # test 3 my @r1 = 1..10; my @r2 = 'a'..'k'; @range = @r1 or @r2; # line 17 print for (@range); print "\n";
    And when I ran it, I got the following warnings and results -
    Useless use of range (or flop) in void context at p92.pl line 10. Useless use of private array in void context at p92.pl line 17. Use of uninitialized value in range (or flip) at p92.pl line 6. abcdefghijk 12345678910 12345678910
    The warnings and results might give a clue to what is happenning with list/range and the for and or operators?

Re: Re: for loops and 'and'
by jweed (Chaplain) on Nov 30, 2003 at 06:51 UTC
    I am wrong. See below. A key question for me is what does or give scalar context to when it is evaluated in list context. I reproduce the results of a test I ran. The results? Mindboggling.


    Who is Kayser Söze?