/usr/share/perl5/Data/Types.pm is in libdata-types-perl 0.09-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 | package Data::Types;
use strict;
require Exporter;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
$VERSION = '0.09';
@ISA = qw(Exporter);
@EXPORT_OK = qw(is_whole to_whole is_count to_count is_int to_int is_real
to_real is_decimal to_decimal is_float to_float is_string
to_string );
@EXPORT = qw();
%EXPORT_TAGS = (
all => \@EXPORT_OK,
whole => [qw(is_whole to_whole)],
count => [qw(is_count to_count)],
int => [qw(is_int to_int)],
decimal => [qw(is_decimal to_decimal)],
real => [qw(is_real to_real)],
float => [qw(is_float to_float)],
string => [qw(is_string to_string)],
is => [qw(is_whole is_int is_real is_decimal is_float is_string)],
to => [qw(to_whole to_int to_real to_decimal to_float to_string)],
);
use constant DEF_PRECISION => 5;
################################################################################
# FUNCTIONS #
################################################################################
sub is_whole ($) {
return unless defined $_[0];
return unless $_[0] =~ /^\d+$/;
return 1;
}
sub to_whole ($) {
return unless defined $_[0];
my ($num) = $_[0] =~ /([+-]?(?:\d+(?:\.\d*)?|\.\d+))/;
return unless defined $num && $num >= 0;
sprintf "%.0f", $num;
}
sub is_count ($) {
return unless $_[0];
return unless $_[0] =~ /^\d+$/;
return 1;
}
sub to_count ($) {
return unless $_[0];
my ($num) = $_[0] =~ /([+-]?(?:\d+(?:\.\d*)?|\.\d+))/;
return unless $num && $num > .5;
sprintf "%.0f", $num;
}
sub is_int ($) {
return unless defined $_[0] && $_[0] ne '';
return unless $_[0] =~ /^[+-]?\d+$/;
return 1;
}
sub to_int ($) {
return unless defined $_[0] && $_[0] ne '';
my ($num) = $_[0] =~ /([+-]?(?:\d+(?:\.\d*)?|\.\d+))/;
return unless defined $num;
sprintf "%.0f", $num;
}
sub is_decimal ($) {
return unless defined $_[0] && $_[0] ne '';
return unless $_[0] =~ /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)$/;
return 1;
}
sub to_decimal ($;$) {
return unless defined $_[0] && $_[0] ne '';
my ($num) = $_[0] =~ /([+-]?(?:\d+(?:\.\d*)?|\.\d+))/;
return unless defined $num;
$_[1] ||= DEF_PRECISION;
sprintf "%.$_[1]f", $num;
}
#sub is_real ($) {
# return unless defined $_[0] && $_[0] ne '';
# return unless $_[0] =~ /^[+-]?\d*\.?\d*$/;
# return 1;
#}
#sub to_real ($) {
# return unless defined $_[0] && $_[0] ne '';
# sprintf "%f", $_[0] =~ /([+-]?\d*\.?\d*)/;
#}
# These may need to be separated in the future, in order to identify non-decimal
# real numbers.
*is_real = *is_decimal;
*to_real = *to_decimal;
sub is_float ($) {
return unless defined $_[0] && $_[0] ne '';
return unless $_[0] =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/;
return 1;
}
sub to_float ($;$) {
return unless defined $_[0] && $_[0] ne '';
my ($num) = $_[0] =~ /(([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?)/;
return unless defined $num;
my $type = $num =~ /e|E/ ? 'e' : 'f';
$_[1] ||= DEF_PRECISION;
sprintf "%.$_[1]$type", $num;
# sprintf "%g", $_[0] =~ /(([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?)/;
}
sub is_string ($) { defined $_[0] && ! ref $_[0] }
sub to_string ($;$) {
return unless defined $_[0];
return $_[1] ? substr("$_[0]", 0, $_[1]) : "$_[0]";
}
1;
__END__
=head1 NAME
Data::Types - Validate and convert data types.
=head1 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);
=head1 DESCRIPTION
This module exports a number of functions that are useful for validating and
converting data types. It is intended for use in applications where data types
are more important than they typically are in Perl -- e.g., database
applications.
=head1 EXPORT
No functions are exported by default, though each function may be exported
explicitly (see L<"Functions">, below, for a list of functions available for
export). The following export tags are supported:
=over 4
=item :whole
Exports is_whole() and to_whole().
=item :count
Exports is_count() and to_count().
=item :int
Exports is_int() and to_int().
=item :decimal
Exports is_decimal() and to_decimal().
=item :real
Exports is_real() and to_real().
=item :float
Exports is_float() and to_float().
=item :string
Exports is_string() and to_string().
=item :is
Exports all validation functions: is_whole(), is_int(), is_real(), is_decimal(),
is_float(), and is_string().
=item :to
Exports all conversion functions: to_whole(), to_int(), to_real(), to_decimal(),
to_float(), and to_string().
=item :all
Exports all functions.
=back
=head1 FUNCTIONS
=head2 is_whole
my $bool = is_whole($val);
Returns true if $val is a whole number (including 0), and false if it is not.
The regular expression used to test the wholeness of $val is C</^\d+$/>.
my $bool = is_whole(1); # Returns true.
$bool = is_whole(-1); # Returns false.
$bool = is_whole(0); # Returns true.
=head2 to_whole
my $whole = to_whole($val);
Converts $val to a whole number and returns it. Numbers will be rounded to the
nearest whole. If $val is a mixture of numbers and letters, to_whole() will
extract the first decimal number it finds and convert that number to a whole
number.
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.
=head2 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
C</^\d+$/>.
my $bool = is_count(1); # Returns true.
$bool = is_count(-1); # Returns false.
$bool = is_count(0); # Returns false.
=head2 to_count
my $count = to_count($val);
Converts $val to a counting number and returns it. Numbers will be rounded to
the nearest counting number. Note that since 0 (zero) is not considered a
counting number by this module, it will not be returned. 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.
=head2 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. The regular expression used to test for an
integer in $val is C</^[+-]?\d+$/>.
my $bool = is_int(0); # Returns true.
$bool = is_int(22); # Returns true.
$bool = is_int(-22); # Returns false.
$bool = is_int(3.2); # Returns false.
=head2 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.
=head2 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
C</^[+-]?(?:\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.
=head2 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.
=head2 is_real
my $bool = is_real($val);
Returns true if $val is a real number, and false if it is not.
B<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.
=head2 to_real
my $real = to_real($val);
$real = to_real($val, $precision);
Converts $val to a real number.
B<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.
=head2 is_float
my $bool = is_real($val);
Returns true if $val is a float, and false if it is not. The regular expression
used to test $val is C</^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/>.
my $bool = is_real(30); # Returns true.
$bool = is_real(1.23e99); # Returns true.
$bool = is_real('foo'); # Returns false.
=head2 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.
=head2 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.
my $bool = is_string('foo'); # Returns true.
$bool = is_string(20001); # Returns true.
$bool = is_string([]); # Returns false.
$bool = is_string(undef); # Returns false.
=head2 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'.
=head1 SUPPORT
This module is stored in an open L<GitHub
repository|http://github.com/theory/data-types/>. Feel free to fork and
contribute!
Please file bug reports via L<GitHub
Issues|http://github.com/theory/data-types/issues/> or by sending mail to
L<bug-Data-Types.cpan.org|mailto:bug-Data-Types.cpan.org>.
Patches against Class::Meta are welcome. Please send bug reports to
<bug-data-types@rt.cpan.org>.
=head1 AUTHOR
David E. Wheeler <david@justatheory.com>
=head1 SEE ALSO
L<perlfaq4|perlfaq4/"How do I determine whether a scalar is anumber/whole/integer/float?">
lists the most of the regular expressions used to identify the different numeric
types used in this module.
L<String::Checker|String::Checker> also does some data type validation.
L<String::Scanf|String::Scanf> reimplements the C C<sscanf()> function in
perl, and also does data type validation and conversion.
L<Regexp::Common|Regexp::Common> contains many useful common regular expressions
(surprise!), including some that can be used to identify data types.
Arthur Bergman's L<types|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 prgrma for fast, static data types.
=head1 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.
=cut
|