Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: String concatenation

by petdance (Parson)
on Apr 11, 2012 at 21:04 UTC ( #964627=note: print w/ replies, xml ) Need Help??


in reply to String concatenation

Use whichever most clearly shows the intent of your code. I can see cases for concatenation and interpolation and join().

For example, I prefer the first one in this example. I think it feels more like reading prose.

print "Action status: $status"; # Preferred by me print 'Action status: ' . $status;

But say you're building a string to show the time. I think the first highlights the elements that are being used to build $date.

$date = join( '/', $day, $month, $year ); # Preferred by me $date = "$day/$month/$year";

And sometimes it doesn't make sense to interpolate or join and you just concatenate.

$response = $head . $html_opening . $body . $html_closing; # preferred $response = join( '', $head, $html_opening, $body, $html_closing );
Bottom line: Go for what is most expressive, and makes the code clearest to the reader. Assume that any microseconds you shave will be insignificant.

xoxo,
Andy


Comment on Re: String concatenation
Select or Download Code
Re^2: String concatenation
by Anonymous Monk on Apr 11, 2012 at 21:50 UTC
    Thanks to all, Andy I agree entirely. I've been slapped on the hand for not being consistent but since speed has little difference between the various methods readability is the key. Being consistently readable is the ideal. Choosing one method and sticking with it is nonsensically dogmatic. I just needed a sounding board, sorry if it seemed trivial. I can now fight my corner with a little more confidence. Thanks Monks.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2014-09-17 23:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (101 votes), past polls