in reply to Is it a list or a flip-flop?

I'm going to go out on a limb here and suggest that perl is possibly trying to optimize this range in scalar context at compile time. Hence, it's not working.

Compare to this - which does work:
#!/usr/bin/perl use strict; use warnings; my $n = 9; my @list = (1..$n); my $list = @list; print @list, "\n"; print $list, "\n";


Update: Limb broken - Paladin seems to have hit on it... as this will compile correctly as well:
#!/usr/bin/perl use strict; use warnings; $. = 1; my $n = 9; my $list = (1..$n);


Update2: From perlop:

If either operand of scalar ".." is a constant expression, that operand is considered true if it is equal (== ) to the current input line number (the $. variable).

Which means, this will also compile!
#!/usr/bin/perl use strict; use warnings; my $m = 1; my $n = 9; my $list = ($m..$n);
... even with $. undefined.

Replies are listed 'Best First'.
Re^2: Is it a list or a flip-flop?
by GrandFather (Saint) on Aug 18, 2005 at 19:18 UTC

    But what does $list contain? If I print it I get 1E0.


    Perl is Huffman encoded by design.
      From perlop:

      The value returned is either the empty string for false, or a sequence number (beginning with 1) for true. The sequence number is reset for each range encountered. The final sequence number in a range has the string "E0" appended to it, which doesn't affect its numeric value, but gives you something to search for if you want to exclude the endpoint. You can exclude the beginning point by waiting for the sequence number to be greater than 1.


      The range operator has always stumped me a bit, so I can't say much more than what it says above

      Update: I suppose it is working like this (in the case of setting $. to 1) - please correct me if I am wrong:

      Since 1 is a constant, and so is $., it is evaluated as TRUE.
      Since the LHS is true, the RHS is evaluated immediately.
      Since the RHS is NOT a constant, it is not compared to $., but is returned as true because it is not false (i.e. 9 is not 0, "" or undef).
      The range operation is completed.
      Because it is a single iteration only, the sequence number is 1, and is appended with "E0"