File Coverage

blib/lib/Number/Base/DWIM.pm
Criterion Covered Total %
statement 19 19 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 26 26 100.0


line stmt bran cond sub pod time code
1             package Number::Base::DWIM;
2              
3 1     1   21093 use strict;
  1         2  
  1         31  
4 1     1   4 use warnings;
  1         2  
  1         23  
5              
6 1     1   20 use 5.006002;
  1         8  
  1         33  
7              
8 1     1   1490 use overload;
  1         1041  
  1         6  
9 1     1   208 use Scalar::Util qw(dualvar);
  1         2  
  1         189  
10              
11             our $VERSION = 0.04;
12              
13             =head1 NAME
14              
15             Number::Base::DWIM - delay parsing of based constants as long as possible.
16              
17             =head1 SYNOPSIS
18              
19             use Numbers::Base::DWIM
20              
21             my $x = 011;
22             print $x, "\n"; # prints 9
23             print "$x\n"; # prints 011
24              
25             print oct($x) # prints 011
26              
27             =head1 DESCRIPTION
28              
29             This module will delay parsing of based numeric constants (0b010101,
30             0655, 0xff) until the last possible moment. This means that if you
31             use the constant as a string, then it will evaluate to the same form
32             that the constant was declared in.
33              
34             This module was developed after an discussion where some people found
35             the behavior of C to be confusing.
36             This module works around this by overloading the parsing of binary,
37             hexidecimal and octal numeric constants. It then stores them in a
38             C, as provided by L.
39              
40             =head1 NOTES
41              
42             Originally this was implemented as a class, and the overload function
43             returned an object with numification and stringification methods.
44             Thanks to Brian D. Foy for suggesting that it use C instead.
45              
46             =head1 BUGS
47              
48             Due to a bug in L, constants inside of and C
49             won't be handled specially.
50              
51             =head1 AUTHOR
52              
53             Clayton OENeill
54              
55             =head1 COPYRIGHT
56              
57             Copyright (c) 2006 by Clayton OENeill
58              
59             This program is free software; you can redistribute it and/or modify
60             it under the same terms as Perl itself.
61              
62             =cut
63              
64             sub import {
65 2     2   318 my $self = shift;
66 2     1   12 overload::constant binary => sub { dualvar(oct $_[0], $_[0]) };
  1         1700  
67            
68 2         53 return;
69             }
70              
71             1;