in reply to Perl function calls.

From perlsub:
If no return is found and if the last statement is an expression, its value is returned. If the last statement is a loop control structure like a foreach or a while , the returned value is unspecified.

Since you have a while loop as the last statement, the returned value is undefined.

UPDATE: Perl : Convert a monolithic code to a function