Extra commas ignored?

Discussion in 'Perl Misc' started by Tim McDaniel, Dec 18, 2013.

  1. Tim McDaniel

    Tim McDaniel Guest

    So I figured that allowing one trailing comma was merely special-cased
    to allow for neat diffing, like in a revision-control system diffing
    %a = (
    FIELD1 => 23,
    FIELD2 => 45,
    versus new
    %a = (
    FIELD1 => 23,
    FIELD2 => 45,
    FIELD3 => 67,

    But then I accidentally did
    FIELD67 => , # FIXME
    and learned that the extra comma is ignored. In fact, it seems to
    ignore extra commas wherever:

    $ perl -e 'use strict; use warnings; use Data::Dumper; my @a = (15,,,,,,16,,,,); print Dumper(\@a), "\n"'
    $VAR1 = [

    The exception is that it doesn't allow a leading comma just after the

    Why in the dickens are extra commas permitted in most places?
    Tim McDaniel, Dec 18, 2013
    1. Advertisements

  2. Tim McDaniel

    $Bill Guest

    Comma Operator
    In list context, it's just the list argument separator, and inserts both its
    arguments into the list. These arguments are also evaluated from left to

    Basically you just have null arguments to the comma operator after the
    15 and 16.
    $Bill, Dec 19, 2013
    1. Advertisements

  3. Tim McDaniel

    Tim McDaniel Guest

    I saw that, but most of these commas are missing an argument on one
    side or both.
    I don't know of anywhere in Perl where there's a concept of "null
    argument", or even where "null" might occur at all (except for null
    string and NUL character). They certainly aren't undef arguments
    (which I might have expected) or the output would have shown them.
    Tim McDaniel, Dec 19, 2013
  4. Tim McDaniel

    Tim McDaniel Guest

    And null statement.

    I shouldn't have gone down that path. Anyway, I don't know of any
    "null arguments" anywhere.

    $a = $b + ;

    is a syntax error (hell, I'd better check that ... yup) as is

    $c = >> $d;

    And things that might look like something with an implied operand are
    actually a different operator, like

    $e = * $f; # Can't use an undefined value as a symbol reference at -e line 1.

    $g = - h; # Results in "-h"
    Tim McDaniel, Dec 19, 2013
  5. Tim McDaniel

    $Bill Guest

    Would missing argument give you a warmer fuzzier feeling ? :)

    It's a list with null/missing arguments in the list and therefore there
    is nothing to insert into the list for the null/missing arguments.

    or even where "null" might occur at all (except for null
    Null \Null\, a. [L. nullus not any, none; ne not + ullus any, a
    dim. of unus one; cf. F. nul. See {No}, and {One}, and cf.

    3. (Math.) Empty; having no members; as, the null set.

    They certainly aren't undef arguments
    $Bill, Dec 19, 2013
  6. Tim McDaniel

    $Bill Guest

    It obviously depends on the operator and the context. The only operator
    we're talking about is the comma operator and only in a list context
    (totally different results in a scalar context).
    $Bill, Dec 19, 2013
  7. Am 18.12.2013 22:53, schrieb Tim McDaniel:
    Beside the syntactic parsing reasons,
    it has the benefit of easy maintanance.

    Imagine you'd have something in addition like
    %b = (
    FIELD3 => 81,
    FIELD4 => 99,

    and for w/e reason you want to combine then in source code to

    %ab = (

    with the extra unnecessairy comma at the end,
    you just can copy and paste the data lines,
    without them, you'd have to insert a , at some place.
    Especially when working with configuration, that gets disturbing and
    when dynamic evaluation comes into play, also can lead to strange failures.

    Janek Schleicher, Dec 19, 2013
  8. The key is the "it's just the list argument separator" here which
    implies that a comma in list context is not 'the comma operator' (which
    takes two arguments, evaluates both left-to-right and returns the
    result of the right one[*]) but a separator. This means it is used while
    creating a token sequence during syntactical analysis: Once the lexer
    encounters a , the current token is finished and it starts accumulating
    a new one. Enter another comma --- no token yet, continue looking. And
    so on[**].

    [*] Technically, the scalar-context comma operator is also not an operator
    in the sense that + is, it's rather a compiler directive.

    [**] This description might seem inaccurate, eg it is possible to
    define a sub

    sub digits
    return 0,,1,2,3,4,5,6,7,8,9;

    which, when executed in list context, returns a list of digits but
    ends up returning only the last element of this list in scalar context.
    However, what happens here is just that all list elements are pushed on
    the stack from left to right. In scalar context, the element at the top
    of the stack kept while all others are removed.
    Rainer Weikusat, Dec 19, 2013
  9. Tim McDaniel

    Tim McDaniel Guest

    As you wrote, it also helps maintenance.

    In a similar way, Perl allows dropping the semicolon before "}", but I
    always put one there anyway (with limited exceptions: one-line or
    few-line code blocks for sort, grep, and map).

    I understand the concept of "null statement" and that it's
    occasionally useful, so I have a mental model of why
    perl -e '{print "a\n";;;;;;;;;;;;;;; print "b\n"}'
    works fine.

    What I'm asking is why Perl allows and ignores multiple internal
    commas in a list. What benefit could this possibly give? Does that
    benefit outweigh being silent on the occasional of leaving off an
    Tim McDaniel, Dec 19, 2013
  10. Actually, that's not quite correct.
    In Perl the semicolon is a statement seperator instead of a statement
    terminator as in some other programming languages.
    Therefore Perl allows you to create an emtpy statement, e.g. in front of
    a "}", by adding an additional semicolon if it pleases you.

    That's the same age-old question as if
    is a text with 3 lines or a text with 2 lines plus some garbage after
    the second line.

    Jürgen Exner, Dec 19, 2013
  11. This idea is unfortunately unsuitable for describing the behaviour of
    perl because commatose expression apparently generally work, cf

    perl -e 'print 5 if 0,,,,5'
    Rainer Weikusat, Dec 19, 2013
  12. Judging from the 5.10.1 perly.y, there's actually no such thing as 'a
    comma operator' which would need to be a 'binary operator between
    terms'. The lexer treats it as such but in the parser, it's only (slight

    /* Expressions are a list of terms joined by commas */
    argexpr : argexpr ','
    #ifdef MAD
    OP* op = newNULLLIST();
    $$ = append_elem(OP_LIST, $1, op);
    $$ = $1;
    | argexpr ',' term
    OP* term = $3;
    term = newUNOP(OP_NULL, 0, term);
    $$ = append_elem(OP_LIST, $1, term);
    | term %prec PREC_LOW

    ie, there is something like a list operator and the , is used as purely
    syntactical element for separating terms in a list.

    At least in certain cases, the list is expanded inline, with no 'list
    operator' anywhere in sight, eg

    [rw@sable]~#perl -MO=Concise,-exec,a -e 'sub a { return ($_[0], $_[3], $_[2]); }'
    1 <;> nextstate(main 1 -e:1) v
    2 <0> pushmark s
    3 <#> aelemfast[*_] s
    4 <#> aelemfast[*_] s/3
    5 <#> aelemfast[*_] s/2
    6 <@> return KP
    7 <1> leavesub[1 ref] K/REFC,1

    In constrast to this, an addition compiles to

    [rw@sable]~#perl -MO=Concise,-exec,a -e 'sub a { return ($_[0] + $_[3] + $_[2]); }'
    1 <;> nextstate(main 1 -e:1) v
    2 <0> pushmark s
    3 <#> aelemfast[*_] s
    4 <#> aelemfast[*_] s/3
    5 <2> add[t3] sK/2
    6 <#> aelemfast[*_] s/2
    7 <2> add[t5] sKP/2
    8 <@> return K
    9 <1> leavesub[1 ref] K/REFC,1

    And in any case,

    [rw@sable]~#perl -e 'LIST =~ /,|comma/i or print "something else\n"'
    something else

    It would be interesting to know if 'the comma operator' in C is actually
    treated as an operator or if the historical misnomer actually came from

    NB: I'm very much obliged to you for posting this because it has greatly
    helped my understanding of perl, even though this reply may not sound
    like this. I don't have an affirmtative mind ...
    Rainer Weikusat, Dec 20, 2013
  13. If it really wasn't different, you'd refer to 'the question mark
    operator' and 'the colon operator' in this case and this would be as
    inadequate because they're also just syntactical elements of something
    There is no such thing as 'a comma operator' in the Perl grammar (at
    least, I didn't find anything like that), commas are used as separators
    in a certain kind of compound expression. As Tim McDaniel observed,
    despite comma is documented as binary operator, it doesn't behave like
    one: It has no arity. The other hypothesis, "it is a binary operator but
    one which can have imaginary arguments" is to be rejected on the grounds
    that it introduces artifical complications in order to bend reality such
    that it fits with terminology (applying Occam's razor).
    The difference I see here is that there has to be some op (not operator)
    in this place and if no other was provided, a 'list' op is used. That
    'return' happens to be equivalent to that is just an accidental property of
    my example (The same phenomenon can be observed for reverse and grep and
    presumably, for any other list operator returning a list).

    The main difference is that , is really a binary operator in C, eg this
    Rainer Weikusat, Dec 22, 2013
  14. I think refering to the complete syntactical construct as 'list' or
    'list expression' makes more sense than talking about a 'comma operator'
    here, especially considering that ; isn't called 'the semicolon
    operator' despite the striking similarities between the two: A list is a
    comma-separated list of expression which are evaluated from left to
    right and ultimatively end up as arguments of a list operator. If an
    explicit list operator doesn't exist, an implicit "return the list in
    list context, the last element otherwise" list operator is being used
    (which implies that a list expression in scalar context _works_ like the
    C 'comma operator', except that the syntax is less restrictive).
    Rainer Weikusat, Dec 30, 2013
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.