| 1 | package IO::Lines; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use IO::ScalarArray; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | # The package version, both in 1.23 style *and* usable by MakeMaker: | ||||||||||||||||||||||||||||
| 8 | 2 | our $VERSION = '2.113'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | # Inheritance: | ||||||||||||||||||||||||||||
| 11 | 2 | our @ISA = qw(IO::ScalarArray); ### also gets us new_tie :-) | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 NAME | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | IO::Lines - IO:: interface for reading/writing an array of lines | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | use IO::Lines; | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | ### See IO::ScalarArray for details | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | This class implements objects which behave just like FileHandle | ||||||||||||||||||||||||||||
| 29 | (or IO::Handle) objects, except that you may use them to write to | ||||||||||||||||||||||||||||
| 30 | (or read from) an array of lines. C<tiehandle> capable as well. | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | This is a subclass of L<IO::ScalarArray|IO::ScalarArray> | ||||||||||||||||||||||||||||
| 33 | in which the underlying | ||||||||||||||||||||||||||||
| 34 | array has its data stored in a line-oriented-format: that is, | ||||||||||||||||||||||||||||
| 35 | every element ends in a C<"\n">, with the possible exception of the | ||||||||||||||||||||||||||||
| 36 | final element. This makes C<getline()> I<much> more efficient; | ||||||||||||||||||||||||||||
| 37 | if you plan to do line-oriented reading/printing, you want this class. | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | The C<print()> method will enforce this rule, so you can print | ||||||||||||||||||||||||||||
| 40 | arbitrary data to the line-array: it will break the data at | ||||||||||||||||||||||||||||
| 41 | newlines appropriately. | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | See L<IO::ScalarArray> for full usage and warnings. | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =cut | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | #------------------------------ | ||||||||||||||||||||||||||||
| 49 | # | ||||||||||||||||||||||||||||
| 50 | # getline | ||||||||||||||||||||||||||||
| 51 | # | ||||||||||||||||||||||||||||
| 52 | # Instance method, override. | ||||||||||||||||||||||||||||
| 53 | # Return the next line, or undef on end of data. | ||||||||||||||||||||||||||||
| 54 | # Can safely be called in an array context. | ||||||||||||||||||||||||||||
| 55 | # Currently, lines are delimited by "\n". | ||||||||||||||||||||||||||||
| 56 | # | ||||||||||||||||||||||||||||
| 57 | 29 | ▶ | sub getline { | ||||||||||||||||||||||||||
|
Subroutine
getline: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 58 | 29 | my $self = shift; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 29 | ▶ | if (!defined $/) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 61 | 2 | return join( '', $self->_getlines_for_newlines ); | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | elsif ($/ eq "\n") { | ||||||||||||||||||||||||||||
| 64 | 27 | ▶ | if (!*$self->{Pos}) { ### full line... | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 26 | return *$self->{AR}[*$self->{Str}++]; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | else { ### partial line... | ||||||||||||||||||||||||||||
| 68 | 1 | my $partial = substr(*$self->{AR}[*$self->{Str}++], *$self->{Pos}); | |||||||||||||||||||||||||||
| 69 | 1 | *$self->{Pos} = 0; | |||||||||||||||||||||||||||
| 70 | 1 | return $partial; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | else { | ||||||||||||||||||||||||||||
| 74 | 0 | croak 'unsupported $/: must be "\n" or undef'; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | #------------------------------ | ||||||||||||||||||||||||||||
| 79 | # | ||||||||||||||||||||||||||||
| 80 | # getlines | ||||||||||||||||||||||||||||
| 81 | # | ||||||||||||||||||||||||||||
| 82 | # Instance method, override. | ||||||||||||||||||||||||||||
| 83 | # Return an array comprised of the remaining lines, or () on end of data. | ||||||||||||||||||||||||||||
| 84 | # Must be called in an array context. | ||||||||||||||||||||||||||||
| 85 | # Currently, lines are delimited by "\n". | ||||||||||||||||||||||||||||
| 86 | # | ||||||||||||||||||||||||||||
| 87 | 2 | ▶ | sub getlines { | ||||||||||||||||||||||||||
|
Subroutine
getlines: called CC=4 SCAR=16.4
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 89 | 2 | ▶ | wantarray or croak("can't call getlines in scalar context!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 2 | ▶ | if ((defined $/) and ($/ eq "\n")) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $/ and $/ eq "\n"
MC/DC0%0/2
defined $/ and $/ eq "\n"
defined $/
$/ eq "\n"
Truth table33%1/3
A and B
A= defined $/B= $/ eq "\n"
| |||||||||||||||||||||||||||||
| 92 | 2 | return $self->_getlines_for_newlines(@_); | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | else { ### slow but steady | ||||||||||||||||||||||||||||
| 95 | 0 | return $self->SUPER::getlines(@_); | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | #------------------------------ | ||||||||||||||||||||||||||||
| 100 | # | ||||||||||||||||||||||||||||
| 101 | # _getlines_for_newlines | ||||||||||||||||||||||||||||
| 102 | # | ||||||||||||||||||||||||||||
| 103 | # Instance method, private. | ||||||||||||||||||||||||||||
| 104 | # If $/ is newline, do fast getlines. | ||||||||||||||||||||||||||||
| 105 | # This CAN NOT invoke getline! | ||||||||||||||||||||||||||||
| 106 | # | ||||||||||||||||||||||||||||
| 107 | 4 | ▶ | sub _getlines_for_newlines { | ||||||||||||||||||||||||||
|
Subroutine
_getlines_for_newlines: called CC=2 SCAR=7.3
| |||||||||||||||||||||||||||||
| 108 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 109 | 4 | my ($rArray, $Str, $Pos) = @{*$self}{ qw( AR Str Pos ) }; | |||||||||||||||||||||||||||
| 110 | 4 | my @partial = (); | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 4 | ▶ | if ($Pos) { ### partial line... | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 0 | @partial = (substr( $rArray->[ $Str++ ], $Pos )); | |||||||||||||||||||||||||||
| 114 | 0 | *$self->{Pos} = 0; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | 4 | *$self->{Str} = scalar @$rArray; ### about to exhaust @$rArray | |||||||||||||||||||||||||||
| 117 | 4 | return (@partial, | |||||||||||||||||||||||||||
| 118 | @$rArray[ $Str .. $#$rArray ]); ### remaining full lines... | ||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | #------------------------------ | ||||||||||||||||||||||||||||
| 122 | # | ||||||||||||||||||||||||||||
| 123 | # print ARGS... | ||||||||||||||||||||||||||||
| 124 | # | ||||||||||||||||||||||||||||
| 125 | # Instance method, override. | ||||||||||||||||||||||||||||
| 126 | # Print ARGS to the underlying line array. | ||||||||||||||||||||||||||||
| 127 | # | ||||||||||||||||||||||||||||
| 128 | 5 | ▶ | sub print { | ||||||||||||||||||||||||||
|
Subroutine
print: called CC=6 SCAR=18.5
Pod
documented
| |||||||||||||||||||||||||||||
| 129 | 5 | ▶ | if (defined $\ && $\ ne "\n") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $\ and $\ ne "\n"
MC/DC0%0/2
defined $\ and $\ ne "\n"
defined $\
$\ ne "\n"
Truth table33%1/3
A and B
A= defined $\B= $\ ne "\n"
| |||||||||||||||||||||||||||||
| 130 | 0 | croak 'unsupported $\: must be "\n" or undef'; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 134 | ### print STDERR "\n[[ARRAY WAS...\n", @{*$self->{AR}}, "<<EOF>>\n"; | ||||||||||||||||||||||||||||
| 135 | 5 | ▶ | my @lines = split /^/, join('', @_); @lines or return 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | ### Did the previous print not end with a newline? | ||||||||||||||||||||||||||||
| 138 | ### If so, append first line: | ||||||||||||||||||||||||||||
| 139 | 5 | ▶ | if (@{*$self->{AR}} and (*$self->{AR}[-1] !~ /\n\Z/)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@{*$self->{"AR"};} and not *$self->{"AR"}[-1] =~ /\n\Z/
MC/DC100%2/2
@{*$self->{"AR"};} and not *$self->{"AR"}[-1] =~ /\n\Z/
@{*$self->{"AR"};}
not *$self->{"AR"}[-1] =~ /\n\Z/
Truth table100%3/3
A and not(B)
A= @{*$self->{"AR"};}B= not *$self->{"AR"}[-1] =~ /\n\Z/
| |||||||||||||||||||||||||||||
| 140 | 2 | *$self->{AR}[-1] .= shift @lines; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | 5 | push @{*$self->{AR}}, @lines; ### add the remainder | |||||||||||||||||||||||||||
| 143 | ### print STDERR "\n[[ARRAY IS NOW...\n", @{*$self->{AR}}, "<<EOF>>\n"; | ||||||||||||||||||||||||||||
| 144 | 5 | 1; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | #------------------------------ | ||||||||||||||||||||||||||||
| 148 | 2 | 1; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | __END__ | ||||||||||||||||||||||||||||