| 1 | package IO::Handle::Rewind; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use base qw(Class::Accessor); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 NAME | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | IO::Handle::Rewind - pretend to rewind filehandles | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 VERSION | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 0.06 | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =cut | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 3 | our $VERSION = '0.06'; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | IO::Handle::Rewind wraps any IO::Handle object in a soft, | ||||||||||||||||||||||||||||
| 22 | fluffy coat. | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =head1 METHODS | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | Delegates most methods to the wrapped object. | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head3 C<< IO::Handle::Rewind->new($obj) >> | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | Return an IO::Handle::Rewind object wrapping the passed-in | ||||||||||||||||||||||||||||
| 31 | IO::Handle. | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | =head3 C<< $re->rewind(@lines, $lines) >> | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | Further calls to C<< readline >>, C<< getline >>, or C<< | ||||||||||||||||||||||||||||
| 36 | getlines >> will read from the passed-in array/arrayrefs | ||||||||||||||||||||||||||||
| 37 | before actually reading further from the filehandle. | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | Despite the name, this does not seek the filehandle. | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =head3 C<< $re->getline >> | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =head3 C<< $re->getlines >> | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =head3 C<< $re->readline >> | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | See documentation for C<< rewind >>. | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | L<IO::Handle> | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | Hans Dieter Pearcey <hdp@icgroup.com> | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | Copyright (C) 2005, Hans Dieter Pearcey. | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | Available under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =cut | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 3 | __PACKAGE__->mk_accessors(qw(rewound obj)); | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 3 | ▶ | sub _delegate { | ||||||||||||||||||||||||||
|
Subroutine
_delegate: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 68 | 3 | my ($class, @meths) = @_; | |||||||||||||||||||||||||||
| 69 | 3 | for my $meth (@meths) { | |||||||||||||||||||||||||||
| 70 | 3 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 71 | 81 | ▶ | *{$class . "::" . $meth} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.6
| |||||||||||||||||||||||||||||
| 72 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 73 | 0 | return $self->obj->$meth(@_); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | 81 | } | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | # XXX I'm not sure all of these make sense to delegate | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 3 | my @meths = qw(fdopen close opened fileno getc eof print | |||||||||||||||||||||||||||
| 81 | printf truncate read sysread write syswrite | ||||||||||||||||||||||||||||
| 82 | stat autoflush input_line_number | ||||||||||||||||||||||||||||
| 83 | format_page_number format_lines_per_page | ||||||||||||||||||||||||||||
| 84 | format_lines_left format_name format_top_name | ||||||||||||||||||||||||||||
| 85 | formline format_write fcntl ioctl constant | ||||||||||||||||||||||||||||
| 86 | printflush); | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 3 | __PACKAGE__->_delegate(@meths); | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 91 | 1 | my ($class, $obj, $opt) = @_; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 1 | ▶ | $obj->isa('IO::Handle') or croak "Can't wrap non-IO::Handle object: $obj"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 1 | my $self = bless {} => $class; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 1 | $self->obj($obj); | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 1 | return $self; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 2 | ▶ | sub rewind { | ||||||||||||||||||||||||||
|
Subroutine
rewind: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 103 | 2 | my ($self, @lines) = @_; | |||||||||||||||||||||||||||
| 104 | 2 | $self->rewound([@lines]); | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 2 | ▶ | sub getline { | ||||||||||||||||||||||||||
|
Subroutine
getline: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 108 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 109 | 2 | return scalar $self->readline(@_); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 1 | ▶ | sub getlines { | ||||||||||||||||||||||||||
|
Subroutine
getlines: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 113 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 114 | 1 | ▶ | croak "Don't call getlines in scalar context" unless wantarray; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 1 | my @lines; | |||||||||||||||||||||||||||
| 116 | 1 | ▶ | while (defined(my $line = $self->readline)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 3 | push @lines, $line | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | 1 | return @lines; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 7 | ▶ | sub readline { | ||||||||||||||||||||||||||
|
Subroutine
readline: called CC=8 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 123 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 124 | 7 | my $re = $self->rewound; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 7 | while (1) { | |||||||||||||||||||||||||||
| 127 | # simple case -- no rewound entries | ||||||||||||||||||||||||||||
| 128 | 8 | ▶ | if (not $re or not @$re) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$re and @$re
MC/DC50%1/2
$re and @$re
$re
@$re
Truth table66%2/3
A and B
A= $reB= @$re
| |||||||||||||||||||||||||||||
| 129 | #warn "real readline\n"; | ||||||||||||||||||||||||||||
| 130 | 3 | return $self->obj->getline | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | # >>> past here, @$re is non-empty | ||||||||||||||||||||||||||||
| 134 | 5 | my $next = $re->[0]; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | # simple case -- next rewound entry is plain scalar | ||||||||||||||||||||||||||||
| 137 | 5 | ▶ | if (not ref($next)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | #warn "plain scalar: $next\n"; | ||||||||||||||||||||||||||||
| 139 | 1 | return shift @$re; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | # simple case -- next rewound entry is something we don't know | ||||||||||||||||||||||||||||
| 143 | 4 | ▶ | if (ref($next) ne "ARRAY") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 0 | die "can't handle rewind entry $next"; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | # >>> complex case -- next rewound entry is arrayref | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | # simple subcase -- it's empty | ||||||||||||||||||||||||||||
| 150 | 4 | ▶ | if (not @$next) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | #warn "ditching empty inner array\n"; | ||||||||||||||||||||||||||||
| 152 | 1 | shift @$re; | |||||||||||||||||||||||||||
| 153 | 1 | next; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | # simple subcase -- its entry is plain scalar | ||||||||||||||||||||||||||||
| 157 | 3 | ▶ | if (not ref($next->[0])) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | #warn "inner scalar: $next->[0]\n"; | ||||||||||||||||||||||||||||
| 159 | 3 | return shift @$next; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | # simple subcase -- its next entry is something we don't know | ||||||||||||||||||||||||||||
| 163 | 0 | ▶ | if (ref($next->[0])) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 0 | die "can't handle nested rewind entry $next"; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 3 | "false"; | |||||||||||||||||||||||||||