develooper Front page | perl.perl6.users | Postings from November 2020

The ,= operator

Thread Next
Joseph Brenner
November 26, 2020 22:29
The ,= operator
Message ID:
I was going through the operator list in the documentation the
other day, and I noticed this one:

  postfix ,=

  Creates an object that concatenates, in a class-dependent way,
  the contents of the variable on the left hand side and the
  expression on the right hand side:

     my %a = :11a, :22b;
     %a ,= :33x;
     say %a # OUTPUT: «{a => 11, b => 22, x => 33}␤»

I was playing around with this a bit and now I'm wondering what
it's talking about with that "class-dependent way".

To start with, I've got a small issue with that example:
that particular pair-input syntax is my least favorite.
Flipping around the order of key and value when the value is a numeric...?

And it isn't needed to demo the operator, any pair input syntax works.
I might argue that examples should favor the form that "say" is
going to spit back to you.  This would be clearer at a glance:

   my %b = a => 11, b => 22;
   %b ,= x => 33;
   say %b; # OUTPUT: «{a => 11, b => 22, x => 33}␤»

But where it gets interesting is trying a ,= on something besides
a hash.  Let's try arrays, where I would expect this to push a
value onto the array:

  my @r = 'a', 'b', 'c';
  say @r; # [a b c]
  @r ,= 'd';
  say @r; # (\Array_53270704 = [Array_53270704 d])
  dd @r;  # Array @r = ((my @Array_37059224) = [@Array_37059224, "d"])

It's difficult for me to even see what that is or how I would use it:

  say "keys: ", @r.keys;      # keys: (0 1)
  say @r[0];  # (\Array_44472736 = [Array_44472736 d])
  say @r[1];  # d

So it *does* push a 'd', but it first mangles the original
contents of the array.  It looks like @r[0] now has some sort
of circular pointer back to @r.

Whatever this "class-dependent" behavior is, I would suggest
it's a "fail".  Could this be a bug?

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About