File Coverage

blib/lib/IRC/Toolkit/TS6.pm
Criterion Covered Total %
statement 29 32 90.6
branch 5 6 83.3
condition 2 2 100.0
subroutine 9 10 90.0
pod 3 4 75.0
total 48 54 88.8


line stmt bran cond sub pod time code
1             package IRC::Toolkit::TS6;
2             $IRC::Toolkit::TS6::VERSION = '0.092001';
3 1     1   17907 use strictures 2;
  1         2185  
  1         70  
4 1     1   292 use Carp;
  1         2  
  1         126  
5              
6 1     1   805 use parent 'Exporter::Tiny';
  1         460  
  1         10  
7 2     2 1 96277 sub ts6_id { __PACKAGE__->new(@_) }
8             our @EXPORT = our @EXPORT_OK = 'ts6_id';
9              
10             use overload
11 0     0   0 bool => sub { 1 },
12 50000     50000   85149 '&{}' => sub { my $s = shift; sub { $s->next } },
  50000         86861  
  50000         51979  
13 1         12 '""' => 'as_string',
14 1     1   6704 fallback => 1;
  1         1029  
15              
16             =pod
17              
18             =for Pod::Coverage new
19              
20             =cut
21              
22 2   100 2 0 28 sub new { bless [ split '', $_[1] || 'A'x6 ], $_[0] }
23              
24 50005     50005 1 32969 sub as_string { join '', @{ $_[0] } }
  50005         152271  
25              
26             sub next {
27 50002     50002 1 38946 my ($self) = @_;
28              
29 50002         39127 my $pos = @$self;
30 50002         63366 while (--$pos) {
31 51433 100       84924 if ($self->[$pos] eq 'Z') {
    100          
32 1428         1201 $self->[$pos] = 0;
33 1428         1500 return $self->as_string
34             } elsif ($self->[$pos] ne '9') {
35 48573         36452 $self->[$pos]++;
36 48573         48336 return $self->as_string
37             } else {
38 1432         2115 $self->[$pos] = 'A';
39             }
40             }
41              
42 1 50       212 croak "Ran out of IDs!" if $self->[0] eq 'Z';
43              
44 0           ++$self->[$pos];
45              
46 0           $self->as_string
47             }
48              
49             1;
50              
51             =pod
52              
53             =head1 NAME
54              
55             IRC::Toolkit::TS6 - Generate TS6 IDs
56              
57             =head1 SYNOPSIS
58              
59             use IRC::Toolkit::TS6;
60              
61             my $idgen = ts6_id();
62             my $nextid = $idgen->next;
63              
64             =head1 DESCRIPTION
65              
66             Lightweight array-type objects that can produce sequential TS6 IDs.
67              
68             =head2 ts6_id
69              
70             The exported B function will instance a new ID object. B
71             optionally takes a start-point as a string (defaults to 'AAAAAA' similar to
72             C).
73              
74             The object is overloaded in two ways; it stringifies to the current ID, and
75             calling it as a C ref is the same as calling L:
76              
77             my $idgen = ts6_id;
78             my $current = "$idgen"; # 'AAAAAA'
79             my $next = $idgen->(); # 'AAAAAB'
80              
81             =head2 as_string
82              
83             The C method explicitly stringifies the current ID.
84              
85             =head2 next
86              
87             The C will increment the current ID and return the ID as a string.
88              
89             If no more IDs are available, B will croak.
90              
91             =head1 AUTHOR
92              
93             Jon Portnoy ; conceptually derived from the relevant
94             C function.
95              
96             =cut