2
use Getopt::Long::Configure(pass_through);
# ....
GetOptions(
        "display=s"  => \$display, 
        "input=s",   => \$input,    # A strange comma right after "input=s", 
);

Can some one explain this Perl code above for me? The second option "input=s", has a strange comma. Does this comma have any special meaning here?

Thank you very much,

lxw
  • 91
  • 1
  • 6

1 Answers1

3

Nope, this comma is mainly misplaced and doesn't make sense at all.

However, it doesn't affect the code, since the parameters you're passing are passed in as a hash and a hash is basically nothing else than a list of key value pairs. The fat-comma (hash-rocket in other languages) => can also be treated like a simple comma - it's mainly there for denoting such a key-value pair.

You can also write it:

GetOptions(
    "display=s", \$display,
    "input=s", \$input,
)

With your additional comma it becomes:

GetOptions(
    "display=s", \$display,
    "input=s", , \$input,
)

which doesn't change the list at all since two or more commas and/or rockets are simply treated by perl as one comma.

@a = (1, 2, 3, , , 6 => 7, 6);
print join(",", @a), "\n";

1,2,3,6,7,6

So: it doesn't harm, but since it leads to confusion, I recommend to remove it.

Karsten S.
  • 2,349
  • 16
  • 32
  • 1
    I think you should very explicitly mention that 2 commas (or comma+rocket) are simply treated by perl as a single comma when building a list, as illustrated by your last code snippet. – DVK Jun 19 '13 at 21:53
  • yep, thanks - I missed to mention it. I edited the page accordingly – Karsten S. Jun 19 '13 at 21:58
  • Fun fact: It's not even a side-effect. The parser specifically ignored the duplicate comma. e.g. `1,2,(),3,4` returns the same thing as `1,2,3,4`, but they compile to different opcodes. On the other hand, `1,2,,3,4` and `1,2,3,4` compiled to the same opcodes. – ikegami Jun 19 '13 at 22:08