Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Bug with lists?

by demerphq (Chancellor)
on Feb 18, 2002 at 12:55 UTC ( [id://146141]=perlquestion: print w/replies, xml ) Need Help??

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

Somebody please tell me where this behaviour is documented, or tell me it is indeed the bug I think it is:
use Data::Dumper; my $x=[1, , ,2]; # This is NOT the same as $y !!!! my $y=[1,undef,undef,2]; print Dumper($x,$y); __END__ $VAR1 = [ 1, 2 ]; $VAR2 = [ 1, undef, undef, 2 ];
I could have sworn the output from both should be THE SAME!

Arrgh!

Yves / DeMerphq
--
When to use Prototypes?

Replies are listed 'Best First'.
Re: Bug with lists?
by jmcnamara (Monsignor) on Feb 18, 2002 at 13:19 UTC

    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.

      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

(tye)Re: Bug with lists?
by tye (Sage) on Feb 18, 2002 at 14:26 UTC

    Why should "empty" be undef??

    my @empty= (); my @list= ( 1, (), empty(), /(hi)/g, @list[@empty], 2 ); sub empty { return } print "(", join(",",@list), ")\n";
    The above code prints (1,2).

            - tye (but my friends call me "Tye")
      Fair point. But I still would argue that ,, being interpreted as ,(), should be documented.

      Yves / DeMerphq
      --
      When to use Prototypes?

        But I still would argue that ,, being interpreted as ,(), should be documented.
        Actually, those are not the same either.
        $a = (4, 3, ,); # sets $a to 3 $b = (4, 3, (),); # sets $b to undef
        Please stop treating a trailing comma or two consecutive commas as anything other than "this disappears like you never even wrote it". It's like making something out of how many newlines you put after a statement. It has no semantics. Get it?

        -- Randal L. Schwartz, Perl hacker

Re: Bug with lists?
by Anonymous Monk on Feb 18, 2002 at 17:40 UTC
    This is actually pretty well-documented in perldata:

    This interpolation combines with the facts that the opening and closing parentheses are optional (except necessary for precedence) and lists may end with an optional comma to mean that multiple commas within lists are legal syntax. The list 1,,3 is a concatenation of two lists, 1, and 3, the first of which ends with that optional comma. 1,,3 is (1,),(3) is 1,3 (And similarly for 1,,,3 is (1,),(,),3 is 1,3 and so on.) Not that we'd advise you to use this obfuscation.

    If you take that logical explanation into account I think you don't want to make perl complain over two commas directly following each-other.

    (However, I wonder if that documentation really is true. To me it seems like 1,,,3 "really" is ((1,),),3 is (1,),3 is 1,3. Futher, afaik the comma operator needs something to the left. In (,) it's got nothing.)

    -Anomo

      Under Perl 5.004_02 for Windows, I get a syntax error if I try perl -e "print join(qq/\n/, (1,),(,),3  )". However, perl -e "print join(qq/\n/, ((1,),), 3 )" works as Anomo predicted. An anonymonk trumps perldoc? I never thought I'd see the day!


      TGI says moo

      Hmm. I can't recall reading that. Thanks.

      While I suppose this puts it all to rest, I'd still like to see it documented under perlop. After all it seems to me that that would be the first port of call trying to track down this issue.

      Nevertheless, much thanks.

      BTW: Im guessing you are one of the perlbug recipients... Would I be correct?

      Yves / DeMerphq
      --
      When to use Prototypes?

Re: Bug with lists?
by jjohn (Beadle) on Feb 19, 2002 at 01:02 UTC
    Merlyn is unsurprisingly right in calling your usage a "syntactic" comma. I find that some folks find undef a bit too magical. In your second example, you use undef as a value. You also know that undef can be used as a function (eq. undef(@my_long_list);). You might also know that constants in Perl are implemented as functions that return the desired value (eq. sub pi(){ 3.14}; print 2 * PI;). When using the value undef, I find it helpful to think of that as a function call returning the value undef. With that in mind, I would have been surprised to learn that (1, , , 2) inserted undef. That seems a little too magical even for Perl, but do read perlfunc's description of split (/ / versus " ") for a mind-blower.
      As a mind-blower I can also recommend the documentation for eof which is highly magical. eof() v.s. eof probably generates a head-ache or two among the few that actually uses the eof function.

      -Anomo

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://146141]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2024-03-28 09:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found