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

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

Thread Previous | Thread Next
From:
Christian Millour
Date:
December 8, 2017 10:08
Subject:
Re: [perl #132537] possible documentation error in perldata
Message ID:
015d31fa-7956-0a4e-5e12-eef639f61626@abtela.com
Le 08/12/2017 à 10:12, Dave Mitchell a écrit :
> On Wed, Dec 06, 2017 at 06:02:52PM -0800, via RT wrote:
>> https://perldoc.perl.org/perldata.html 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>
  679
  680 List values are denoted by separating individual values by commas
  681 (and enclosing the list in parentheses where precedence requires it):
  682
  683     (LIST)
  684
  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,
  688
  689     @foo = ('cc', '-E', $bar);
  690
  691 assigns the entire list value to array @foo, but
  692
  693     $foo = ('cc', '-E', $bar);
  694
  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


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About