in reply to Fringe case slice behavior

Some of these weirdnesses will go away in Perl 6. It is still true that = will take its context from the left side, while += will enforce scalar context on both sides. What will change, however, is that any list-producing operator that returns its last value in scalar context in Perl 5 will instead produce a list reference in Perl 6.

That means there's no C-style comma operator in Perl 6. If you want the last value of a list, subscript it with [-1]. This also means that a slice in scalar context will not return the last value as it does in Perl 5. If you try that in Perl 6, you'll get a list reference, which will fail when you try to apply a scalar operator like += to it. (Perl 6 has a way to apply scalar operators to lists, but you have to be explicit.)

Replies are listed 'Best First'.
Re: Re: Fringe case slice behavior
by BrowserUk (Patriarch) on Jan 13, 2004 at 21:10 UTC

    I remember seeing some discussion about replacing the c-style comma operator with a keyword that achieved a similar effect. Various option where discussed -- also & then were muted I think.

    Was any conclusion reached as a) whether there would be such a keyword? b) What it is likely to be?


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    Timing (and a little luck) are everything!

      Was any conclusion reached as a) whether there would be such a keyword?
      Not by me. :-)
      b) What it is likely to be?
      I suspect a pop should be made to do the expected thing, in which case you have your keyword (or at least, method name). But as I said, a [-1] subscript will certainly work. It may well become the idiom of choice for when you really do want to select the last value.

      But note that many uses of the C-style comma operator are in fact in void context, in which case you don't much care whether it returns the last value or a reference to the list, since it's just going to throw it away anyway. So you can just use comma in those cases.

        Not by me. :-)

        Hence my question :)

        The particular case being [news://nntp.perl.org/perl.perl6.language/16066|discussed] (for example) was the replacement of

        ... warn "some error message", return unless condition;
        by
        ... warn "some error message" also return unless condition;

        Also discussed was

        for( my $i=1 also my j=3; cond; $i++ also $j+=3 ){ ...

        I'm more than happy to see the demise of the comma-operator-separated-list in a scalar context -v- flattened-array in a scalar context anomoly, and it's replacement by (1,2,3)[-1] for those rare occasions where that is required.


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail
        Timing (and a little luck) are everything!

      I cannot remember any discussion to replace the C-style comma in the 7 years that I've read p5p. I don't think such a proposal would make any chance, for several reasons: there is already a synonym for the comma: =>; it doesn't save anything, instead of a single keystroke, you'd have to use at least 4, but often 5. And it has the potential to break existing code.

      The comma is a well-known construct - especially amongst the people on p5p, as they tend to know C quite well.

      Abigail

        I didn't actually mention p5p.

        The discussion was on the p6lang list, and I was responding to "That means there's no C-style comma operator in Perl 6".


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail
        Timing (and a little luck) are everything!