in reply to A list assignment gotcha
in thread Not understanding 2 sentences in perldoc

> plain assignment will operate on lists, but modifying assignments (+= in the example that I recall) will not

If you really needed this, it could be done with a little syntactic sugar

something like

L($x,$y,$z) += L(1,2,3);

the trick would be to let L() ( for "list" ) return an object with overload ed operators (in scalar context) performing the side-effect

Tho I'm not sure if the RHS needs to be packed into an object too, but I assume += is imposing scalar context.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^2: A list assignment gotcha
by choroba (Cardinal) on Jul 30, 2020 at 13:02 UTC
    A bit trickier than I originally thought. What I didn't expect was the need to use the :lvalue on both the listifier and constructor.
    #!/usr/bin/perl use warnings; use strict; use feature qw{ say }; { package L; use overload '+=' => sub { my ($self, $inc) = @_; $_ += shift @$inc for @$self; }; sub new :lvalue { bless $_[1], $_[0] } } sub L :lvalue { 'L'->new(\@_) } my ($x, $y, $z) = (10, 20, 30); L($x, $y, $z) += L(3, 2, 1); say "$x $y $z"; # 13 22 31
    You can use [3, 2, 1] on the RHS, as well.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      Hi

      > A bit trickier than I originally thought.

      I was about to update that one needs :lvalue and returning a tied scalar then.

      Surprised you made it without tie , my last use case must have been different than. :)

      update

      > You can use [3, 2, 1] on the RHS, as well.

      Nice! :)

      > on both the listifier and constructor.

      Genius!!! that's how you avoided using Tie::Scalar! :)

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        Great work by both of you, and the possible syntax tricks nicely show TIMTOWTDI, but the original motive for trying LIST += LIST was a hoped-for performance gain from the interpreter completing the whole set in one opcode, instead of add...add...add....

        I suspect that overloaded objects like this will cause problems if used in inner loops. :-) Neat tricks, though...