Search code examples
perl

Concatenating strings in Perl with "join"


See Update Below

I am going through a whole bunch of Perl scripts that someone at my company wrote. He used join to concatenate strings. For example, he does this (taken hot out of a real Perl script):

$fullpath=join "", $Upload_Loc, "/", "$filename";

Instead of this:

$fullpath = "$Upload_Loc" . "/" . "$filename";

Or even just this:

$fullpath = "$Upload_Loc/$filename";

He's no longer here, but the people who are here tell me he concatenated strings in this way because it was somehow better. (They're not too clear why).

So, why would someone use join in this matter over using the . concatenate operator, or just typing the strings together as in the third example? Is there a valid reason for this style of coding?

I'm trying to clean up lot of the mess here, and my first thought would be to end this practice. It makes the code harder to read, and I'm sure doing a join is not a very efficient way to concatenate strings. However, although I've been writing scripts in Perl since version 3.x, I don't consider myself a guru because I've never had a chance to hang around with people who were better than Perl than I am and could teach me Perl's deep inner secrets. I just want to make sure that my instinct is correct here before I make a fool of myself.

I've got better ways of doing that around here.


Update

People are getting confused. He isn't just for concatenating paths. Here's another example:

$hotfix=join "", "$app", "_", "$mod", "_", "$bld", "_", "$hf", ".zip";

Where as I would do something like this:

$hotfix = $app . "_"  $mod . "_" . $bld . "_" . "$hf.zip";

Or, more likely

$hotfix = "${app}_${mod}_${bld}_${hf}.zip";

Or maybe in this case, I might actually use join because the underscore causes problems:

$hotfix = join("_", $app, $mod, $bld, $hf) . ".zip";

My question is still: Is he doing something that real Perl hackers know, and a newbie like me who's been doing this for only 15 years don't know about? Do people look at me concatenating strings using . or just putting them in quotes and say "Ha! What a noob! I bet he owns a Macintosh too!"

Or, does the previous guy just has a unique style of programming much like my son's unique style of driving includes running head on into trees?


Solution

  • I've done my fair share of commercial Perl development for "a well known online retailer", and I've never seen join used like that. Your third example would be my preferred alternative, as it's simple, clean and readable.

    Like others here, I don't see any genuine value in using join as a performance enhancer. It might well perform marginally better than string interpolation but I can't imagine a real-world situation where the optimisation could be justified yet the code still written in a scripting language.

    As this question demonstrates, esoteric programming idioms (in any language) just lead to a lot of misunderstanding. If you're lucky, the misunderstanding is benign. The developers I enjoy working alongside are the ones who code for readability and consistency and leave the Perl Golf for the weekends. :)

    In short: yes, I think his unique style is akin to your son's unique style of driving. :)