but something fast. above implementation is very slow.$ perl -MB -E 'my $x = 1; say B::class(B::svref_2object(\$x)) eq "IV" +? "yes" : "no" ' yes $ perl -MB -E 'my $x = "1"; say B::class(B::svref_2object(\$x)) eq "IV +" ? "yes" : "no" ' no
Another possible way is to just use looks_like_number, because anything that looks_like_number is plain ASCII and should not be encoded/decoded. But I don't want to skip plain ASCII strings with numbers, with UTF-8 flag on, as several modules, that we use are affected by The Unicode Bug. So I prefer find solution like B::svref_2objectuse strict; use warnings; use Benchmark qw/:all/; use B; use Scalar::Util qw/looks_like_number/; cmpthese(-1, { iv => sub { my $r; for my $var (1...1000) { $r = B::class(B::svref_2o +bject(\$var)) eq 'IV' } }, ref => sub { my $r; for my $var (1...1000) { $r = ref($var) eq '' } + }, lln => sub { my $r; for my $var (1...1000) { $r = Scalar::Util::looks +_like_number($var) } }, }); __END__ iv 806/s -- -93% -95% lln 12047/s 1394% -- -18% ref 14768/s 1732% 23% -- __END__ Rate iv lln ref iv 799/s -- -93% -95% lln 11821/s 1379% -- -20% ref 14768/s 1748% 25% --
In reply to Fast way to distinc number vs string from Perl level by vsespb
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |