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

Hi monks,

Along with programing in perl deeply, I found a strange comma in many occasions, especially in config context. for example

use ExtUtils::MakeMaker; $Verbose = 1; WriteMakefile( NAME => 'Mytest2::mylib', SKIP => [qw(all static static_lib dynamic dynamic +_lib)], clean => {'FILES' => 'libmylib$(LIB_EXT)'}, );
# Configure the application __PACKAGE__->config( name => 'MyApp', setup_components => { except => qr/SCCS/ }, );

I'm just curious about the last comma. As usual sense, the last item is no need to add a comma.

Hope monks could enlighten me! TIA





I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

Replies are listed 'Best First'.
Re: strange comma
by stevieb (Canon) on Jun 29, 2010 at 02:52 UTC

    This is a very common, and imho, proper practice.

    The extra comma (ie. separator) signifies that something may be coming next. If nothing comes next, perl ignores it.

    However, with decent layout techniques, it is easy to identify where the original coder meant to allow for future manual or automated growth by including the separator within the code. I *always* include the extra separator: eg

    my $href = { a => 'apple', b => 'bannana', c => 'coconut', };

    I find this especially useful when designing an API that can grow at any time:

    func({ this => 'that', these => 'those', ours => 'theirs', when => 'now', });

    Having the habit of always writing the trailing comma (separator) means that I'll *never* have to troubleshoot an issue where one isn't present.

    stevieb

Re: strange comma
by tcurtis (Acolyte) on Jun 29, 2010 at 02:43 UTC
    It doesn't do anything. It's included simply to make adding new items to the list or hash more convenient. Without the extra comma, adding something to the list requires going back to the "clean => ...." line and adding a comma, and then adding the new thing on the next line. It's easy to forget to add the comma, so people often use extra commas after the last item so they won't have to worry about it if they think they'll need to add or remove options from the list often.

      Without the extra comma, adding something to the list requires going back to the "clean => ...." line and adding a comma, and then adding the new thing on the next line.

      In addition to that, it also means that the diff between revisions in your VCS (which you of course use, whichever one you use) ends up having that extra -/+ pair of lines just to add the comma, which makes it that much slower to scan visually.

        ends up having that extra -/+ pair of lines just to add the comma

        That leads one of my two reasons for using it: It's easier to add a comma at the end of the line you are typing than to have to modify two lines (instead of one) when adding a new item to the list.

        (The second reason is that I think it looks better with the comma.)

      perl-mode in emacs doesn't do indentation properly if that comma is missing ...
Re: strange comma
by Your Mother (Archbishop) on Jun 29, 2010 at 06:37 UTC

    Side note: it seems strange or maybe even stupid from the perspective of other languages but I miss it a lot when doing SQL or JavaScript (I know it's allowed in some JS engines but it's not portable) et al. It's very convenient and I echo stevieb's sentiment; while not necessary, I think it's a best practice.

Re: strange comma
by ikegami (Patriarch) on Jun 29, 2010 at 05:51 UTC

    It's optional, just like the last semi-colon of a block.

    for (1..3) { print "$_\n" } print "done\n"

    It's a question of preference.

Re: strange comma
by CaMelRyder (Pilgrim) on Jun 29, 2010 at 14:05 UTC
    I personally prefer to shy away from the extra comma. I do this expressly because the majority of the perl programming that I do is in a web environment. As 'Your Mother' has pointed out, some javascript engines allow the extra comma and others do not. If you have in an environment where you switch between javascript and perl freely, this kinda of thing can be a major headache. If you get in the habit of leaving the extra comma, you are more likely to do so in the javascript snippets. This is all well and good under mozilla (which you are probably developing with if your a LAMPer), but internet explorer will barf everywhere over the extra comma. I once spent well over an hour on this "syntax error" that basically equated to a difference in the grammars.
    ¥peace from CaMelRyder¥

      I definitely understand. This is why I have taken to this style for JS (and SQL)-

      $.ajax({ url: "test.html" ,context: document.body ,success: function(){ $(this).addClass("done"); } });

      It's a bit weird at first but it makes yanks and pastes and edits easier and harder to mess up.