File Coverage

blib/lib/Sort/MergeSort/Iterator.pm
Criterion Covered Total %
statement 61 94 64.8
branch 13 18 72.2
condition 5 6 83.3
subroutine 14 37 37.8
pod 7 20 35.0
total 100 175 57.1


line stmt bran cond sub pod time code
1             package Sort::MergeSort::Iterator;
2              
3             # $Id: Iterator.pm 13859 2009-07-24 08:02:37Z david $
4              
5 2     2   35523 use strict;
  2         6  
  2         225  
6 2     2   13 use warnings;
  2         4  
  2         58  
7 2     2   9 use Carp;
  2         9  
  2         181  
8 2     2   11 use Carp qw(confess);
  2         4  
  2         109  
9             our $VERSION = '0.01';
10 2     2   1932 use Symbol;
  2         8732  
  2         2296  
11              
12             ##############################################################################
13             # Constructor.
14             ##############################################################################
15              
16             sub new {
17 164     164 1 346273 my ( $class, $code, $destroy ) = @_;
18 164 100       680 croak qq{Argument "$code" is not a code reference} if ref $code ne 'CODE';
19 163 50 66     367 croak qq{Argument "$destroy" is not a code reference}
20             if $destroy && ref $destroy ne 'CODE';
21              
22 163         432 my $self = bless gensym, $class;
23 163         1784 ${*$self}{code} = $code;
  163         515  
24 163         222 ${*$self}{destroy} = $destroy;
  163         318  
25 163         205 ${*$self}{count} = 0;
  163         306  
26              
27 163         859 tie(*$self, $class, $self);
28              
29 163         637 return $self;
30             }
31              
32             DESTROY {
33 163     163   146779 my $self = shift;
34 163 100       147 my $dest = ${*$self}{destroy} or return;
  163         21156  
35 1         4 $dest->();
36             }
37              
38             ##############################################################################
39             # Instance Methods.
40             ##############################################################################
41              
42             sub next {
43 89074     89074 1 252331 my $self = shift;
44 89074         259876 ${*$self}{curr} = exists ${*$self}{peek}
  89074         221157  
  17         67  
45 89057         217744 ? delete ${*$self}{peek}
46 89074 100       81960 : ${*$self}{code}->();
47 89074 100       143611 ${*$self}{count}++ if defined ${*$self}{curr};
  88909         154288  
  89074         232665  
48 89074         111699 return ${*$self}{curr};
  89074         267091  
49             }
50              
51             ##############################################################################
52              
53             sub current {
54 2     2 1 5 my $self = shift;
55 2         3 ${*$self}{curr};
  2         12  
56             }
57              
58             ##############################################################################
59              
60             sub peek {
61 18     18 1 503 my $self = shift;
62 18   100     15 return ${*$self}{peek} ||= ${*$self}{code}->();
  18         70  
  17         49  
63             }
64              
65             ##############################################################################
66              
67             sub position {
68 4     4 1 1141 my $self = shift;
69 4         5 ${*$self}{count};
  4         27  
70             }
71              
72             ##############################################################################
73              
74             sub all {
75 2     2 1 5 my $self = shift;
76 2         2 my @items;
77 2         6 push @items, $self->next while $self->peek;
78 2 100       27 return wantarray ? @items : \@items;
79             }
80              
81             ##############################################################################
82              
83             sub do {
84 3     3 1 7 my ( $self, $code ) = @_;
85 3         6 while ( local $_ = $self->next ) {
86 16 100       39 return unless $code->($_);
87             }
88             }
89              
90             ##############################################################################
91              
92             sub close
93             {
94 0     0 0 0 my $self = shift;
95 0 0       0 my $dest = ${*$self}{destroy} or return;
  0         0  
96 0         0 $dest->();
97 0         0 delete ${*$self}{destroy};
  0         0  
98 0     0   0 ${*self}{code} = sub { die "attempt to read a closed iterator" };
  0         0  
  0         0  
99             }
100              
101             *CLOSE = \&close;
102              
103 0     0 0 0 sub sysread { die "not implemented" }
104 0     0 0 0 sub open { die "not implemented" }
105 0     0 0 0 sub syswrite { die "not implemented" }
106 0     0 0 0 sub ungetc { die "not implemented" }
107 0     0 0 0 sub getc { die "not implemented" }
108 0     0 0 0 sub ungetline { die "not implemented" }
109 0     0 0 0 sub xungetc { die "not implemented" }
110 0     0 0 0 sub print { die "not implemented" }
111 0     0 0 0 sub fileno { die "not implemented" }
112 0     0   0 sub PRINTF { die "not implemented" }
113 0     0   0 sub PRINT { die "not implemented" }
114 0     0   0 sub READ { confess "not implemented" }
115 0     0 0 0 sub read { die "not implemented" }
116 0     0   0 sub WRITE { die "not implemented" }
117 0     0   0 sub TELL { die "not implemented" }
118 0     0   0 sub SEEK { die "not implemented" }
119 0     0   0 sub OPEN { die "not implemented" }
120 0     0   0 sub GETC { die "not implemented" }
121 0     0   0 sub BINMODE { }
122              
123             sub eof
124             {
125 0     0 0 0 my $self = shift;
126 0         0 return ! defined $self->peek;
127             }
128              
129             *EOF = \&eof;
130              
131             sub getlines
132             {
133 0     0 0 0 my $self = shift;
134 0 0       0 die unless wantarray;
135 0         0 return ($self->all());
136             }
137              
138             sub TIEHANDLE
139             {
140 163     163   251 my ($class, $self) = @_;
141 163         379 return $self;
142             }
143              
144             *READLINE = \&next;
145              
146             1;
147             __END__