Data::Types(3pm)

πŸ“¦
version 0.17 Β· 4 versions
, in debian bookworm, debian trixie, ubuntu jammy, ubuntu noble

✨ 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 β€” exports is_whole() and to_whole().
  • :count β€” exports is_count() and to_count().
  • :int β€” exports is_int() and to_int().
  • :decimal β€” exports is_decimal() and to_decimal().
  • :real β€” exports is_real() and to_real().
  • :float β€” exports is_float() and to_float().
  • :string β€” exports is_string() and to_string().
  • :is β€” exports all validation functions: is_whole(), is_int(), is_real(), is_decimal(), is_float(), and is_string().
  • :to β€” exports all conversion functions: to_whole(), to_int(), to_real(), to_decimal(), to_float(), and to_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 πŸ’…

  • perlfaq4 lists the most of the regular expressions used to identify the different numeric types used in this module.
  • String::Checker also does some data type validation.
  • String::Scanf reimplements the C sscanf() function in perl, and also does data type validation and conversion.
  • Regexp::Common contains many useful common regular expressions (surprise!), including some that can be used to identify data types.
  • Arthur Bergman's types pragma, 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 (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