in reply to Re^2: Operator overloading with returning lists?
in thread Operator overloading with returning lists?

x behaves different in list context as well
Not a very good example. It's more the LHS operand than the context which determines what is returned.
That's not all I said. I immediately followed it (not even stopping to start a new sentence) with
but only if its LHS has parens, making it an odd one
Both context and the LHS matter, just as I said:
my $w = 'x' x 5; say "$w"; # Scalar context, no parens. my $x = ('x') x 5; say "$x"; # Scalar context, parens. my @y = 'x' x 5; say "@y"; # List context, no parens. my @z = ('x') x 5; say "@z"; # List context, parens. __END__ xxxxx xxxxx xxxxx x x x x x

Replies are listed 'Best First'.
Re^4: Operator overloading with returning lists?
by LanX (Saint) on Dec 01, 2008 at 14:42 UTC
    since when does scalar context of a list result in a join???
    DB<88> p Dumper (('x') x 5) $VAR1 = 'x'; $VAR2 = 'x'; $VAR3 = 'x'; $VAR4 = 'x'; $VAR5 = 'x'; DB<89> p Dumper scalar (('x') x 5) $VAR1 = 'xxxxx'; DB<90> p Dumper scalar (@x=('x') x 5) $VAR1 = 5; DB<91> p Dumper (scalar qw/x x x x x/) $VAR1 = 'x';

    mysterious perl ... 8(

    Cheers Rolf

    UPDATE:

    DB<97> p Dumper scalar ( (1,2) x 5 ) $VAR1 = '22222'; DB<98> p Dumper ( (1,2) x 5 ) $VAR1 = 1; $VAR2 = 2; $VAR3 = 1; $VAR4 = 2; $VAR5 = 1; $VAR6 = 2; $VAR7 = 1; $VAR8 = 2; $VAR9 = 1; $VAR10 = 2;

    The scalar is applied to the list before the x-op can act. The parens are ignored...

    Thats a bug or a feature?

      Perl doesn't see things as left-to-right as you do.

      "scalar" in "scalar ( (1,2) x 5 )" acts on "x", not "(1,2)".
      The return value of "()x" in scalar context is (join '', map LHS, 1..RHS).

      Similarly, "scalar" in "scalar ( ($a) = 5 )" acts on "=", not "($a)".
      The return value of "()=" in scalar context is the number of items returned by its RHS.

      Not a bug. In both case, the code works as designed and as intended.

      Neither.
      C:\>perl -MData::Dumper -le"print Dumper scalar ( 1, 2 ) " $VAR1 = 2; C:\>perl -MData::Dumper -le"print Dumper scalar ( 1, 2 ) x 5" $VAR1 = '22222'; C:\>perl -MData::Dumper -le"print Dumper 2 x 5" $VAR1 = '22222';
      "List" Is a Four-Letter Word
        Thx for the link, but the only thing that might explain what happens is lists do not exist in scalar context .

        such that

        DB<97> p Dumper scalar ( (1,2) x 5 ) $VAR1 = '22222';
        transforms to
        DB<101> p Dumper scalar(1,2) x 5 $VAR1 = '22222';

        Cheers Rolf

        UPDATE: after reading: http://perldoc.perl.org/perlop.html#Multiplicative-Operators I understand that the scalar context inhibits the parens around (1,2) !
      since when does scalar context of a list result in a join???
      It does? Which of your examples do you think supports this notion?
      The scalar is applied to the list before the x-op can act. The parens are ignored...
      I do not know what you mean by this.
      Thats a bug or a feature?
      From 'man perlop':
             Binary "x" is the repetition operator.  In scalar context or if the
             left operand is not enclosed in parentheses, it returns a string
             consisting of the left operand repeated the number of times specified
             by the right operand.  In list context, if the left operand is enclosed
             in parentheses or is a list formed by "qw/STRING/", it repeats the
             list.  If the right operand is zero or negative, it returns an empty
             string or an empty list, depending on the context.
      
        The scalar is applied to the list before the x-op can act. The parens are ignored...
        I do not know what you mean by this.
        Context is not a function applied after the parens are evaluated. In other words: Precedence is weaker than context!

        Cheers Rolf