samtregar has asked for the wisdom of the Perl Monks concerning the following question:

I'm doing a little technical reviewing on a Perl book project and I was sure I'd found an error on this line:

  my @array = (1...5);

However, the code worked fine, just as if two dots had been used instead of three! B::Deparse is no help here:

$ perl -MO=Deparse -e 'my @array = (1...5);' my(@array) = (1, 2, 3, 4, 5); -e syntax OK

What's going on here? Shouldn't '...' be a syntax error?

UPDATE: Ok, now that I know what '...' is, can someone explain why it would be used instead of '..'? I don't find the text in perlop very illuminating.

-sam

Replies are listed 'Best First'.
Re: Perl treats '...' the same as '..'. Why?
by Fletch (Bishop) on Apr 19, 2004 at 00:59 UTC

    You would use ... in the case where you could have the begin and end condition on the same line, but don't want that to be considered as having met the end condition.

    my $save = tell( DATA ); while( <DATA> ) { print if /a/ .. /z/; } print "-" x 5, "\n"; seek( DATA, $save, 0 ); while( <DATA> ) { print if /a/ ... /z/; } exit 0 __DATA__ a b z c d w q z
Re: Perl treats '...' the same as '..'. Why?
by Old_Gray_Bear (Bishop) on Apr 18, 2004 at 23:34 UTC
    '..' -- the range operator
    '...' -- the range operator, but: "The operator ... doesn't return true if both its tests are true on the same line, .. does. (Perl Coookbook -- 6.8 Extracting a Range of Lines)

    ----
    I Go Back to Sleep, Now.

    OGB

Re: Perl treats '...' the same as '..'. Why?
by adrianh (Chancellor) on Apr 18, 2004 at 23:28 UTC
    What's going on here? Shouldn't '...' be a syntax error?

    Nope. See perlop:

    If you don't want it to test the right operand till the next evaluation, as in sed, just use three dots ("...") instead of two. In all other regards, "..." behaves just like ".." does.
Re: Perl treats '...' the same as '..'. Why?
by bart (Canon) on Apr 19, 2004 at 00:11 UTC
    In scalar context, perl treats the .. and ... operators as only slightly different from one another, just like the other repondants said. Thus:
    if(a() ... b()) { # etc
    and
    if(a() .. b()) { # etc
    are almost, but not exactly the same. That's the reason why both exist.

    In list context, that is, the way you were using it, they appear to act exactly the same. I can't, in the same perspective, really imagine

    print 3 .. 3;
    to behave differently from
    print 3 ... 3;
    Can you?
      Well, it does seem a little odd to me to have
      echo -e "1\n2\n3\n4" | perl -lne 'print if 2..2'
      return the same as
      perl -le 'print 2..2'
      while
      echo -e "1\n2\n3\n4" | perl -lne 'print if 2...2'
      returns something very different from
      perl -le 'print 2...2'
      I understand why it does, but it would seem preferable to either disallow ... in list context, or make it behave "the same" in list and scalar contexts. (So the list 2...2 would be 2,3,...,INTMAX, and the list a()...b() would be the same as a()..b() unless a()==b(), in which case it would be a(),...,INTMAX.)

      Although, come to think of it, I think what would really make sense is if someone wrote a source filter than numbers all of the operators, with .. in slot 2 and ... in slot 3, so that you can use a sequence of periods of the appropriate length to choose any operator in Perl. Perl likes gathering up different programming paradigms, right? (Functional, OO, logical, ...) So this would make it easier to implement the genetic algorithm style, where you start off with something simple like

      $x = $x ....... $y ...... $z ...... $x; $y = $x ...... $y ........... $z; $z = $x .... $y ........ $z;
      and then you can easily mutate and interbreed code merely by changing the number of periods. Repeat until you get the answer desired. Some of my debugging sessions already closely resemble this.
Re: Perl treats '...' the same as '..'. Why?
by TimToady (Parson) on Apr 19, 2004 at 17:53 UTC
    The "..." is in Perl 5 only to make the sed-to-perl translator more accurate. It's not expected the most people would use it. Note also that either kind of scalar range operator is going away in Perl 6 in favor of a flipflop() function or some such. "$a..$b" will always produce a range list, and "$a..." will mean the same as "$a..Inf" (meaning you shouldn't use it where it will get flattened, unless you have a lot of memory...)

      My Turing Machine ran out of infinate paper tape.

A reply falls below the community's threshold of quality. You may see it by logging in.
A reply falls below the community's threshold of quality. You may see it by logging in.