Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

perl - Scalar vs List Assignment Operator

Please help me understand the following snippets:

  • my $count = @array;
  • my @copy = @array;
  • my ($first) = @array;
  • (my $copy = $str) =~ s/\/\\/g;
  • my ($x) = f() or die;
  • my $count = () = f();
  • print($x = $y);
  • print(@x = @y);
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The symbol = is compiled into one of two assignment operators:

  • A list assignment operator (aassign) is used if the left-hand side (LHS) of a = is some kind of aggregate.
  • A scalar assignment operator (sassign) is used otherwise.

The following are considered to be aggregates:

  • Any expression in parentheses (e.g. (...))
  • An array (e.g. @array)
  • An array slice (e.g. @array[...])
  • A hash (e.g. %hash)
  • A hash slice (e.g. @hash{...})
  • Any of the above preceded by my, our or local

There are two differences between the operators.

Context of Operands

The two operators differ in the context in which their operands are evaluated.

  • The scalar assignment evaluates both of its operands in scalar context.

    # @array evaluated in scalar context.
    my $count = @array;
    
  • The list assignment evaluates both of its operands in list context.

    # @array evaluated in list context.
    my @copy = @array;
    

    # @array evaluated in list context.
    my ($first) = @array;
    

Value(s) Returned

The two operators differ in what they return.

  • The scalar assignment ...

    • ... in scalar context evaluates to its LHS as an lvalue.

      # The s/// operates on $copy.
      (my $copy = $str) =~ s/\/\\/g;
      
    • ... in list context evaluates to its LHS as an lvalue.

      # Prints $x.
      print($x = $y);
      
  • The list assignment ...

    • ... in scalar context evaluates to the number of scalars returned by its RHS.

      # Only dies if f() returns an empty list.
      # This does not die if f() returns a
      # false scalar like zero or undef.
      my ($x) = f() or die;
      

      # $counts gets the number of scalars returns by f().
      my $count = () = f();
      
    • ... in list context evaluates to the scalars returned by its LHS as lvalues.

      # Prints @x.
      print(@x = @y);
      

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...