Try the following code:
my $silly = '12345';
my @is_valid = Medical::NHSNumber::is_valid($silly);
if (@is_valid) {
print "$silly is valid!\n";
}
It reports that the number is valid. This is because the is_valid
function, when called in list context, returns a list containing one
item (undef). Any list containing one item evaluates to true, not false.
The code above is quite contrived, but there are situations when you
can induce list context without really meaning to.
The solution is quite simple: rather than returning undef, just return
nothing. That is, replace things like this:
return undef
unless ( scalar(@digits) == 10 );
with things like this:
return
unless ( scalar(@digits) == 10 );
A bare "return" like that will always be treated as false whether
called in scalar context or list context.