Perl makes no useful distinction between numbers and string representations of these numbers. Your script should not either. You can write some code to distinguish between things that look like integers and floats, but the only way to find out if this is a string is that the scalar is not like an integer or a float.
Here is a simple procedure that will return int , rat or str for its argument. Note that 100 and '100' are equal to int , but something like 'asdf' will be str .
use Scalar::Util 'looks_like_number'; sub guess_type { looks_like_number($_[0]) ? $_[0] =~ /\D/ ? 'rat' : 'int' : 'str' } say guess_type 1;
Since you are working on converting Perl variables to C functions, you can write something like this:
sub myfunction { if (looks_like_number($_[0]) { if ($_[0] =~ /\D/) {C_float($_[0])} else { C_int($_[0])} } else {C_string($_[0])} }
What should "do right" when specifying a Perl scanner. You can also add to the check to check if the argument is a link, and then handle this case differently.
source share