in reply to Re^3: Concatenate printing a string and array
in thread Concatenate printing a string and array

Thanks to everyone that responded. I like getting the varied responses that come about through the different experiences we've all had with Perl. I often don't understand what I'm reading, but I did find JohnGG's response to be something I was able to work with, despite the fact that I don't understand it much. I think it has something to do with specifying that the normal string separator $" (which normally has a value of a whitespace) to have a value of null. So even though the separator is still in effect, we're controlling it output by modifying its attributes. Is this correct?
Here is my working code:

print do { local $" = q{}; "Your result is:" . qq{@binary}; }

Cheers,
Scott

Replies are listed 'Best First'.
Re^5: Concatenate printing a string and array
by johngg (Canon) on Jan 05, 2009 at 23:52 UTC

    You have the essence of it but it ($") is called the "list separator" (see $LIST_SEPARATOR or $" in perlvar). It determines what character will separate elements of a list or array that has been interpolated into a double quoted string (or double quote-like string, see Quote and Quote like Operators in perlop). Also, I think that the word "null" is not correct as I take that to mean local $" = qq{\0}; ... i.e. the NUL character. As NUL is non-printing the effect would be the same but the code is actually setting $" to a zero-length, empty string which is not the same thing.

    You should note that the quoting constructs I habitually use, q{...} and qq{...}, are really the same as single and double quotes respectively. They are not commonly used but they make life much easier when doing Perl one-line commands at the shell or command prompt, which is why I have got into that habit. As a general guide, only use double quotes when you want to interpolate a variable into the string; use single quotes if no interpolation is needed.

    As to your working code, I think it will be easier to read and more maintainable if you employ some form of code indentation. This becomes more and more important as projects grow and require multiple developers/maintainers. Have a look around the Monstery at what some of the other Monks do, choose (and adapt if necessary) a scheme that suits you then use it consistently.

    In the last expression in the do BLOCK you have concatenated two strings to form the one you want to print. The concatenation is unnecessary since your @binary array could be interpolated in the first string. Using quote marks rather than my usual quoting constructs, your code becomes.

    print do { local $" = ''; "Your result is:@binary"; };

    I hope these points help to clarify things for you but please ask further if there's something you don't understand.

    Cheers,

    JohnGG