New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
perlop / list assignment documentation #9820
Comments
From @ig3Created by ian@alula.localHere is a patch to perlop.pod which, I think, clarifies the Perl Info
|
From @ig30001-Clarified-description-of-the-assignment-operator.patchFrom 165a188b1d79244c58e9ed0d4be753b6ee674f7d Mon Sep 17 00:00:00 2001
From: Ian Goodacre <Ian.Goodacre@xtra.co.nz>
Date: Sat, 8 Aug 2009 11:10:17 +1200
Subject: [PATCH] Clarified description of the assignment operator.
---
pod/perlop.pod | 27 ++++++++++++++++++++-------
1 files changed, 20 insertions(+), 7 deletions(-)
diff --git a/pod/perlop.pod b/pod/perlop.pod
index e677430..b9ec357 100644
--- a/pod/perlop.pod
+++ b/pod/perlop.pod
@@ -731,7 +731,12 @@ X<%=> X<^=> X<x=>
"=" is the ordinary assignment operator.
-Assignment operators work as in C. That is,
+There are two forms of the assignment operator: scalar assignment and list
+assignment.
+
+If the left operand is a scalar lvalue then the assignment operator
+is a scalar assignment operator. Scalar assignment operators work
+as in C. That is,
$a += 2;
@@ -740,8 +745,8 @@ is equivalent to
$a = $a + 2;
although without duplicating any side effects that dereferencing the lvalue
-might trigger, such as from tie(). Other assignment operators work similarly.
-The following are recognized:
+might trigger, such as from tie(). Other scalar assignment operators work
+similarly. The following are recognized:
**= += *= &= <<= &&=
-= /= |= >>= ||=
@@ -767,10 +772,18 @@ is equivalent to
$a += 2;
$a *= 3;
-Similarly, a list assignment in list context produces the list of
-lvalues assigned to, and a list assignment in scalar context returns
-the number of elements produced by the expression on the right hand
-side of the assignment.
+If the left operand of the assignment operator is a list then the operator
+is a list assignment operator. In this case, each element of the list
+must be an lvalue or C<undef>. The right operand is evaluated in list context
+and elements of the resulting list are assigned to elements of the list on the
+left hand side. See L<List value constructors> in L<perldata> for examples.
+
+In scalar context, the list assignment operator returns the number of
+elements produced by the expression on the right hand side of the
+assignment.
+
+In list context, the list assignment operator returns the list of
+lvalues assigned to.
=head2 Comma Operator
X<comma> X<operator, comma> X<,>
--
1.6.1
|
From Eirik-Berg.Hanssen@allverden.no"ian.goodacre@xtra.co.nz (via RT)" <perlbug-followup@perl.org> writes:
Please don't call the left operand a "list". That is not just Consider: "is an array, a hash, a slice, or enclosed in parentheses". Consider: In the following, is f a list? a scalar? both? use 5.010; Eirik |
The RT System itself - Status changed from 'new' to 'open' |
From Eirik-Berg.Hanssen@allverden.noEirik Berg Hanssen <Eirik-Berg.Hanssen@allverden.no> writes:
Come to think of it, I could do that one even clearer: use 5.010; Eirik |
From @ig3Eirik Berg Hanssen via RT wrote:
Despite having submitted the patch, because I think the description I would be happy to submit a revised patch incorporating your (Eirik's) I am curious why you (Eirik) say "enclosed in parentheses" rather than I am uncertain whether it makes more sense to say that the assignment Regards, |
From @davidnicolOn Mon, Aug 10, 2009 at 7:44 AM, Ian Goodacre<Ian.Goodacre@xtra.co.nz>
As I understand it, there is a weak taboo against calling an "array"
Me too. I think a chart of various examples and what happens with
lvalue subroutines are always scalar. "list" syntax, in scalar catnip:~# perl -wle 'my $x = (5,6); print $x' In the first, the list is parsed as a list. In the second, the list is
Context is provided by the left side of an assignment. lvalue As l-values, parentheses make lists. As r-values, they make lists in catnip:~# perl -wle 'my $x = () = (5,6); print $x' We might say that the empty list is parsed as an array, then evaluated
-- |
From Eirik-Berg.Hanssen@allverden.noDavid Nicol <davidnicol@gmail.com> writes:
Because parentheses do not make a list, except in a certain Nowhere else. (Unless I'm forgetting yet another special case.)
Really now? use 5.010; Not only does say() evaluate f in list context, list assignment does In a nutshell: The lvalueness of f does not determine whether it Always context. And the list context seems to be provided by the parentheses, but is
This example is a list assignment because of the parentheses, and
By the syntax of the assignment, actually. The context is
No, they only guide precedence either way. use 5.010; Except for scoping considerations, you could use do{ } blocks use 5.010; eval '( List repetition: 2-2-2 Bottom line: There is a whole lot of confusion surrounding lists in Eirik |
From @davidnicolOn Mon, Aug 10, 2009 at 11:20 AM, Eirik Berg Hanssen<Eirik-
and by "always scalar" I mean they are like $x not like @x when -- |
From Eirik-Berg.Hanssen@allverden.noDavid Nicol <davidnicol@gmail.com> writes:
Better, but still not quite right. lvalue subroutines are unlike use 5.010; my($x, $y, $f, @f, $g, @h); say "How many elements do they gobble up?"; Yeah, it's crazy. Crazy powerful. :) But also crazy complex. :-\ See how much simpler it is to note that assignment is list Eirik |
From @davidnicolOn Mon, Aug 10, 2009 at 3:18 PM, Eirik Berg
I would classify that gobbling of two list elements as a bug, as it f = (5,6); # sets $y to 6, leaves $x alone would be right. Which is intensely impressive. -- |
From @ig3Eirik Berg Hanssen wrote:
I am thinking that should be: an array, a hash, an array or hash slice, Because trying to assign to a list slice produces a compile time error my ($x,$y,$z,$h,$i,$j) = (1..6); say f,$h,$i,$j; Adding parentheses makes it a list assignment, but it still doesn't compile: my ($x,$y,$z,$h,$i,$j) = (1..6); say f,$h,$i,$j; I guess the compile error is because list slices don't return lvalues. Eirik Berg Hanssen wrote:
None the less, it seems a list slice is a third special case where Ian Goodacre |
From Eirik-Berg.Hanssen@allverden.noIan Goodacre <Ian.Goodacre@xtra.co.nz> writes:
I was not aware of that, but it does rather make sense. :)
Right again[*]. Good catches both. :) [*]: To the extent that the interpretation to which I was referring is Eirik -- Larry Wall |
From @ig3Thanks for all the comments and examples. I have learned a lot... What do you thing of this revised patch? Ian Goodacre |
From @ig30001-Revised-assignment-operator-docs.patchFrom e3b7da907b3774fffa3ffeb3b715e914b4612faf Mon Sep 17 00:00:00 2001
From: Ian Goodacre <Ian.Goodacre@xtra.co.nz>
Date: Tue, 11 Aug 2009 22:30:15 +1200
Subject: [PATCH] Revised assignment operator docs
Elaborated description of list assignment operator, providing a description
of left operands that cause an assignment operator to be a list assignment
operator, and reorganized section.
---
pod/perlop.pod | 53 ++++++++++++++++++++++++++++++++++++-----------------
1 files changed, 36 insertions(+), 17 deletions(-)
diff --git a/pod/perlop.pod b/pod/perlop.pod
index e677430..bcb5ad4 100644
--- a/pod/perlop.pod
+++ b/pod/perlop.pod
@@ -729,9 +729,26 @@ X<assignment> X<operator, assignment> X<=> X<**=> X<+=> X<*=> X<&=>
X<<< <<= >>> X<&&=> X<-=> X</=> X<|=> X<<< >>= >>> X<||=> X<//=> X<.=>
X<%=> X<^=> X<x=>
-"=" is the ordinary assignment operator.
+"=" is the ordinary assignment operator. There are two forms of this
+operator: list assignment and scalar assignment. If the left operand
+is an array, a hash, an array or hash slice or enclosed in parentheses,
+then the assignment is a list assignment. Otherwise it is a scalar assignment.
-Assignment operators work as in C. That is,
+There are other assignment operators, as in C. The following are recognized:
+
+ **= += *= &= <<= &&=
+ -= /= |= >>= ||=
+ .= %= ^= //=
+ x=
+
+These other assignment operators are all scalar assignment operators.
+Although they are grouped by family, they all have the precedence
+of assignment.
+
+The scalar assignment operators provide scalar context to both their operands.
+The left operand must be an lvalue (meaning you can assign to it).
+
+The other assignment operators work as in C. That is,
$a += 2;
@@ -740,18 +757,9 @@ is equivalent to
$a = $a + 2;
although without duplicating any side effects that dereferencing the lvalue
-might trigger, such as from tie(). Other assignment operators work similarly.
-The following are recognized:
-
- **= += *= &= <<= &&=
- -= /= |= >>= ||=
- .= %= ^= //=
- x=
+might trigger, such as from tie().
-Although these are grouped by family, they all have the precedence
-of assignment.
-
-Unlike in C, the scalar assignment operator produces a valid lvalue.
+Unlike in C, the scalar assignment operators produce a valid lvalue.
Modifying an assignment is equivalent to doing the assignment and
then modifying the variable that was assigned to. This is useful
for modifying a copy of something, like this:
@@ -767,10 +775,21 @@ is equivalent to
$a += 2;
$a *= 3;
-Similarly, a list assignment in list context produces the list of
-lvalues assigned to, and a list assignment in scalar context returns
-the number of elements produced by the expression on the right hand
-side of the assignment.
+The list assignment operator provides list context to both its operands.
+
+If the left operand of the list assignment operator is enclosed in parentheses,
+each element of the enclosed list must be an lvalue or C<undef>.
+
+In scalar context, the list assignment operator returns the number of
+elements produced by the expression on the right hand side of the
+assignment.
+
+In list context, the list assignment operator returns the list of
+lvalues assigned to.
+
+See L<List value constructors> in L<perldata> for examples of list assignment.
+
+
=head2 Comma Operator
X<comma> X<operator, comma> X<,>
--
1.6.1
|
From Eirik-Berg.Hanssen@allverden.noIan Goodacre <Ian.Goodacre@xtra.co.nz> writes:
You're welcome. It's nice when one's rants are appreciated. :)
It is not my call, but since I've involved myself in this request, It looks good to me. Certainly a significant improvement on the Eirik, with thumbs up |
From @davidnicolI like "list assignment in scalar context" as that is exactly what
but that is how lists are in scalar context: this behavior is not As an r-value, the list assignment operator passes its own @XmlThings = m{<thing>(.+)</thing>}gs or do{ |
From Eirik-Berg.Hanssen@allverden.noDavid Nicol <davidnicol@gmail.com> writes:
I feel another rant coming up ... one of the old ones: Lists are never in scalar context. List assignment may be, list slices may be, but lists may not. (List repetitions may not be in scalar context either, surpisingly
What is "its own r-value"? If you mean to say the "elements (And if you meant something else, I found it less clear than what use 5.010; 3 elements, not 10. Specifically, the three elements assigned to. Aside: And none of them undef either. In fact, this is one ... rather use 5.010; The assignment to ($u) evaluates to a list of one element, the value Eirik |
From @davidnicolOn Tue, Aug 11, 2009 at 2:31 PM, Eirik Berg Hanssen<Eirik-
Yes, that's what I meant. Thanks for straightening out my misconception.
wow. /tmp: $ perl -le '@a=qw/24 57 32 hike!/; $count = () = @a; print $count' /tmp: $ So list assignment in scalar context returns the count of the RHS, but
Is that not a bug, that an assignment to a constant ($u is an alias to /tmp: $ perl -le ' $count = undef = 3; print $count' |
From @gbarrOn Aug 11, 2009, at 3:40 PM, David Nicol wrote:
unless the element in the LHS is undef, in which case it returns the $ perl -le '@a=qw/24 57 32 hike!/; @copy = ($x,undef,$y) = @a; $,=","; I was about to say that a list assignment in a list context returns {package Bar; will output 88,2 Graham. |
From Eirik-Berg.Hanssen@allverden.noGraham Barr <gbarr@pobox.com> writes:
The RHS value, perhaps, but not the RHS element itself. Just like your elegant tied() demonstration, list assignment in use 5.010; Compare: use 5.010; Without spending too much text on corner cases nor misleading the Eirik |
From @ig3Below is a revision of the patch to perlop.pod which incorporates Inline Patchdiff --git a/pod/perlop.pod b/pod/perlop.pod
index 1df9fcb..7120a8c 100644
--- a/pod/perlop.pod
+++ b/pod/perlop.pod
@@ -729,9 +729,26 @@ X<assignment> X<operator, assignment> X<=> X<**=>
-"=" is the ordinary assignment operator. -Assignment operators work as in C. That is, $a += 2; @@ -740,18 +757,9 @@ is equivalent to although without duplicating any side effects that dereferencing the lvalue -Unlike in C, the scalar assignment operator produces a valid lvalue. -Similarly, a list assignment in list context produces the list of =head2 Comma Operator |
From @jkeenanThere was a lot of back-and-forth and revision of patches in this RT Could people evaluate the last patch submitted, below? Thank you very much. On Sun Aug 30 08:46:10 2009, iang wrote:
|
From @cpansproutOn Wed Mar 28 18:40:43 2012, jkeenan wrote:
I thought the consensus was that that was far too much detail, and that
-- Father Chrysostomos |
From @jkeenanOn Wed Mar 28 22:58:33 2012, sprout wrote:
Should we take that as a recommendation to reject and close the ticket? Thank you very much. |
From @cpansproutOn Thu Apr 05 19:09:01 2012, jkeenan wrote:
I think we should close it, if simply because it makes it easier to fix -- Father Chrysostomos |
From [Unknown Contact. See original ticket]On Thu Apr 05 19:09:01 2012, jkeenan wrote:
I think we should close it, if simply because it makes it easier to fix -- Father Chrysostomos |
From @jkeenanOn Fri Apr 06 06:22:20 2012, sprout wrote:
Closing ticket. |
@jkeenan - Status changed from 'open' to 'rejected' |
Migrated from rt.perl.org#68312 (status was 'rejected')
Searchable as RT68312$
The text was updated successfully, but these errors were encountered: