-
Notifications
You must be signed in to change notification settings - Fork 577
perldata doesn't tell what happens on list assignment, with less/more elements #16289
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
Comments
From [email protected]This is a bug report for perl from calestyo@scientia.net, perldata explains most things related to lists, arrays, etc. and However, I feel it misses one crucial point, namely what happens @more=(1, 2, 3, 4); and with that each of the following then the cases: Seems the result is, that if there is - as above - no array or hash Cheers, Flags: Site configuration information for perl 5.26.1: Configured by Debian Project at Tue Nov 28 17:44:14 UTC 2017. Summary of my perl5 (revision 5 version 26 subversion 1) configuration: Locally applied patches: @INC for perl 5.26.1: Environment for perl 5.26.1: |
From @jkeenanOn Thu, 07 Dec 2017 04:00:45 GMT, calestyo@scientia.net wrote:
Please review the patch attached. Thank you very much. -- |
From @jkeenan132538-0001-Clarify-different-cases-of-assignment-to-list-of-sca.patchFrom e01ff6d79771f0c623f6be7a477289c8edd26d35 Mon Sep 17 00:00:00 2001
From: James E Keenan <[email protected]>
Date: Thu, 7 Dec 2017 16:09:54 -0500
Subject: [PATCH] Clarify different cases of assignment to list of scalars.
Prepared in response to RT #132538.
---
pod/perldata.pod | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 50 insertions(+)
diff --git a/pod/perldata.pod b/pod/perldata.pod
index 9d714f2..b093e3a 100644
--- a/pod/perldata.pod
+++ b/pod/perldata.pod
@@ -778,6 +778,56 @@ As of Perl 5.22, you can also use C<(undef)x2> instead of C<undef, undef>.
(You can also do C<($x) x 2>, which is less useful, because it assigns to
the same variable twice, clobbering the first value assigned.)
+Note that assignment of one list of scalars to another list of scalars is
+B<not> the same as assigning that first list to an array.
+
+When you assign a list of scalars to an array, all previous values in that
+array are wiped out and number of elements in the array will now be equal to
+the number of elements in the right-hand list -- the list from which
+assignment was made. The array will automatically resize itself to precisely
+accommodate each element in the right-hand list.
+
+ use warnings;
+ my (@xyz, $x, $y, $z);
+
+ @xyz = (1, 2, 3);
+ print "@xyz\n"; # 1 2 3
+
+ @xyz = ('al', 'be', 'ga', 'de');
+ print "@xyz\n"; # al be ga de
+
+ @xyz = (101, 102);
+ print "@xyz\n"; # 101 102
+
+When, however, you assign a list of scalars to another list of scalars, the
+results differ according to whether the left-hand list -- the list being
+assigned to -- has the same, more or fewer elements than the right-hand list.
+
+ ($x, $y, $z) = (1, 2, 3);
+ print "$x $y $z\n"; # 1 2 3
+
+ ($x, $y, $z) = ('al', 'be', 'ga', 'de');
+ print "$x $y $z\n"; # al be ga
+
+ ($x, $y, $z) = (101, 102);
+ print "$x $y $z\n"; # 101 102
+ # Use of uninitialized value $z in concatenation (.)
+ # or string at [program] line [line number].
+
+If the number of scalars in the left-hand list is less than that in the
+right-hand list, the "extra" scalars in the right-hand list will simply not be
+assigned.
+
+If the number of scalars in the left-hand list is greater than than in the
+left-hand list, the "missing" scalars will become undefined.
+
+ ($x, $y, $z) = (101, 102);
+ for my $el ($x, $y, $z) {
+ (defined $el) ? print "$el " : print "<undef>";
+ }
+ print "\n";
+ # 101 102 <undef>
+
List assignment in scalar context returns the number of elements
produced by the expression on the right side of the assignment:
--
2.7.4
|
The RT System itself - Status changed from 'new' to 'open' |
From @cpansproutOn Thu, 07 Dec 2017 13:18:52 -0800, jkeenan wrote:
I find this sentence confusing. I think the rest of your patch clarifies things sufficiently that you can just leave out this sentence.
*the* number
s/than tha\Kn/t/ -- Father Chrysostomos |
From @jkeenanOn Fri, 08 Dec 2017 00:55:01 GMT, sprout wrote:
Pushed to blead with those corrections in commit 436908e. Marking ticket Resolved. Thank you very much. -- |
@jkeenan - Status changed from 'open' to 'resolved' |
From [email protected]Hi. Thanks for the patch :) My comments below: +Note that assignment of one list of scalars to another list of scalars is => I also think this is confusing... +When you assign a list of scalars to an array, all previous values in that => I would add here (just for convenience): + use warnings; + my (@xyz, $x, $y, $z); => could also fit in 3 lines, but I'm fine with it :) + => "... or an array" , the => one might add here, that 'de' is of course still evaluated, but just discarded + => there are two times left-hand list... the 2nd should be right hand list + Thanks :-) |
Migrated from rt.perl.org#132538 (status was 'resolved')
Searchable as RT132538$
The text was updated successfully, but these errors were encountered: