On Thu, Aug 6, 2009 at 12:10, Bryan R Harris<bryan_r_har...@raytheon.com> wrote:
>
>
> A question about the comma operator:
>
> (John and Chas deserve a rest from my questions, if they want it  =).
>
> The binary comma operator in scalar context supposedly "evaluates its left
> argument, throws that value away, then evaluates its right argument and
> returns that value."
>
> So this:
>
>  $_ = "dogs and cats";
>  $r = s/o/i/g, s/s/y/g;
>  print "$r: $_\n";
>
> ... prints "1: digy and caty".
>
> Why doesn't it print a "2" instead of a "1"?  It did 2 replaces of s to y...
> If I change the second line to read:

Because assignment binds more closely than comma.  Perl saw this

($r = s/o/i/g), s/s/y/g;

Try this instead:

$r = (s/o/i/g, s/s/y/g);

snip
> ... not evaluated as a binary comma operator?  Because it's in list context?
> Then why can't I do this with a list (without the parenthesis)?
>
> �...@a = 1, 2, 3;
snip

Because that is equivalent to

(@a = 1), 2, 3;

You might want to look at the [precedence chart][1]:

             left        terms and list operators (leftward)
             left        −>
             nonassoc    ++ −−
             right       **
             right       ! ~ \ and unary + and −
             left        =~ !~
             left        * / % x
             left        + − .
             left        << >>
             nonassoc    named unary operators
             nonassoc    < > <= >= lt gt le ge
             nonassoc    == != <=> eq ne cmp ~~
             left        &
             left        | ^
             left        &&
             left        || //
             nonassoc    ..  ...
             right       ?:
= is here => right       = += −= *= etc.
, is here => left        , =>
             nonassoc    list operators (rightward)
             right       not
             left        and
             left        or xor


[1] : http://perldoc.perl.org/perlop.html#Operator-Precedence-and-Associativity


-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
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