Thomas Bätzler wrote:
> freefox <[email protected]> asked:
>> As join is a build in I guess it should/could be called without
>> parenthesis.
>> However these two lines gives me two different answers.
>>
>> my @strings = ('foo', 'bar');
>>
>> my $string1 = q{('} . join q{', '}, @strings . q{')};
>> my $string2 = q{('} . join(q{', '}, @strings) . q{')};
>>
>> print "$string1\n";
>> print "$string2\n";
>>
>> Output:
>> ('2')
>> ('foo', 'bar')
>>
>> Anyone who knows/understands why?
>
> See http://perl.plover.com/context.html for a complete explanation.
The comma operator binds very loosely, so in your first version the expression
@strings . q{')
is evaluated before join() is called. The concatenation operator puts its
parameters in scalar context, so @strings evaluates to 2.
The second version does what you intend because the parentheses force the comma
operator to be evaluated first.
HTH,
Rob
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/