Finding a scalar variable data type in Perl

I have a function that accepts input from a user. The input can be an integer, a float, or a string. I have three overloaded functions that should be called based on the DATA TYPE of the data entered. For example, if the user enters an integer (say 100), you should call a function with an integer parameter. If the user enters a string (say, "100"), you should call a function that has a string parameter.

So, I need to know the data type of the data entered. With regular expressions, I can distinguish between an integer and a float (since I just need to figure out the type, I will not use the library provided on cpan.org), but I cannot figure out how to distinguish an integer from a string. Does Perl treat "100" and 100 as the same? Is there any way around this problem?

+6
source share
4 answers

From perldoc perldata :

Scalars must be so or not. There is no place for declaring a scalar variable of type "string", enter "number", enter "link" or anything else. Due to the automatic conversion of scalars, operations that return scalars do not need to worry (and they don’t really care) whether the caller is looking for a string, number or link. Perl is a contextually polymorphic language whose scalars can be strings, numbers, or references (including objects). Although strings and numbers are considered almost the same for almost all purposes, references are strongly typed, non-transferable pointers with built-in reference counting and a call to the destructor.

So, for entire scalars, you just need to decide in advance how you want to handle them. Perl will be fun to convert from a number to a string or vice versa depending on the context.

+11
source

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; # int say guess_type "1"; # int say guess_type 1.1; # rat say guess_type 'asdf'; # str 

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.

+9
source
 #!perl6 use v6; multi guess ( Int $a ) { say "got integer: $a" } multi guess ( Str $a ) { say "got string: $a" } multi guess ( Rat $a ) { say "got float: $a" } guess(3); guess("3"); guess(3.0); 

Cheating, I know ...

Floor

+2
source

Have you considered passing a function a hash link using keys that indicate what type of data is being entered?

 my $str_input = { string => "100" }; my $int_input = { integer => 100 }; my $float_input = { float => 100.0 }; 

You can check what type you got by checking which key has an input: my $datatype = shift (keys %{$input}) and from there from there. (Note that implicit dereferencing happens with $input )

 switch ($datatype) { case string: C_string($input->{$datatype}); case integer: C_integer($input->{$datatype}); case float: C_float($input->{$datatype}); } 
+1
source

Source: https://habr.com/ru/post/890625/


All Articles