in reply to Perl style: Arguing against three-argument join()

How does your second snippet "obfuscate the order of parts in the final result"?

  • Comment on Re: Perl style: Arguing against three-argument join()

Replies are listed 'Best First'.
Re^2: Perl style: Arguing against three-argument join()
by Fletch (Bishop) on Jan 24, 2008 at 18:48 UTC

    Maybe if one doesn't know Perl well enough to simply use concatenation to join two items with a separator one is bound to get confuzzled about the order things come out of a join . . . :)

    Update: Just to clarify, that's a big smiley there. I don't think the join form is unqualified blasphemy dooming the author to an eternity of maintaining phpbb mods, but it's not the first choice I'd use unless it was relatively certain that the number of things needing to be concatenated was either arbitrary or varying (e.g. you need to conditionally intersperse $config{'infix'} every fourth Thursday in months not containing an "r"). qq{$config{'prefix'}$sep$config{'suffix'}} reads just fine to me, as does the OP's concatenation version.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re^2: Perl style: Arguing against three-argument join()
by martin (Friar) on Jan 24, 2008 at 19:03 UTC
    How does your second snippet "obfuscate the order of parts in the final result"?

    The three strings to be concatenated are given in a different order than from left to right, which might be counter-intuitive for a casual reader.

      That's silly. Someone who doesn't know what join does or the arguments it takes--or who doesn't know how to look it up in a reference or in the Perl documentation--has no business maintaining a serious Perl program. While there are definitely Perl operators with poor interfaces (caller is one of my least favorite), avoiding the use of join and slices because people who don't know Perl might not understand it is silly.

      (I realize you said "casual reader", which implies someone not reading carefully, but a similar argument applies there.)

      The three strings to be concatenated are given in a different order than from left to right, which might be counter-intuitive for a casual reader.

      Fat arrow to the rescue?

      join $separator => @things{'this', 'that', 'the other'};

      However, where the task at hand is better described as prepending and appending to a string, rather than sewing together a list of arbitrary length, I concur with some others in this topic that concatenation is clearer.

      -Mike

      As a casual reader, albeit one that has used, and is used, to the join function, I had to go over the OP twice to understand what is said in the first option (join '',a,b,c). I can't really relate to using a function in a way that is counter intuitive to someone who is familiar with the way it should be used, but I'd hazard that for anyone who never used it, or Perl, neither will make sense; and if that person is to perldoc it, the first example might only serve to confuse more.

      Software speaks in tongues of man; I debug, therefore I code.
      Stop saying 'script'. Stop saying 'line-noise'.