File Coverage

blib/lib/Dunce/time.pm
Criterion Covered Total %
statement 44 44 100.0
branch 5 6 83.3
condition 3 5 60.0
subroutine 14 14 100.0
pod 0 4 0.0
total 66 73 90.4


line stmt bran cond sub pod time code
1             package Dunce::time;
2              
3 4     4   35027 use strict;
  4         10  
  4         183  
4 4     4   39 use vars qw($VERSION);
  4         9  
  4         408  
5             $VERSION = '0.02';
6              
7 4         43 use overload '""' => \&timize,
8             '0+' => \&timize,
9             'fallback' => 'TRUE',
10             'cmp' => \&str_compare,
11             '<=>' => \&num_compare,
12 4     4   7516 ;
  4         4794  
13              
14             sub import {
15 4     4   27 my($class, $reaction) = @_;
16 4         8 my $caller = caller;
17             {
18 4     4   438 no strict 'refs';
  4         8  
  4         1829  
  4         7  
19 4         6998 *{$caller.'::time'} = sub {
20 8     8   76 return Dunce::time->new($reaction);
21 4         16 };
22             }
23             }
24              
25             sub new {
26 8     8 0 17 my($proto, $reaction) = @_;
27 8   100     37 $reaction ||= ':DIE';
28 8   33     44 my $class = ref $proto || $proto;
29 8         51 bless {
30             _time => time,
31             _callback => $class->_get_callback($reaction),
32             }, $class;
33             }
34              
35             sub _get_callback {
36 8     8   15 my($class, $reaction) = @_;
37 8         13 my $dying_msg = "Possible misuse of time().";
38 8         19 for ($reaction) {
39             /^:WARN/i && return sub {
40 1     1   8 require Carp;
41 1         196 Carp::carp $dying_msg;
42 8 100       45 };
43             /^:FIX/i && return sub {
44 1     1   2 my($this, $that) = @_;
45 1         9 require Carp;
46 1         233 Carp::carp $dying_msg, " I'll fix it.";
47 1         82 return $this <=> $that; # goes to num_compare()
48 6 100       45 };
49             /^:DIE/i && return sub {
50 1     1   8 require Carp;
51 1         309 Carp::croak $dying_msg;
52 4 50       45 };
53             }
54             }
55            
56             sub timize {
57 25     25 0 489 shift->{_time};
58             }
59            
60             sub str_compare {
61 3     3 0 157 my($this, $that) = @_;
62 3         9 my $mine = (grep { ref($this) } ($this, $that))[0];
  6         17  
63 3         12 $mine->{_callback}->($this, $that);
64             }
65              
66             sub num_compare {
67 6     6 0 21 my($this, $that) = map { $_ + 0 } @_; # numize
  18         39  
68 6         30 return $this <=> $that;
69             }
70              
71              
72             1;
73            
74             __END__