6 IO::Lines - IO:: interface for reading/writing an array of lines
13 ### See IO::ScalarArray for details
18 This class implements objects which behave just like FileHandle
19 (or IO::Handle) objects, except that you may use them to write to
20 (or read from) an array of lines. They can be tiehandle'd as well.
22 This is a subclass of L<IO::ScalarArray|IO::ScalarArray>
23 in which the underlying
24 array has its data stored in a line-oriented-format: that is,
25 every element ends in a C<"\n">, with the possible exception of the
26 final element. This makes C<getline()> I<much> more efficient;
27 if you plan to do line-oriented reading/printing, you want this class.
29 The C<print()> method will enforce this rule, so you can print
30 arbitrary data to the line-array: it will break the data at
31 newlines appropriately.
33 See L<IO::ScalarArray> for full usage and warnings.
40 use vars qw($VERSION @ISA);
42 # The package version, both in 1.23 style *and* usable by MakeMaker:
46 @ISA = qw(IO::ScalarArray); ### also gets us new_tie :-)
49 #------------------------------
53 # Instance method, override.
54 # Return the next line, or undef on end of data.
55 # Can safely be called in an array context.
56 # Currently, lines are delimited by "\n".
62 return join( '', $self->_getlines_for_newlines );
65 if (!*$self->{Pos}) { ### full line...
66 return *$self->{AR}[*$self->{Str}++];
68 else { ### partial line...
69 my $partial = substr(*$self->{AR}[*$self->{Str}++], *$self->{Pos});
75 croak 'unsupported $/: must be "\n" or undef';
79 #------------------------------
83 # Instance method, override.
84 # Return an array comprised of the remaining lines, or () on end of data.
85 # Must be called in an array context.
86 # Currently, lines are delimited by "\n".
90 wantarray or croak("can't call getlines in scalar context!");
92 if ((defined $/) and ($/ eq "\n")) {
93 return $self->_getlines_for_newlines(@_);
95 else { ### slow but steady
96 return $self->SUPER::getlines(@_);
100 #------------------------------
102 # _getlines_for_newlines
104 # Instance method, private.
105 # If $/ is newline, do fast getlines.
106 # This CAN NOT invoke getline!
108 sub _getlines_for_newlines {
110 my ($rArray, $Str, $Pos) = @{*$self}{ qw( AR Str Pos ) };
113 if ($Pos) { ### partial line...
114 @partial = (substr( $rArray->[ $Str++ ], $Pos ));
117 *$self->{Str} = scalar @$rArray; ### about to exhaust @$rArray
119 @$rArray[ $Str .. $#$rArray ]); ### remaining full lines...
122 #------------------------------
126 # Instance method, override.
127 # Print ARGS to the underlying line array.
130 if (defined $\ && $\ ne "\n") {
131 croak 'unsupported $\: must be "\n" or undef';
135 ### print STDERR "\n[[ARRAY WAS...\n", @{*$self->{AR}}, "<<EOF>>\n";
136 my @lines = split /^/, join('', @_); @lines or return 1;
138 ### Did the previous print not end with a newline?
139 ### If so, append first line:
140 if (@{*$self->{AR}} and (*$self->{AR}[-1] !~ /\n\Z/)) {
141 *$self->{AR}[-1] .= shift @lines;
143 push @{*$self->{AR}}, @lines; ### add the remainder
144 ### print STDERR "\n[[ARRAY IS NOW...\n", @{*$self->{AR}}, "<<EOF>>\n";
148 #------------------------------
156 $Id: Lines.pm,v 1.3 2005/02/10 21:21:53 dfs Exp $
162 =head2 Primary Maintainer
164 David F. Skoll (F<dfs@roaringpenguin.com>).
166 =head2 Principal author
168 Eryq (F<eryq@zeegee.com>).
169 President, ZeeGee Software Inc (F<http://www.zeegee.com>).
172 =head2 Other contributors
174 Thanks to the following individuals for their invaluable contributions
175 (if I've forgotten or misspelled your name, please email me!):
178 for his $/ patch and the new C<getlines()>.
181 for the IO::Handle inheritance and automatic tie-ing.