Skip to content
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

Conditional 'return()' in function call #14721

Open
p5pRT opened this issue May 30, 2015 · 3 comments
Open

Conditional 'return()' in function call #14721

p5pRT opened this issue May 30, 2015 · 3 comments

Comments

@p5pRT
Copy link

p5pRT commented May 30, 2015

Migrated from rt.perl.org#125286 (status was 'open')

Searchable as RT125286$

@p5pRT
Copy link
Author

p5pRT commented May 30, 2015

From trizenx@gmail.com

Let's assume the following code​:

#===BEGIN===#

use 5.010;
use strict;
use warnings;

sub a {
  return "foo";
}

sub b {
  my ($x, $y) = @​_;
  say $x; # $x is "foo"
  say $y; # $y is undefined
}

b(a() // return(), "bar"); # this fails
b(a() // return, "bar"); # this works
b(a() // (return()), "bar"); # this works

#===END===#

In the first call, the second parameter ($y) becomes undefined. This is triggered by the return keyword used with parentheses.

In the second call, after removing the parentheses, it seems to work correctly. Also, wrapping the return inside parentheses seems to work too.

As far as I tested, the issue can be reproduced only with the "return" keyword. It's reproducible with the following versions of Perl (and possible others)​: 5.14.4, 5.20.2, perl-5.22.0-RC2

Output​:

#===BEGIN===#
foo
Use of uninitialized value $y in say at x.pl line 12.

foo
bar
foo
bar
#===END===#

@p5pRT
Copy link
Author

p5pRT commented May 31, 2015

From zefram@fysh.org

Daniel Suteu wrote​:

b(a() // return(), "bar"); # this fails
b(a() // return, "bar"); # this works
b(a() // (return()), "bar"); # this works

Not a bug​: return just has exceptional grammatical behaviour, that it
doesn't turn into a function-like operator when followed by a parenthesis.
In the `failing' case, return's argument expression is ((), "bar").
You can see this by using B​::Deparse​:

$ perl -MO=Deparse -e 'b(a() // return(), "bar");'
b(a() // return((), 'bar'));
-e syntax OK

The other two cases `work' because they do not admit such a parsing.

It might be worth us adding a warning about this kind of case, where
return behaves differently from a regular list operator. The trigger
condition would be something like the "return" keyword being immediately
followed by a parenthesis and that parenthesised group immediately
followed by an infix operator (including comma). I'm not going to argue
the merits of warning.

We could also consider deprecating the current behaviour and turning
return into a regular list operator.

-zefram

@p5pRT
Copy link
Author

p5pRT commented May 31, 2015

The RT System itself - Status changed from 'new' to 'open'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants