File Coverage

blib/lib/Scalar/Number.pm
Criterion Covered Total %
statement 117 123 95.1
branch 44 50 88.0
condition 25 30 83.3
subroutine 24 25 96.0
pod 5 5 100.0
total 215 233 92.2


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Scalar::Number - numeric aspects of scalars
4              
5             =head1 SYNOPSIS
6              
7             use Scalar::Number qw(scalar_num_part);
8              
9             $num = scalar_num_part($scalar);
10              
11             use Scalar::Number qw(sclnum_is_natint sclnum_is_float);
12              
13             if(sclnum_is_natint($value)) { ...
14             if(sclnum_is_float($value)) { ...
15              
16             use Scalar::Number qw(sclnum_val_cmp sclnum_id_cmp);
17              
18             @sorted_nums = sort { sclnum_val_cmp($a, $b) } @floats;
19             @sorted_nums = sort { sclnum_id_cmp($a, $b) } @floats;
20              
21             =head1 DESCRIPTION
22              
23             This module is about the numeric part of plain (string) Perl scalars.
24             A scalar has a numeric value, which may be expressed in either the
25             native integer type or the native floating point type. Many values
26             are expressible both ways, in which case the exact representation is
27             insignificant. To fully understand Perl arithmetic it is necessary to
28             know about both of these representations, and the differing behaviours
29             of numbers according to which way they are expressible.
30              
31             This module provides functions to extract the numeric part of a scalar,
32             classify a number by expressibility, and compare numbers across
33             representations.
34              
35             This module is implemented in XS, with a pure Perl backup version for
36             systems that can't handle XS.
37              
38             =cut
39              
40             package Scalar::Number;
41              
42 6     6   342792 { use 5.006; }
  6         22  
  6         255  
43 6     6   33 use warnings;
  6         12  
  6         173  
44 6     6   55 use strict;
  6         12  
  6         311  
45              
46             our $VERSION = "0.006";
47              
48 6     6   31 use parent "Exporter";
  6         10  
  6         63  
49             our @EXPORT_OK = qw(
50             scalar_num_part
51             sclnum_is_natint sclnum_is_float
52             sclnum_val_cmp sclnum_id_cmp
53             );
54              
55             eval { local $SIG{__DIE__};
56             require XSLoader;
57             XSLoader::load(__PACKAGE__, $VERSION);
58             };
59              
60             if($@ eq "") {
61             close(DATA);
62             *scalar_num_part = sub($) {
63 6     6   1166 no warnings qw(numeric uninitialized);
  6         11  
  6         869  
64 35     35   61364 return _warnable_scalar_num_part($_[0]);
65             };
66             } else {
67             local $/ = undef;
68             my $pp_code = ;
69             close(DATA);
70             {
71             local $SIG{__DIE__};
72 3 50 100 3 1 17 eval $pp_code;
  3 0 33 3 1 6  
  3 50 100 3 1 227  
  3 100 100 3 1 1154  
  3 100 100 3 1 8969  
  3 100 100 3   410  
  3 50 100 3   22  
  3 100 66 3   53  
  3 100 66 3   798  
  3 100 66 3   18  
  3 100   3   6  
  3 100   3   185  
  3 100   3   14  
  0 100   0   0  
  3 100   35   202  
  3 100   2433   5  
  3 100   720   13  
  3 100   720   423  
  3 100   2104   6  
  3 100       148  
  3 100       15  
  3 100       6  
  3 100       524  
  3 100       6  
  3 50       15  
  3         23  
  3         11  
  3         209  
  0         0  
  0         0  
  0         0  
  3         15  
  3         5  
  3         761  
  3         20  
  3         6  
  3         201  
  3         15  
  3         5  
  3         1578  
  3         19  
  3         4  
  3         906  
  0         0  
  35         37724  
  35         136  
  13         46  
  13         2912  
  12         18  
  12         42  
  12         37  
  12         856  
  2         8  
  10         63  
  32         106  
  19         121  
  2         5  
  17         21  
  17         123  
  17         42  
  17         91  
  0         0  
  19         239  
  13         48  
  2433         1286478  
  2433         13311  
  98         518  
  94         502  
  137         504  
  137         2541  
  2104         4410  
  720         6147  
  720         15129  
  14         24  
  14         191  
  166         1092  
  540         21205  
  250         666  
  212         6067  
  11         17  
  11         42  
  8         14  
  201         290  
  209         535  
  985         2319  
  898         1793  
  122         584  
  720         427472  
  720         2716  
  14         22  
  14         139  
  162         769  
  544         1898  
  2104         3247  
  2104         2312  
  2104         2548  
  2104         2823  
  2104         2731  
  2104         17853  
  148         419  
  148         489  
  1628         8581  
  208         200  
  208         183  
  208         230  
  208         1564  
  148         742  
73             }
74             die $@ if $@ ne "";
75             }
76              
77             1;
78              
79             __DATA__