develooper Front page | perl.perl5.porters | Postings from May 2015

[perl #125286] Conditional 'return()' in function call

Thread Next
From:
Daniel Șuteu
Date:
May 31, 2015 16:41
Subject:
[perl #125286] Conditional 'return()' in function call
Message ID:
rt-4.0.18-29309-1432945838-735.125286-75-0@perl.org
# New Ticket Created by  Daniel Șuteu 
# Please include the string:  [perl #125286]
# in the subject line of all future correspondence about this issue. 
# <URL: https://rt.perl.org/Ticket/Display.html?id=125286 >


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===#


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About