in reply to force user to only enter numbers

There's Scalar::Util::looks_like_number(), or course. Is any number OK, or are you specifically wanting only 0 to 9? (I ask because your spec says "only numbers", but your sample code seems to be saying 0 to 9. An accurate idea of what you are planning is crucial to doing exactly what you want to do, instead of kind of doing it.)

If you really meant 0..9, then use

if ($number =~ /^[0-9]$/) { ...
That says "one character, which must be 0 through 9, and no others".