Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Is there a reason to use "join" instead of just using the period?

by qazwart (Scribe)
on Nov 18, 2010 at 19:03 UTC ( #872323=perlquestion: print w/ replies, xml ) Need Help??
qazwart has asked for the wisdom of the Perl Monks concerning the following question:

I am a new person at my job, and I started going through a lot of the Perl scripts. The previous employee is doing a lot of this:

$hotfix=join "", "$app", "_", "$mod", "_", "$bld", "_", "$hf", ".zip";
$fullpath=join "", "$Upload_Loc", "/", "$filename";

While I would probably do something like this

$hotfix = "${app}_${mod}_${bld}_${hf}.zip";
$fullpath = "$Upload_Loc/$filename";

Or maybe

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

Is there a reason why his way may be better than my way?

Comment on Is there a reason to use "join" instead of just using the period?
Re: Is there a reason to use "join" instead of just using the period?
by BrowserUk (Pope) on Nov 18, 2010 at 19:07 UTC

    Perhaps he didn't know about the ${varname} technique and had trouble with the underscores.

    Personally, I like your last one best. Though I'd use single quotes for constant bits.

    Mind you, if you remove the redundant double quote from his variables, that looks a lot less busy:

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

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Is there a reason to use "join" instead of just using the period?
by ikegami (Pope) on Nov 18, 2010 at 19:36 UTC

    I suspect it's an attempt to micro-optimise. join could in theory calculate the target string length before doing any concatenation. I don't know if it does or doesn't. If it does, that means it won't have to expand the string buffer size as it append its arguments.

Re: Is there a reason to use "join" instead of just using the period?
by wfsp (Abbot) on Nov 18, 2010 at 19:36 UTC
    I find interpolating/concatanating of any complexity mainly looks messy. Try to add dereferencing hash/array refs or method calls to the mix and it gets worse. I get on better with a template so I often press sprintf into service.
    $hotfix = sprintf( q{%s_%s_%s_%s.zip}, $app, $mod, $bld, $hf, );
    Expressions, ternaries, refs, method calls, etc. will fit nicely while still, imo, being readable.

    Although in the second (simple) case

    $fullpath = join(q{/}, $dir, $filename);
    looks better to me.

    ymmv

Re: Is there a reason to use "join" instead of just using the period?
by aquarium (Curate) on Nov 18, 2010 at 23:03 UTC
    i also like best your "or maybe" technique.
    i've seen the pattern your new person does in scripting, not just perl. the more typical case for using it is when there's a big bag of all sorts of variables and/or output from inline system calls. using this pattern you can fairly clearly see everything that gets joined together, and in the right order. in other words, a visual aid. but with all those quotes and commas, it loses some of that. i wouldn't frown upon it.
    the hardest line to type correctly is: stty erase ^H
Re: Is there a reason to use "join" instead of just using the period? (Benchmark results.)
by kcott (Abbot) on Nov 19, 2010 at 00:45 UTC

    Quite a few years ago (probably when Perl 5.6 was the current version) I read some documentation regarding this; unfortunately, I can't readily locate it now - I don't even remember if it was a manpage or a Perl book. Anyway, the gist of it was that join was a single operation whereas concatentation was a mult-stepped operation (i.e. $string = $x . $y . $z; was effectively $temp = $x . $y; $string = $temp . $z;) - the recommendation was to choose join over concatenation as a general rule-of-thumb.

    So, other than for very simple cases (such as $string = $x . $y; and $string .= $x;), I've pretty much kept to that rule.

    As we've moved through a number of Perl versions since then, and prompted by this thread, I decided to do some benchmarking. The results are mostly the reverse of the advice above. In general:

    • For relatively small numbers of strings that aren't exceptionally long (such as shown in qazwart's examples above), concatenation with the dot operator is typically faster.
    • For large numbers of strings, or when the strings are large, then join comes into its own.

    On this basis, I'm changing my general rule-of-thumb to use concatenation over join.

    Unless these operations are occurring in loops, recursive calls or similar situations where the net effect stacks noticeably, it probably doesn't matter that much which is used: spending hours trying to micro-optimise code to save a few nanoseconds just isn't worth it.

    The code and results are below (click on Read more ...).

    Just to head off the inevitable "Why did you ...?" question: the code contains an array (@counts) with one element which is used to iterate through the outer loop once. While testing, this contained a variety of values and I've left it that way in case others want to do the same.

    -- Ken

      On this basis, I'm changing my general rule-of-thumb to use concatenation over join.
      Go with the most readable alternative. The slowest of your examples happens 300,000 times per second. I very much doubt you do enough concatenatons in you progams for a 15% improvment of that to matter.
Re: Is there a reason to use "join" instead of just using the period?
by TomDLux (Vicar) on Nov 19, 2010 at 01:23 UTC

    My interpretation is that he knew he join command, but didn't know how to do interpolation. Maybe he was a C programmer who thought join would be faster than printf(). :-)

    Knowing several ways to do things, you can choose one that best expresses your intentions.

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Re: Is there a reason to use "join" instead of just using the period?
by talexb (Canon) on Nov 19, 2010 at 15:54 UTC

    Just compare the three ways of doing it (from your code):

      $hotfix=join "", "$app", "_", "$mod", "_", "$bld", "_", "$hf", ".zip";
      $hotfix = "${app}_${mod}_${bld}_${hf}.zip";
      $hotfix = join("_", $app, $mod, $bld, $hf) . ".zip";
    I prefer the last way, myself -- it's cleaner and the least noisy from a visual point of view.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://872323]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2014-11-29 00:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (200 votes), past polls