Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: check if its a number

by AV-2 (Novice)
on Apr 12, 2008 at 02:13 UTC ( [id://679921]=note: print w/replies, xml ) Need Help??


in reply to check if its a number

I was going to post something else, but in the process I tried this:
my $foo = 'name'; print $foo + 1;
Perl warns me "Argument "name" isn't numeric in addition (+)" How does Perl know it isn't a number? Is the method available through a function?

Replies are listed 'Best First'.
Re^2: check if its a number
by nobull (Friar) on Apr 12, 2008 at 14:23 UTC
    Yes you can write a function that tells you if a given string triggers the warning.

    sub perl_thinks_it_is_number { eval { use warnings FATAL => 'numeric'; no warnings 'void'; 0+shift; 1; } } print "'foo' isn't a number\n" unless perl_thinks_it_is_number 'foo'; print "'17' is a number\n" if perl_thinks_it_is_number '17';
    Note that this will consider '00' and some other oddities to be a number. You are probably better sticking with other answers elsewhere in this thread.
Re^2: check if its a number
by parv (Parson) on Apr 12, 2008 at 09:31 UTC
    ... warning comes up only when using, well, warnings pragma (otherwise string is used as a zero).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://679921]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-04-18 20:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found