What's the difference between return; and return undef; in Perl

后端 未结 4 831
天涯浪人
天涯浪人 2020-12-09 15:55

Is there a difference between a subroutine that does

return;

and one that does?

return undef;
相关标签:
4条回答
  • 2020-12-09 16:02

    return; will return an empty list in list context but undef in scalar context. return undef; will always return a single value undef even in list context.

    In general, it's usually not a good idea to return undef; from a subroutine normally used in list context:

    sub foo { return undef }
    if ( my @x = foo() ) {
        print "oops, we think we got a result";
    }
    

    In general, it's usually not a good idea to return; from a subroutine normally used in scalar context, because it won't behave as the user expects in list context:

    sub foo { return }
    %x = ( 'foo' => foo(), 'bar' => 'baz' );
    if ( ! exists $x{'bar'} ) {
        print "oops, bar became a value, not a key";
    }
    

    Both of these errors happen quite a bit in practice, the latter more so, perhaps because subs that are expected to return a scalar are more common. And if it's expected to return a scalar, it had better return a scalar.

    0 讨论(0)
  • 2020-12-09 16:19

    The book "Perl Best Practices" recommends the use of return; instead of return undef; because the latter would return a one-element list in list context (the other answers already mention this). This would be a "nasty bug" according to the book.

    However, I think returning nothing can actually cause serious bugs (probably difficult to find), whereas calling a boolean function in list context seems rather simple to debug.

    So I always return something (that evaluates to) false in my boolean functions (usually 0 for false and 1 for true).
    (Yes, this would also return a one-element list in list context - so the comment in the book is technically right -, but then the actual error would be calling the boolean function in list context.)

    To be clear, I still recommend the book. It offers a lot of good advice.
    And I am referring to Chapter 9 (Subroutines), page 199/200 ("Use a bare return to return failure.") in the book "Perl Best Practices" by Damian Conway (O'Reilly Media, Inc.), ISBN 978-0-596-00173-5. Not sure if there's another/newer edition.

    Side note:
    Perl seems to return an empty string when negating something. my $foo = 5; return !$foo; returns q().

    0 讨论(0)
  • 2020-12-09 16:22

    Given

    sub foo { return; }
    sub bar { return undef; }
    

    In scalar context, they behave the same.

    my $foo = foo();   # $foo is undef
    my $bar = bar();   # $bar is undef
    

    In list context, they behave differently

    my @foo = foo();   # @foo is ()  (an empty list)
    my @bar = bar();   # @bar is ( undef )  (a one-element list)
    

    Note that a one-element list is a true value in boolean context, even though the only element is undef.

    In general, it's usually not a good idea to return undef; from a subroutine, because of how it behaves in context.

    0 讨论(0)
  • 2020-12-09 16:24

    I think I still agree with PBP though.

    1) You should avoid nesting function calls:

    my $result = bar();
    foo(1, $result, 2);
    

    2) You should always be explicit (when doing "complicated" things):

    foo(1, scalar bar(), 2);
    
    0 讨论(0)
提交回复
热议问题