in reply to ... for (@_) x= 2;

I had problems to understand what your goal is, this code already looks broken.

It's true that x has a double nature (repeat string vs repeat list) like .. depending on context (flip flop vs range)

But "combined assignments" like += only allow scalars on the LHS...

See perlop

> These combined assignment operators can only operate on scalars, whereas the ordinary assignment operator can assign to arrays, hashes, lists and even references.

update

Besides you don't need any assignment

print for @_ x 2 should already work perfectly.

update

correction print for (@_) x 2 should already work perfectly.

DB<100> @_=1..3 => (1, 2, 3) DB<101> print for @_ x 2 => "" 33 DB<102> print for (@_) x 2 => "" 123123

from perlop

> In list context, if the left operand is enclosed in parentheses or is a list formed by qw/STRING/, it repeats the list.

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

Replies are listed 'Best First'.
Re^2: ... for (@_) x= 2; (scalar assignment)
by rsFalse (Chaplain) on Dec 29, 2015 at 13:53 UTC
    I supposed that three of my codes looks equivalently (for me), so and output should be the same - e.g. warning + compilation error.
      the list assignment returns the length of the list in scalar context, so
      DB<114> $l = ( @a = qw/a b/ ) => 2

      So it's not the same code!

      The difference between your code and my example is that there is no explicit variable (like $l) used here, but somehow it's still possible to change it.

      Maybe it's a side effect of for ? I dunno.

      I'd prefer a Can't modify warning here too!

      But it's still a very peculiar construction, so I'm not surprised if this edge case wasn't covered.

      HTH!

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

      update

      More insights:

      ... it's not the temporary scalar which is modified but the resulting list in brackets

      DB<121> print "$_\n" for ($a=666) x= 2 666 666 => "" DB<122> $a => 666

      According to the already cited documentation of "combined assignments" this shouldn't be possible, b/c its a list operation.

        I tried compare three of my codes and their outputs. 'x=' seems doing the same in all three cases - it asks '@_' to be a scalar.
Re^2: ... for (@_) x= 2; (scalar assignment)
by rsFalse (Chaplain) on Dec 29, 2015 at 14:24 UTC
    LanX cited: "> In list context, if the left operand is enclosed in parentheses or is a list formed by qw/STRING/, it repeats the list."

    Now I tried to write some lines with 'qw' and found next interesting situation, which I can't understand:
    #!/usr/bin/perl use warnings; use strict; $\ = $/; $, = '+'; print qw/a b/ x 2; print scalar(qw/a b/ x 2); print for (qw/a b/ x 2) x 2; print for qw/a b/ x 2 x 2;
    OUTPUTS:
    a+b+a+b
    bb
    a b a b a b a b
    bbbb
    The strange for me is the output of second 'print' (of course so does 4th too): it prints "bb" instead of supposed "b". I don't catch that, because print scalar('a','b','a','b'); would print "b".
      the documentation says that x can only act like a list repeater in list context.

      > > In list context, if the left operand is enclosed in parentheses or is a list formed by qw/STRING/, it repeats the list.

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

        Thanks :) . Now I catched it!