Thomas Bätzler wrote:
> freefox <robert.lo...@privat.utfors.se> 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: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to