Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: A list assignment gotcha

by choroba (Archbishop)
on Jul 30, 2020 at 13:02 UTC ( #11120065=note: print w/replies, xml ) Need Help??


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

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]

Replies are listed 'Best First'.
Re^3: A list assignment gotcha (Updated)
by LanX (Cardinal) on Jul 30, 2020 at 14:48 UTC
    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...

        > will cause problems if used in inner loops

        performance won't be great unless implemented in XS.

        But that's the way I would implement new features in Perl.

        • a nice syntactic sugar which does the job in pure Perl and stays fully backwards compatible.
        • speed it up in XS for newer versions
        • if it becomes popular, consider including it into CORE.

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11120065]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2021-01-20 12:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?