Is there a difference between a subroutine that does
return;
and one that does?
return undef;
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
return;will return an empty list in list context butundefin scalar context.return undef;will always return a single valueundefeven in list context.In general, it’s usually not a good idea to
return undef;from a subroutine normally used in list context: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: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.