Perl Scripts ·
How to Check if a Perl Variable Is a Number
By Manu Mathew, Technical Lead Engineer · Published 12 December 2014 · Last updated 5 October 2026

Form fields, CSV columns and configuration values all arrive as strings, so sooner or later you need to check if a Perl variable is a number before doing arithmetic with it.
Quick answer
Use looks_like_number from Scalar::Util. It ships with Perl and returns true when Perl itself would treat the value as a number, without a “non-numeric” warning. It accepts integers, decimals and exponents such as 1e3. When you need a positive whole number only, check with /\A[0-9]+\z/ instead.
use strict;
use warnings;
use Scalar::Util qw(looks_like_number);
for my $value ('42', '-7', '3.14', '1.3e8', 'inf', 'nan', '0x1A', '12abc', '', ' 5', '5 ', undef) {
my $label = defined $value ? "'$value'" : 'undef';
printf "%-8s %s\n", $label, looks_like_number($value) ? 'number' : 'not a number';
}
Output on Perl 5.40:
'42' number
'-7' number
'3.14' number
'1.3e8' number
'inf' number
'nan' number
'0x1A' not a number
'12abc' not a number
'' not a number
' 5' number
'5 ' number
undef not a number
Which results might surprise you?
inf,infinityandnancount as numbers, because Perl can do arithmetic with them.- Hex strings such as
0x1Aare not numbers. Perl only reads hex in source code or throughhex()/oct(). - Leading and trailing spaces are allowed, so
" 5"passes. - The empty string and
undefare not numbers.
What if I need a whole number only?
When the input is something like a quantity or an ID, a stricter regular expression is often what you actually want:
my $quantity = '12';
if ($quantity =~ /\A[0-9]+\z/) {
print "positive whole number\n";
}
\A and \z anchor the whole string, so trailing newlines and spaces are rejected too. Use [0-9] rather than \d, because \d also matches digits from other scripts.
Frequently asked questions
Does looks_like_number accept numbers with commas, such as 1,000?
No. A comma isn’t part of Perl’s number syntax, so '1,000' is not a number. Remove the separators first, for example with tr/,//d, then check the cleaned value with looks_like_number.
How do I check for a decimal with at most two places?
Use a pattern such as /\A[0-9]+(?:\.[0-9]{1,2})?\z/. It accepts 12, 12.5 and 12.50 but rejects 12.505, .5 and 1e3, which suits amounts and quantities that people type into a web form.
Why does Perl warn “Argument isn’t numeric”?
The value was used in a numeric operation, such as + or ==, but doesn’t look like a number. Perl still converts it, using any leading digits or 0, which can hide a bug. Check the input with looks_like_number or a pattern before doing arithmetic.
Is ‘0 but true’ a number?
Yes. Perl treats the string '0 but true' as 0 without a warning, and looks_like_number returns true for it. Some older interfaces return it to mean “it worked, but the answer is zero”, so you may meet it in older code.
Related notes: Perl strings and regex; Perl arrays: length, add, remove and search; why Perl::Critic flags conditional declarations; hiring a remote Perl developer.
Need help with Perl code?
If you have Perl code that needs fixing, extending or upgrading, I’m happy to take a look. I work remotely from Kozhikode, Kerala, with teams across India and elsewhere; Perl development and maintenance explains how I help. If you are deciding how to bring someone in, Hiring a remote Perl developer is a short checklist.
You can reach me through the contact section or email me at manu@manu.co.in. A few lines about the system and what you need is a good start.
An earlier version of this note reproduced a Stack Overflow answer (answer 28589, CC BY-SA). This version is rewritten in my own words, with new examples.
Sources: Scalar::Util documentation; Stack Overflow question 12647.