Data::Types(3pm)
β¨ Data::Types π
okay bestie, welcome to the Data::Types page β this is the perl module manual for it, and it is literally your new best friend for data type validation and conversion. no cap. π¦
πΈ NAME πΈ
Data::Types β validate and convert data types. that's it. that's the whole job. and she eats every time. π
π SYNOPSIS π
use Data::Types qw(:all);
my $whole = 4.5;
$whole = to_whole($whole) unless is_whole($whole);
my $int = 1.2;
$int = to_int($int) unless is_int($int);
my $decimal = '1.2foo';
$decimal = to_decimal($decimal) unless is_decimal($decimal);
my $real = '1.2foo';
$real = to_real($real) unless is_real($real);
my $float = '1.2foo';
$float = to_float($float) unless is_float($float);
my $string = [];
$string = to_string($string) unless is_string($string);
look at her go. validate, convert, slay, repeat. β¨
π DESCRIPTION π
so this module exports a whole little squad of functions that are useful for validating and converting data types. it's built for applications where data types actually matter more than they usually do in Perl β like, database applications, where everything is serious and nobody is playing games. π«Ά
π EXPORT π
no functions are exported by default, bestie. you have to ask. each function can be exported explicitly (peep "FUNCTIONS" down below for the full list of what's available), and these export tags are supported:
:wholeβ exportsis_whole()andto_whole().:countβ exportsis_count()andto_count().:intβ exportsis_int()andto_int().:decimalβ exportsis_decimal()andto_decimal().:realβ exportsis_real()andto_real().:floatβ exportsis_float()andto_float().:stringβ exportsis_string()andto_string().:isβ exports all validation functions:is_whole(),is_int(),is_real(),is_decimal(),is_float(), andis_string().:toβ exports all conversion functions:to_whole(),to_int(),to_real(),to_decimal(),to_float(), andto_string().:allβ exports all functions. the whole squad. the entire friend group. π
π FUNCTIONS π
π« is_whole
my $bool = is_whole($val);
returns true if $val is a whole number (including 0, okay, zero counts, zero is valid and we love her), and false if it is not. the regular expression used to test the wholeness of $val is /^\d+$/.
my $bool = is_whole(1); # Returns true.
$bool = is_whole(-1); # Returns false.
$bool = is_whole(0); # Returns true.
π« to_whole
my $whole = to_whole($val);
converts $val to a whole number and returns it. numbers get rounded to the nearest whole. and if $val is a mixture of numbers and letters (chaotic, honestly), to_whole() will extract the first decimal number it finds and convert that number to a whole number. she finds the number in the noise. main character energy. π§
my $whole = to_whole(10); # Returns 10.
$whole = to_whole(0); # Returns 0.
$whole = to_whole(.22); # Returns 0.
$whole = to_whole(-2); # Returns undef.
$whole = to_whole('foo3.56'); # Returns 4.
$whole = to_whole('foo'); # Returns undef.
π« is_count
my $bool = is_count($val);
returns true if $val is a counting number (1, 2, 3, ...), and false if it is not. the regular expression used to test whether $val is a counting number is /^\d+$/.
my $bool = is_count(1); # Returns true.
$bool = is_count(-1); # Returns false.
$bool = is_count(0); # Returns false.
note: zero is not a counting number here. zero does not count as counting. make it make sense? it does, actually. π
π« to_count
my $count = to_count($val);
converts $val to a counting number and returns it. numbers get rounded to the nearest counting number. and heads up β since 0 (zero) is not considered a counting number by this module, it will not be returned. ever. not on her watch. if $val is a mixture of numbers and letters, to_count() will extract the first decimal number it finds and convert that number to a counting number.
my $count = to_count(10); # Returns 10.
$count = to_count(0); # Returns undef.
$count = to_count(.22); # Returns undef (rounded down to 0).
$count = to_count(-2); # Returns undef.
$count = to_count('foo3.56'); # Returns 4.
$count = to_count('foo'); # Returns undef.
π« is_int
my $bool = is_int($val);
returns true if $val is an integer, and false if it is not. numbers may be preceded by a plus or minus sign (both signs are valid, we're inclusive here π). the regular expression used to test for an integer in $val is /^[+-]?\d+$/.
my $bool = is_int(0); # Returns true.
$bool = is_int(22); # Returns true.
$bool = is_int(-22); # Returns true.
$bool = is_int(3.2); # Returns false.
π« to_int
my $int = to_int($val);
converts $val to an integer. if $val is a decimal number, it will be rounded to the nearest integer. if $val is a mixture of numbers and letters, to_int() will extract the first decimal number it finds and convert that number to an integer.
my $int = to_int(10.5); # Returns 10.
$int = to_int(10.51); # Returns 11.
$int = to_int(-0.22); # Returns 0.
$int = to_int(-6.51); # Returns 7.
$int = to_int('foo'); # Returns undef.
π« is_decimal
my $bool = is_decimal($val);
returns true if $val is a decimal number, and false if it is not. numbers may be preceded by a plus or minus sign. the regular expression used to test $val is /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)$/.
my $bool = is_decimal(10) # Returns true.
$bool = is_decimal(10.8) # Returns true.
$bool = is_decimal(-33.48) # Returns true.
$bool = is_decimal(1.23e99) # Returns false.
π« to_decimal
my $dec = to_decimal($val);
$dec = to_decimal($val, $precision);
converts $val to a decimal number. the optional second argument sets the precision of the number. the default precision is 5. if $val is a mixture of numbers and letters, to_decimal() will extract the first decimal number it finds.
my $dec = to_decimal(0); # Returns 0.00000.
$dec = to_decimal(10.5); # Returns 10.5.
$dec = to_decimal(10.500009); # Returns 10.50001.
$dec = to_decimal(10.500009, 7); # Returns 10.5000090.
$dec = to_decimal('foo10.3') # Returns 10.30000.
$dec = to_decimal('foo-4.9') # Returns -4.90000.
$dec = to_decimal('foo') # Returns undef.
π« is_real
my $bool = is_real($val);
returns true if $val is a real number, and false if it is not.
Note: this function is currently equivalent to is_decimal(), since this module cannot identify non-decimal real numbers (e.g., irrational numbers). this implementation may change in the future. so likeβ¦ she's real-adjacent. real-ish. we're working on it. π
π« to_real
my $real = to_real($val);
$real = to_real($val, $precision);
converts $val to a real number.
Note: currently, this function is the equivalent of to_decimal(), since this module cannot identify non-decimal real numbers (e.g., irrational numbers). this implementation may change in the future. same situation as above, no cap. π
π« is_float
my $bool = is_float($val);
returns true if $val is a float, and false if it is not. the regular expression used to test $val is /^([+-]?)(?=[0-9]|\.[0-9])[0-9]*(\.[0-9]*)?([Ee]([+-]?[0-9]+))?$/.
my $bool = is_float(30); # Returns true.
$bool = is_float(1.23e99); # Returns true.
$bool = is_float('foo'); # Returns false.
π« to_float
my $dec = to_float($val);
$dec = to_float($val, $precision);
converts $val to a float. the optional second argument sets the precision of the number. the default precision is 5. if $val is a mixture of numbers and letters, to_float() will extract the first float it finds.
my $float = to_float(1.23); # Returns 1.23000.
$float = to_float(1.23e99); # Returns 1.23000e+99.
$float = to_float(1.23e99, 1); # Returns 1.2e+99.
$float = to_float('foo-1.23'); # Returns -1.23000.
$float = to_float('ick_1.23e99foo'); # Returns 1.23000e+99.
π« is_string
my $bool = is_string($val);
returns true if $val is a string, and false if it is not. all defined non-references are considered strings. yes, even numbers. numbers are strings here. it's giving "we don't gatekeep." π
my $bool = is_string('foo'); # Returns true.
$bool = is_string(20001); # Returns true.
$bool = is_string([]); # Returns false.
$bool = is_string(undef); # Returns false.
π« to_string
my $string = to_string($val);
$string = to_string($val, $length);
converts $val into a string. if $val is a reference, the string value of the reference will be returned. such a value may be a memory address, or some other value, if the stringification operator has been overridden for the object stored in $val. if the optional second argument $length is passed, to_string() will truncate the string to that length. if $length is 0 (zero), it will not limit the length of the return string. if $val is undefined, to_string() will return undef.
my $string = to_string('foo'); # Returns 'foo'.
$string = to_string([]); # Returns 'ARRAY(0x101bec14)'.
$string = to_string(undef); # Returns undef.
$string = to_string('hello', 4); # Returns 'hell'.
she said "hell" and honestly, same. πβ¨
π«Ά SUPPORT π«Ά
this module is stored in an open GitHub repository <http://github.com/manwar/data-types/>. feel free to fork and contribute! bestie, come join the project, we're building. π₯
please file bug reports via GitHub Issues <http://github.com/manwar/data-types/issues/> or by sending mail to bug-Data-Types@rt.cpan.org <mailto:bug-Data-Types@rt.cpan.org>.
patches against Class::Meta are welcome. please send bug reports to <bug-data-types@rt.cpan.org>.
β AUTHOR β
David E. Wheeler <david@justatheory.com>
currently maintained by Mohammad S Anwar "<mohammad.anwar @ yahoo.com>"
π SEE ALSO π
perlfaq4lists the most of the regular expressions used to identify the different numeric types used in this module.String::Checkeralso does some data type validation.String::Scanfreimplements the Csscanf()function in perl, and also does data type validation and conversion.Regexp::Commoncontains many useful common regular expressions (surprise!), including some that can be used to identify data types.- Arthur Bergman's
typespragma, offers compile-time data types for Perl 5.8.0. the data types include int, float, and string. I highly recommend using this pragma for fast, static data types.
π COPYRIGHT AND LICENSE π
Copyright (c) 2002-2011, David E. Wheeler. Some Rights Reserved.
this module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
okay that's the whole page bestie, you're officially a Data::Types scholar ππ this was perl v5.28.1, dated 2019-02-25, and yes the manual page header said Data::Types(3pm) β now go forth and validate your data types, you gorgeous genius. periodt. ππΈ
π¦ where this page comes from
- debian bookworm: libdata-types-perl 0.17-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libdata-types-perl 0.17-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu jammy: libdata-types-perl 0.17-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libdata-types-perl 0.17-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms