in reply to Adding items to arrays: best approach?

What's the best/preferred/Perl-ish method?

That will be subjective. However, I'd have to go with push for several reasons perhaps the strongest of which is that this is precisely what it is designed to do: append entries to arrays. If it were no better than the first approach it would not be in the language.

Has the first method advantages over the second method or vice versa? What are the advantages, if any?

I see some advantages to the second method:

  1. There is no need to specify the name of the array twice therefore the programmer is less likely to make a mistake such as extracting data from the wrong array in the RHS.
  2. The first method does a lot more stuff: extracting data from the array into a list, appending other values and then overwriting the array with the resulting list. This sounds much less efficient than a simple push which (as a guess) I would expect literally to append to the array in situ. For large arrays or many ops this might become an important consideration.
  3. push will also save you from mistakes like attempting to push to a scalar.

That said, I would definitely combine your 4 pushes into one and probably use a glob or map to avoid all the repetition.

What's the habbit in case of initial array declaration and first method: put the comma's at the back or at the front?

Not sure what you mean here, sorry.

Replies are listed 'Best First'.
Re^2: Adding items to arrays: best approach?
by geertvc (Sexton) on May 28, 2020 at 12:01 UTC
    Hello hippo,

    Not sure what you mean here, sorry.

    Pls. allow me to clarify:

    Chose between
    $inputPathPrefix . "/test4.xml",
    or
    , $inputPathPrefix . "/test4.xml"
    when adding items to an array (or something similar).

    But seen the majority of the people that answered (really big thank you to all!) I think it's not that relevant anymore, since most - if not all - are in favor of the push way of working.

    Nevertheless, it would be interesting to hear from wintered Perl programmers their preferred way, although I realize this might be a matter of personal taste and flavor and as such, be quite subjective...

    Best,
    --Geert
      Perl accepts commas before the closing parenthesis/bracket/brace without inserting a (dummy) item, therefore I prefer the comma "after", so that all entries are equal (neither the first nor the last differ).

      This is indeed a purely stylistic concern. Personally, when a line needs to be split around a binary operator I would split after the operator as I think this reinforces the point that the statement isn't finished once the end of the line has been reached. I do this in any language, not just Perl. It was probably taught to me decades ago as a minor technique in aiding clarity and has stayed with me ever since.

      With postfix control operators (if, unless, for etc.) it's the other way round as they are more tightly bound to the subsequent clauses (again, subjective). So I might write:

      print 'Very looooooooooooooooooooooooooooooooooooooooooooooooooooooooo +ooooong non-interpolated string costing $many ', "and another clause from prog $0\n"; # but die 'Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo +ooooooong exception message' if $OMG_ZOMBIES > $max_zombies_allowable;

        Interestingly, the GNU convention is "When you split an expression into multiple lines, split it before an operator, not after one." but GNU style apparently does not treat commas as operators — those are still left at the end of a line.

        if (($foo && $bar && ...) || ($baz && $quux && ...)) { ... } # but function_call_with_many_lengthy_args (very_long_arg1, very_long_arg2, and_on_and_on_and_on);

        GNU style is specifically for C, but Perl syntax is reasonably similar. I suppose the point is that this really is a matter of style, except in Awk, where if I remember correctly, an operator at the end of a line also implies line continuation; the GNU convention requires backslash-newline in Awk.