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
'x' forces scalar context on array variables #9526
Comments
From jschneid@redhat.comCreated by jschneid@redhat.comThe 'x' operator forces scalar context on the left operand, unless For example, the following code fragment prints the string "22", rather @foo = qw(foo bar); The alternative: print "$_\n" foreach qw(foo bar) x 2; does print the alternating lines. "perldoc perlop" is not clear on the expected behavior - quoting: In scalar context or if the left operand is not enclosed in parentheses, It could be read that the behavior is the documented behavior, but it In the event this is not fixed, I suggest the passage above be amended to: The "x" operator will evaluate its left operand in scalar context, Perl Info
|
From @schwernJames Schneider (via RT) wrote:
I think what you're seeing in the second example is a quirk of Perl's
Because operands don't have context, only operators. That's sort of like For example, print evaluates its arguments in list context. When you write I suppose one could look at 'x' as being two different operators with the same You're right that C<< @foo x 4 >> is confusing just as C<< reverse $foo >> is. In conclusion, the 'x' operator probably should just DWIM according to the $string = @foo x 4; # $string = join '', map { scalar @foo } 1..4; And if you want to force an operand into scalar context, you do it @list = scalar @foo x 4; # @list = map { scalar @foo } 1..4; But it's too late now. :(
Something like that, yes. I'd use the "white lie" approach, start with a Binary "x" is the repetition operator. It repeats the left operand, $line = '-' x 80; # a string of 80 dashes If the left operand is enclosed in parenthesis (including a @dashes = ('-') x 80; # a list of 80 dashes If the right operand is zero or negative, it returns an empty string -- |
The RT System itself - Status changed from 'new' to 'open' |
From @ikegamiOn Tue, Oct 14, 2008 at 11:40 AM, Michael G Schwern <schwern@pobox.com>wrote:
Including qw/STRING/ enclosed in parenthesis? If the left operand is enclosed in parenthesis or if it's a qw/STRING/, |
From @schwernEric Brine wrote:
Ahh, I see the ambiguity. I want to make it clear that qw/STRING/ isn't a If the left operand is enclosed in parenthesis (qw/STRING/ is considered to be Speaking of weird exceptions... $ perl -wle '@foo = ("a".."b"); $x = @foo x 2; print $x' $ perl -wle '@foo = ("a".."b"); $x = (@foo) x 2; print $x' $ perl -wle '$x = ("a".."b") x 2; print $x' $ perl -wle '$x = (("a".."b") x 2); print $x' Which seems to be a special case of.. $ perl -wle 'print scalar (1..2)' And I'm not going to pretend I understand scalar flip/fop. -- |
From perl@nevcal.comOn approximately 10/14/2008 9:15 AM, came the following characters from
Except for your weird exceptions below, I was about to suggest saying But the weird exceptions do point out that the wording "if the left It is certainly true that not everything in Perl that is enclosed in The Perl user does need to understand the differences between arrays, 'x' does seem to be somewhat unique in that its own context affects the
--
|
From @schwernGlenn Linderman wrote:
Hmm, good point. I can't think of any offhand. -- |
From blgl@hagernas.comIn article <48F4CD2B.6050205@NevCal.com>, perl@NevCal.com (Glenn Linderman)
Unary: + /Bo Lindbergh |
From Eirik-Berg.Hanssen@allverden.noBo Lindbergh <blgl@hagernas.com> writes:
Also, comma. If comma in list context is considered an operator? sub w { $w=wantarray; push @w, $w } __END__ $VAR1 = [ Eirik |
From @jkeenanOn Mon Oct 13 14:11:35 2008, jschneid@redhat.com wrote:
I reviewed this older ticket this evening. The OP was concerned about ##### Drop the parentheses around the '@foo' and you get the second case: ##### ... which the OP felt violated the principle of least surprise, even if ##### I am attaching a patch in the same spirit as a revision suggested by the Thank you very much. |
From @jkeenan0001-Clarify-case-of-x-operator-with-left-operand-lacking.patchFrom bbd88daf10e3c66a615601e34a020bd10361b08c Mon Sep 17 00:00:00 2001
From: James E Keenan <jkeenan@cpan.org>
Date: Mon, 18 Feb 2013 21:51:19 -0500
Subject: [PATCH] Clarify case of "x" operator with left operand lacking parens.
Modification of documentation change suggested by James Schneider.
For: RT #59884.
---
pod/perlop.pod | 33 +++++++++++++++++++--------------
1 files changed, 19 insertions(+), 14 deletions(-)
diff --git a/pod/perlop.pod b/pod/perlop.pod
index 40402be..c1b1944 100644
--- a/pod/perlop.pod
+++ b/pod/perlop.pod
@@ -297,22 +297,27 @@ operator is not as well defined for negative operands, but it will
execute faster.
X<%> X<remainder> X<modulo> X<mod>
-Binary "x" is the repetition operator. In scalar context or if the left
-operand is not enclosed in parentheses, it returns a string consisting
-of the left operand repeated the number of times specified by the right
-operand. In list context, if the left operand is enclosed in
-parentheses or is a list formed by C<qw/STRING/>, it repeats the list.
-If the right operand is zero or negative, it returns an empty string
-or an empty list, depending on the context.
+Binary "x" is the repetition operator. It will evaluate its left
+operand in scalar context unless (a) the left operand is enclosed in
+parentheses or (b) is a list formed by C<qw/STRING/> -- in which case it
+will evaluate its left operand in list context. In scalar context, it
+returns a string consisting of the left operand repeated the number of
+times specified by the right operand. In list context, it repeats the
+list.
X<x>
- print '-' x 80; # print row of dashes
-
- print "\t" x ($tab/8), ' ' x ($tab%8); # tab over
-
- @ones = (1) x 80; # a list of 80 1's
- @ones = (5) x @ones; # set all elements to 5
-
+ print '-' x 80; # print row of dashes
+ print "\t" x ($tab/8),
+ ' ' x ($tab%8); # tab over
+
+ @terms = ('foo', 'bar');
+ print "$_\n" for @terms x 2; # prints "22\n"; lack of parens
+ # on left operand causes @terms
+ # to be evaluated in scalar context
+
+ @ones = (1) x 80; # a list of 80 1's
+ @ones = (5) x @ones; # set all elements to 5
+ @terms = qw/foo bar/ x 2; # ('foo', 'bar', 'foo', 'bar')
=head2 Additive Operators
X<operator, additive>
--
1.6.3.2
|
From @tamiasOn Mon, Feb 18, 2013 at 06:55:34PM -0800, James E Keenan via RT wrote:
The original text is precise and correct, while the proposed text conflates If the 'x' operator is in scalar context, then the left operand is If the 'x' operator is in list context *and* the left operand is enclosed Ronald |
From tchrist@perl.com"James E Keenan via RT" <perlbug-followup@perl.org> wrote
The doc patch looks good. Thanks. I agree with the original submitter that this is all somewhat bizarre, but I doubt if we had it to do over again, we would do it this way. --tom |
From @jkeenanOn Mon Feb 18 19:43:03 2013, tom christiansen wrote:
We had one vote for the patch (tomc) and one against (ronald). Hence, I Thank you very much. |
From @bulk88On Tue Mar 12 18:21:40 2013, jkeenan wrote:
I like the current blead wording more. |
From @rjbs* James E Keenan via RT <perlbug-followup@perl.org> [2013-03-12T21:21:40]
I agree with Ronald's criticism. -- |
From @jkeenanOn Wed Mar 13 06:23:09 2013, perl.p5p@rjbs.manxome.org wrote:
Marking as rejected. Thank you very much. Jim Keenan |
@jkeenan - Status changed from 'open' to 'rejected' |
Migrated from rt.perl.org#59884 (status was 'rejected')
Searchable as RT59884$
The text was updated successfully, but these errors were encountered: