Scalar::Type(3pm)
π Scalar::Type π
okay bestie, gather round, because today we're talking about Scalar::Type β a lil Perl module whose whole entire job is figuring out what type a scalar actually is. iconic behaviour honestly. it's part of the User Contributed Perl Documentation and it's here to help you vibe-check your variables. π¦
β¨ NAME β¨
Scalar::Type β figure out what type a scalar is πΈ
β¨ SYNOPSIS β¨
use Scalar::Type qw(is_number);
if(is_number(2)) {
# yep, 2 is a number
# it is_integer too
}
if(is_number("2")) {
# no, "2" is a string
}
β¨ OVERVIEW β¨
so like. Perl scalars can be either strings or numbers, and normally you do NOT care which is which, because Perl does all the type conversions automagically for you. you can just do numeric stuff on strings and, as long as they look like a number, you get a sensible answer. slay:
my $string = "4";
my $number = 1;
my $result = $string + $number; # 5
but β and this is the plot twist β in some rare cases, generally when you're serialising data, the difference matters. this package hands you some very useful functions to help you figure out what's what. here's everything that's available. none of them are exported by default, btw, no cap. if you want all of them, export ':all':
use Scalar::Type qw(:all);
and if you only want the is_* functions, you can grab 'em all in one go:
use Scalar::Type qw(is_*);
For Reasons β¨, ":is_*" is equivalent.
β¨ FUNCTIONS β¨
all of these functions require an argument. calling them without one is a fatal error. no argument, no vibes. π
β¨ type β¨
returns the type of its argument.
- if the argument is a reference, it returns either
blessed($argument)(if it's an object) or'REF_TO_'.ref($argument). - if the argument is
undef, it returns'UNDEF'. - if you're on perl 5.35.7 or later and the argument is the result of a comparison, it returns
'BOOL'. - otherwise it goes looking for the
IOKorNOKflags on the underlying SV (peep "GORY DETAILS" for the exact mechanics, bestie) and returns"INTEGER"or"NUMBER"as appropriate. and finally, if neither of those is set, it returns"SCALAR".
β¨ bool_supported β¨
returns true if the 'BOOL' type is supported on this perl (aka if your perl version is 5.35.7 or later) and false otherwise.
β¨ sizeof β¨
returns the size, in bytes, of the underlying storage for numeric types, and die()s for any other type. dramatic but fair. π
β¨ is_integer β¨
returns true if its argument is an integer. note that "1" is not an integer, it's a string. 1 is an integer. 1.1 is obviously not an integer. 1.0 is also not an integer, because it makes a different statement about precision β 1 is exactly one, but 1.0 is only one to two significant figures. the way that math works is genuinely so real of it.
all integers are of course also numbers.
β¨ is_number β¨
returns true if its argument is a number. "1" is not a number, it's a string. 1 is a number. 1.0 and 1.1 are numbers too.
β¨ is_bool β¨
this one is not available on perl versions earlier than 5.35.7. calling it, or even just trying to import it, on older perls is a fatal error. β
returns true if its argument is a Boolean β ie, the result of a comparison.
β¨ GORY DETAILS β¨
β¨ PERL VARIABLE INTERNALS β¨
as far as Perl code is concerned, scalars will present themselves as integers, floats or strings on demand. internally though, scalars live in a C structure called an SV (scalar value), and that thing has several slots. the ones we care about are:
IVβ an integer valueUVβ an unsigned integer value, only used for ints > MAXINT / 2.NVβ a numeric value (ie a float)PVβ a pointer value (ie a string)
when a value gets created, one of those slots gets filled. as you do various operations on a value, the slot's contents may change, and other slots may get filled too. lowkey chaotic honestly.
for example:
my $foo = "4"; # fill $foo's PV slot, as "4" is a string
my $bar = $foo + 1; # fill $bar's IV slot, as 4 + 1 is an int,
# and fill $foo's IV slot, as we had to figure
# out the numeric value of the string
$foo = "lemon"; # fill $foo's PV slot, as "lemon" is a string
that last operation immediately shows a problem, bestie. $foo's IV slot was filled with the integer value 4, but assigning the string "lemon" only filled the PV slot. so... what's sitting in the IV slot?? there's a handy tool for that β Devel::Peek, which ships with perl. here's part of Devel::Peek's output:
$ perl -MDevel::Peek -E 'my $foo = 4; $foo = "lemon"; Dump($foo);'
IV = 4
PV = 0x7fe6e6c04c90 "lemon"\0
so how does perl know that even though there's a value in the IV slot, it shouldn't be used? because once you've assigned "lemon" to the variable, you literally cannot get that 4 to show itself ever again, at least not from pure perl code. it's giving ghosted.
the SV also has a flags field, which I skipped over above. (I've also skipped over some of the flags here β I'm only showing you the relevant ones):
$ perl -MDevel::Peek -E 'my $foo = 4; $foo = "lemon"; Dump($foo);'
FLAGS = (POK)
IV = 4
PV = 0x7fe6e6c04c90 "lemon"\0
the "POK" flag means, as you might have guessed, that the "PV" slot has valid contents β and in case you're wondering, the "PV" slot there contains a pointer to the memory address 0x7fe6e6c04c90, at which can be found the word "lemon". iconic memory address.
it's possible to have multiple flags set. that's exactly what's happening in the second line of code in the example. in that example a variable contains the string "4", so the "PV" slot is filled and the "POK" flag is set. we then take the value of that variable, add 1, and assign the result to another variable. obviously adding 1 to a string is meaningless, so the string has to first be converted to a number. that fills the "IV" slot:
$ perl -MDevel::Peek -E 'my $foo = "4"; my $bar = $foo + 1; Dump($foo);'
FLAGS = (IOK,POK)
IV = 4
PV = 0x7fd6e7d05210 "4"\0
notice there are now two flags. "IOK" means the "IV" slot's contents are valid, and "POK" means the "PV" slot's contents are valid. why do we need both slots in this case? because a non-numeric string such as "lemon" is treated as the integer 0 if you perform numeric operations on it. πβ‘οΈ0οΈβ£
all that I said above about "IV"s also applies to "NV"s, and you will sometimes come across a variable with both the "IV" and "NV" slots filled, or even all three:
$ perl -MDevel::Peek -E 'my $foo = 1e2; my $bar = $foo + 0; $bar = $foo . ""; Dump($foo)'
FLAGS = (IOK,NOK,POK)
IV = 100
NV = 100
PV = 0x7f9ee9d12790 "100"\0
finally, it's possible to have multiple flags set even though the slots contain what looks (to a human) like different values:
$ perl -MDevel::Peek -E 'my $foo = "007"; $foo + 0; Dump($foo)'
FLAGS = (IOK,POK)
IV = 7
PV = 0x7fcf425046c0 "007"\0
that code initialises the variable to the string "007", then uses it in a numeric operation. that causes the string to be numified, the "IV" slot to be filled, and the "IOK" flag to be set. it should, of course, be clear to any fan of classic literature that "007" and 7 are very different things. "007" is not an integer. π
β¨ Booleans β¨
in perl 5.35.7 and later, Boolean values β ie the results of comparisons β have some extra magic. as well as their value, which is either 1 (true, an integer) or '' (false, an empty string), they have a flag to indicate their Booleanness. this is exposed via the "builtin::is_bool" perl function, so we don't need to do any XS voodoo to interrogate it. π§β¨
β¨ WHAT Scalar::Type DOES (at least in version 1.0.0) β¨
NB that this section documents an internal function that is not intended for public use. the interface of "_scalar_type" should be considered unstable, not fit for human consumption, and subject to change without notice. this documentation is correct as of version 1.0.0 but may not be updated for future versions β its purpose is pedagogical only. you have been warned, bestie. β οΈ
the "is_*" functions are just wrappers around the "type" function. that in turn delegates most of the work to a few lines of C code which grovel around looking at the contents of the individual slots and flags. that function isn't exported, but if you really want to call it directly, it's called "_scalar_type" and will return one of three strings: "INTEGER", "NUMBER", or "SCALAR". it will return "SCALAR" even for a reference or undef, which is why I said the "type" function only mostly wraps around it :-)
the first thing "_scalar_type" does is look at the "IOK" flag. if it's set, and the "POK" flag is not set, then it returns "INTEGER". if "IOK" and "POK" are set, it stringifies the contents of the "IV" slot, compares to the contents of the "PV" slot, and returns "INTEGER" if they are the same, or "SCALAR" otherwise.
the reason for jumping through those hoops is so we can correctly divine the type of "007" in the last example above. π΅οΈβοΈ
if "IOK" isn't set, we then look at "NOK". that follows exactly the same logic, looking also at "POK", and returning either "NUMBER" or "SCALAR", being careful about strings like "007.5".
if neither "IOK" nor "NOK" is set, then we return "SCALAR".
and what about "UV"s? they are treated exactly the same as "IV"s, and a variable with a valid "UV" slot will have the "IOK" flag set. it will also have the "IsUV" flag set, which we use to determine how to stringify the number. π
β¨ SEE ALSO β¨
Test2::Tools::Type, which is bundled with this module.Scalar::Utilβ in particular its"blessed"function.builtinβ if you have perl 5.35.7 or later.
β¨ BUGS β¨
if you find any bugs, please report them on Github, preferably with a test case. π
integers that are specifed using exponential notation, such as if you say 1e2 instead of 100, are not internally treated as integers. the perl parser is lazy and only bothers to convert them into an integer after you perform int-ish operations on them, such as adding 0. likewise, if you add 0 to the thoroughly non-numeric "100", perl will convert it to an integer. these edge cases are partly why you almost certainly don't care about what this module does. if they irk you, complain to p5p. π€
β¨ FEEDBACK β¨
I welcome feedback about my code, especially constructive criticism. π«Ά
β¨ AUTHOR, COPYRIGHT and LICENCE β¨
Copyright 2024 David Cantrell <david@cantrell.org.uk>
this software is free-as-in-speech software, and may be used, distributed, and modified under the terms of either the GNU General Public Licence version 2 or the Artistic Licence. it's up to you which one you use. the full text of the licences can be found in the files GPL2.txt and ARTISTIC.txt, respectively. π
β¨ CONSPIRACY β¨
this module is also free-as-in-mason software. π§±π
and that's the whole page bestie, no notes, we ate. π β¨ this absolute banger of a doc shipped with perl v5.42.3 and is dated 2026-08-20 β k byeeee πππ¦
π¦ where this page comes from
- ubuntu stonking: libscalar-type-perl 1.0.1-1build2 Β· Artistic-1.0-Perl OR GPL-2.0-only
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms