in reply to Bug with lists?


I would guess that the comma is being used as an operator. Deparse seems to agree:
perl -MO=Deparse -e '$x=[1, , ,2]' $x = [1, 2];
The Comma Operator, is documented in perlop.

Update: My guess was wrong. The comma isn't being used in a scalar context. As an example of that see:

perl -MO=Deparse -e '$x=(1, , ,2)' $x = ('???', 2);
merlyn seems to have the correct reason.

--
John.

Replies are listed 'Best First'.
Re: Re: Bug with lists?
by demerphq (Chancellor) on Feb 18, 2002 at 13:32 UTC
    Thanks a lot!

    I guess it makes sense, but I have to say that this hardly seems like DWIM behaviour...

    Oh well, can't win em all can you?

    UPDATE:

    Ive now had a look at the section on the comma operator, but frankly I can't tell which part of the docs suggest that this behaviour is correct.

    • Binary ``,'' is the comma operator. In scalar context it evaluates its left argument, throws that value away, then evaluates its right argument and returns that value. This is just like C's comma operator.

    • In list context, it's just the list argument separator, and inserts both its arguments into the list.

    • The => digraph is mostly just a synonym for the comma operator. It's useful for documenting arguments that come in pairs. As of release 5.001, it also forces any word to the left of it to be interpreted as a string.

    This seems to suggest, given that inside of an annon array the comma's are in list context, that it should insert both of its arguments (empty agreed, so it should be undef no?) into the list. So I am still confused about this behaviour, and unconvinced that its not a bug.

    I welcome further info.

    Yves / DeMerphq
    --
    When to use Prototypes?

      empty agreed, so it should be undef no?
      I think this is where you are taking a leap of faith, and leaping in the wrong direction. {grin}

      In fact, Perl hints the contrary in perldoc perldata:

      You may have an optional comma before the closing parenthesis of a list literal, so that you can say: @foo = ( 1, 2, 3, );
      So there, you're not getting 1,2,3,undef. Just 1,2,3. An extra embedded syntax-only comma was formerly illegal (I believe). I'm a bit surprised to see that it's now being nicely ignored, just as the trailing comma had been, but it's not totally inconsistent with the trailing-comma-ignored feature.

      Conclusion: no bug, although not precisely documented to work with embedded comma as well as trailing comma.

      -- Randal L. Schwartz, Perl hacker

        Thanks merlyn, this seems like a credible explanation. Although as I said in the CB it seems like a bad call (contrary to DWIM, as well as the behaviour of about every other language.)

        Nevertheless I would like to post this to perl5porters, if only to get the documentation improved. That would be the correct forum wouldn't it?

        Yves / DeMerphq
        --
        When to use Prototypes?