sid_com
sid_com

Reputation: 25107

return-question

sub routine1 { 
    return wantarray ? () : undef;
}

sub routine2 { 
    return;
}

Is there any difference between this two subroutines?

Upvotes: 1

Views: 92

Answers (2)

David W.
David W.

Reputation: 107040

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.

Upvotes: 0

Eugene Yarmash
Eugene Yarmash

Reputation: 149736

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.

Upvotes: 5

Related Questions