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
Array deref in return statement causes incorrect value in scalar context #7852
Comments
From perl-bug@owl.me.ukCreated by perl-bug@owl.me.ukDear all, I have noticed that including an array deref in the return statement Take the following script as an example: --- start --- use strict; sub a { sub b { sub c { sub d { print a; print scalar a; The output is as follows: --- start --- All four subs return the list ('tom', 'dick', 'harry') when called in sub c, which returns a list containing a scalar and a deref'd array sub d is even more interesting, since that evaluates to 'harry' in Thanks for listening! ~ Rich Perl Info
|
From perl-bug@owl.me.ukAha, I have found something in perlfunc(1) which leads me to understand Because "scalar" is unary operator, if you accidentally use for So because the return statements of c and d are "comma expressions", I must admit that I am surprised that expressions of this type can make 1. All subs should return arrays or scalar, and if a list is returned, ~ Rich |
perl-bug@owl.me.uk - Status changed from 'new' to 'open' |
From rick@bort.caOn Sun, Mar 27, 2005 at 12:30:19PM -0000, Rich Daley wrote:
[...]
This is correct behaviour. In scalar context, the comma operator ('tom', @$foo); returns the result of @$foo in scalar context, which is 2. (@$foo, 'harry'); returns the result of 'harry' in scalar context, which is 'harry'. -- |
@rgs - Status changed from 'open' to 'resolved' |
From perl5-porters@ton.iguana.beIn article <rt-3.0.11-34582-109696.13.4721151028463@perl.org>, Not a bug. When you see a construct like In return (foo, bar, baz) the () are unneeded since the precedence is such that the comma operators return foo, bar, baz Like every perl operator, "," is really two different operators, one for sub a { @bar is executed in scalar context => you get 3 sub c {
Nope. sub c doesn't return a list. There is no such thing as returning
sub c executed in scalar context will do 'tom', @$foo in scalar context, sub d {
@$foo, 'harry' is executed in scalar context. First it executes @$foo |
From RandyS@ThePierianSpring.orgRich Daley (via RT) wrote:
The return statement in sub c and sub d, despite appearances is not return ('tom', @$foo); # same as: return @$foo; You might try something like: return @{[ 'tom', @$foo ]}; This behavior is consistent going back to 5.005 and with perl's C-like |
From perl-bug@owl.me.ukApologies in advance for reopening the ticket. I understand now that this is the intended behaviour. However, even 1. If a comma is evaluated in scalar context, or I understand that some people would want to use this legitimately, but I Thanks for your time! ~ Rich |
perl-bug@owl.me.uk - Status changed from 'resolved' to 'open' |
From @ysthOn Sun, Mar 27, 2005 at 09:11:12AM -0500, Rick Delaney wrote:
Quite correct. I actually would rather scalar context comma evaluated Note the results from this: $ perl -wle'sub ctx{print wantarray?"list":defined wantarray?"scalar":"void"} sub foo { ctx,ctx,ctx } print "void call"; foo for 1; print "scalar call"; grep foo, 1; print "list call"; map foo, 1' =head2 Comma Operator Binary "," is the comma operator. In scalar context it evaluates In list context, it's just the list argument separator, and inserts |
From rick@bort.caOn Sun, Mar 27, 2005 at 12:13:12PM -0800, Yitzchak Scott-Thoennes wrote:
That wouldn't be very useful. Then there wouldn't be any result from the -- |
From @ysthOn Sun, Mar 27, 2005 at 04:44:21PM -0500, Rick Delaney wrote:
Arrg. s/right/left/ |
From @ysthOn Sun, Mar 27, 2005 at 12:13:12PM -0800, Yitzchak Scott-Thoennes wrote:
Hmm, there appears to be a bug in the above. When the context is $ perl -wle'sub ctx{print wantarray?"list":defined wantarray?"scalar":"void"} |
p5p@spam.wizbit.be - Status changed from 'open' to 'resolved' |
Migrated from rt.perl.org#34582 (status was 'resolved')
Searchable as RT34582$
The text was updated successfully, but these errors were encountered: