As already mentioned, this precedence problem is that the repetition operator x has higher priority than the concatenation operator . . However, this is not all that happens here, and the problem itself arises from a poor solution.
First of all, when you say
print (($foo . "\n") x $count);
What you do is change the context of the repeat statement for the list context.
(LIST) x $count
The above statement really means this (if $count == 3 ):
print ( $foo . "\n", $foo . "\n", $foo . "\n" );
From perldoc perlop :
The binary "x" is the repeat operator. In a scalar context, or if the left operand is not enclosed in parentheses, it returns a string consisting of the left operand repeating the number of times indicated by the right operand. In the context of a list , if the left operand is enclosed in parentheses or is a list formed by qw / STRING / , it repeats the list . If the right operand is zero or negative, it returns an empty string or empty list, depending on the context.
The solution works as intended because print accepts list arguments. However, if you have something else that takes scalar arguments, such as a subroutine:
foo(("text" . "\n") x 3); sub foo { # @_ is now the list ("text\n", "text\n", "text\n"); my ($string) = @_; # error enters here # $string is now "text\n" }
This is a subtle difference that may not always give the desired result.
The best solution for this particular case is to not use the concatenation operator at all, since it is redundant:
print "$foo\n" x $count;
Or even use more mundane methods:
for (0 .. $count) { print "$foo\n"; }
or
use feature 'say' ... say $foo for 0 .. $count;