Firstly your regexp will match anything which has a digit it in somewhere. This might not be what you want. Perhaps something like /^\s*(\d+)\s*$/ would be better, to require a sequence of digits with possible whitespace on either side. This leaves your number in $1 afterwards.
Secondly, think twice about having your input function call your input function. You could end up with a very large call stack here, killing performance and chewing memory.
I would suggest you use a loop like this:
This keeps reading from STDIN until we get a number (in which case we return it), or we run out of input. Plus there's no recusion involved, which is much better than calling ourselves repeatedly.while (defined($_ = <STDIN>)) { print "Numbers only please" and next unless (/^\s*(\d+)\s*$/); return $1; } die "urk! Out of input.\n";
It's not a single line, but it probably does what you want, and is shorter than the original code.
Cheers,
Paul
In reply to Re: Can this If/Else be condensed into a 1 liner with a trailing If?
by pjf
in thread Can this If/Else be condensed into a 1 liner with a trailing If?
by jerrygarciuh
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |