in reply to Concatenate printing a string and array

The concatenation "." (dot) operator provides a scalar context to its arguments. The value of an array in scalar context is the length of the array. As your array has 4 elements, its return value in scalar context is 4.

Double quotes maintain the array context, so you could try the following:

{ local $" = undef; print "Your result is: @array"; }
Note the accolades to restrict the change to the $" (list separator variable) to the smallest scope possible.

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Replies are listed 'Best First'.
Re^2: Concatenate printing a string and array
by AnomalousMonk (Archbishop) on Jan 05, 2009 at 09:53 UTC
    local $" = undef;
    This produces warnings.
    >perl -wMstrict -le "my @ra = (1,2,3,4,5); { $\" = undef; print qq{@ra}; } " Use of uninitialized value in join or string at -e line 1. 12345
    Try something like assigning $" the empty string instead of undef:
    >perl -wMstrict -le "my @ra = (1,2,3,4,5); { $\" = ''; print qq{@ra}; } " 12345
    Or better yet, just use join('', @ra) as suggested by others.
      Try something like assigning $" the empty string instead of undef:
      >perl -wMstrict -le "my @ra = (1,2,3,4,5); { $\" = ''; print qq{@ra}; } "

      You are correct about the use of undef but the local that CountZero used is necessary otherwise the change is global in scope.

      $ perl -Mstrict -wle ' > my @arr = ( 1, 2, 3 ); > print qq{@arr}; > { $" = q{}; print qq{@arr} } > print qq{@arr};' 1 2 3 123 123 $

      I usually limit the scope to the print itself by using a do BLOCK.

      $ perl -Mstrict -wle ' my @arr = ( 1, 2, 3 ); > print do{ local $" = q{}; qq{@arr} };' 123 $

      I hope this is of interest.

      Cheers,

      JohnGG

        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
        Rats - forgot the local.

        All the more reason to use join().