develooper Front page | perl.perl5.porters | Postings from December 2017

Re: [perl #132537] possible documentation error in perldata

Thread Previous | Thread Next
Christian Millour
December 8, 2017 10:08
Re: [perl #132537] possible documentation error in perldata
Message ID:
Le 08/12/2017 à 10:12, Dave Mitchell a écrit :
> On Wed, Dec 06, 2017 at 06:02:52PM -0800, via RT wrote:
>> says:
>>> LISTs do automatic interpolation of sublists. That is, when a LIST is evaluated,
>>> each element of the list is evaluated in list context, and the resulting list
>>> value is interpolated into LIST just as if each individual element were a member of LIST.
>> This seems to be wrong, in the sense, that the list
>> itself doesn't mandate its elements to be evaluated
>> in list context.
> It does by definition:
>> When doing $b=(1, 2, @a); the list is evaluated in scalar context
> No, in scalar context, there isn't a list; instead there is merely
> a sequence of comma operators (which return their RH argument), with
> parentheses added for the purpose of precedence.
FWIW I find the sentence above clearer than the current wording in perldata

  677 =head2 List value constructors
  678 X<list>
  680 List values are denoted by separating individual values by commas
  681 (and enclosing the list in parentheses where precedence requires it):
  683     (LIST)
  685 In a context not requiring a list value, the value of what appears
  686 to be a list literal is simply the value of the final element, as
  687 with the C comma operator.  For example,
  689     @foo = ('cc', '-E', $bar);
  691 assigns the entire list value to array @foo, but
  693     $foo = ('cc', '-E', $bar);
  695 assigns the value of variable $bar to the scalar variable $foo.

YMMV. From experience this is a significant hurdle for beginners.

@array  = (...): # OK
%hash   = (...); # OK
$scalar = (...); # Possibly not what was intended,
                  # consider $scalar = [...] instead ?

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