开发者

return-question

开发者_Python百科sub routine1 { 
    return wantarray ? () : undef;
}

sub routine2 { 
    return;
}

Is there any difference between this two subroutines?


No.

From perldoc -f return:

If no EXPR is given, returns an empty list in list context, the undefined value in scalar context, and (of course) nothing at all in a void context.


Actually, there is a difference...

Take a look at this link from Perl Critic.

Returning undef upon failure from a subroutine is pretty common. But if the subroutine is called in list context, an explicit return undef; statement will return a one-element list containing (undef). Now if that list is subsequently put in a boolean context to test for failure, then it evaluates to true. But you probably wanted it to be false.

It's subtle, but could be an issue.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜