On 3/3/09 Tue Mar 3, 2009 3:05 AM, "freefox"
<[email protected]> scribbled:
> Hi,
>
> 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?
Because of the precedence of the operators involved, which are '.' and ','.
The dot operator (.) has higher precedence than the comma operator (,).
Therefore, the expression " A, B . C" will be evaluated as if it were "A,
(B.C)".
The expression
join q{','}, @strings . q{')}
Is evaluated as if it were
( join q{','}), (@string . q{')})
The dot operator expects scalars on either side, forcing scalar context on
the evaluation of @strings, yielding the number of elements of @strings, or
'2'.
This is one of the reasons why I always use parentheses when writing
expressions.
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/