in reply to Strange behaviour with Arrays and print?

For completeness:

my @a = qw/foo bar quz/; print "----- 1 -----\n"; print @a; # "foobarquz" print "\n----- 2 -----\n"; print scalar @a; # "3" print "\n----- 3 -----\n"; print @a , "\n"; # "foobarquz" print "----- 4 -----\n"; print @a . "\n"; # "3" print "----- 5 -----\n"; print "@a"; # "foo bar quz" print "\n----- 6 -----\n"; $" = "&"; print "@a\n"; # "foo&bar&quz" print "----- 7 -----\n"; $, = "%"; print @a , "\n"; # "foo%bar%quz%" print "----- -----\n";
  1. @a is in list context and provides the arguments to print, which simply outputs them one after the other.
  2. @a is forced to scalar context, and an array in scalar context returns the number of its elements.
  3. The same as 1. except there's an additional argument, "\n", compare this to the next case.
  4. The dot operator (.) concatenates two strings, and here @a is forced into scalar context the same way the scalar function does in 2.
  5. String interpolation of an array is like doing join($", @a) (see perldata).
  6. Same as 5. except we've fiddled with $" (the list separator) for demonstration purposes.
  7. Here's an interesting one not yet mentioned: $, (the output field separator) is what print outputs between its arguments. The extra % at the end is because that's output between the last element of @a and the \n.