10 use Symbol qw
/qualify_to_ref/;
11 use Scalar
::Util qw
/openhandle/;
14 our $VERSION = '1.04';
15 XSLoader
::load __PACKAGE__
, $VERSION;
18 my $USE_PERLIO = HAVE_PERLIO_LAYERS
;
22 my $func = (caller 1)[3];
23 $Debug and warn "$func: ", @_;
45 !/^_/ and /[^[:upper:]]/ and
46 exists &{"$from\::$_"} and do
48 *{"$to\::$_"} = \
&{"$from\::$_"};
52 push @carp, qq/"$_" is not exported by $from/;
57 croak
"can't continue after import errors";
66 qualify_to_ref
shift, caller 2;
68 openhandle
$fh or return;
70 _check_fh
$fh or return;
77 no warnings
'uninitialized';
78 debug
'[', (join '][', @_), ']';
81 my $fh = _get_fh
shift or return;
83 my $str = @_ ?
(join "", reverse @_) : $_;
84 length $str or return "0 but true";
89 debug
"using PerlIO_unread";
90 $rv = eval { _PerlIO_unread
$fh, $str };
93 debug
"using IO::Unread::Tied";
94 tie
*$fh, 'IO::Unread::Tied' => $fh, $str;
99 warnings
::enabled
"io" and carp
$@
;
102 defined $rv or return;
103 $rv or return "0 but true";
108 my $fh = _get_fh
shift or return;
110 my $str = @_ ?
shift : $_;
111 length $str or return '';
113 my $rv = _PerlIO_ungetc
$fh, substr $str, 0, 1;
114 defined $rv or return;
120 package IO
::Unread
::Tied
;
123 use base qw
/Tie::Handle/;
125 BEGIN { *debug
= \
&IO
::Unread
::debug
}
128 my ($c, $handle, $data) = @_;
130 $handle eq 'RETIE' and (debug
"retieing"), return $data;
131 length $data or croak __PACKAGE__
."::TIEHANDLE called with null data";
132 return bless { handle
=> $handle, data
=> $data }, $c;
136 my ($s, $data, $len, $off) = @_;
138 my $h = $s->{handle
};
140 my $rv = print $h substr $data, 0, $off;
141 tie
*$h, ref $s => RETIE
=> $s;
146 my ($s, undef, $len, $off) = @_;
147 my $h = $s->{handle
};
152 my $read = substr $s->{data
}, 0, $len, '';
153 $len -= length $read;
154 unless (length $s->{data
}) {
156 $rv = read $h, $read, $len, length $read;
157 defined $rv and $rv += length $read;
160 substr($_[1], $off, 0) = $read;
166 my $h = $s->{handle
};
171 if (not defined $/) {
173 return $s->{data
} . <$h>;
177 $rv = $s->{data
} =~ s!^ ([^\n]* \n+)!!x;
178 $rv = $rv ?
$1 : undef;
181 $rv = $s->{data
} =~ s!^ (.*? \Q$/\E )!!x;
182 $rv = $rv ?
$1 : undef;
185 debug
"rv = ", (defined $rv) ?
(quotemeta $rv) : "(undef)";
187 unless (defined $rv) {
192 if ($s->{data
} eq '') {
195 my $done = $rv =~ m! \Q$/\E $ !x;
198 IO
::Unread
::ungetc
$h, $chr;
199 $done = ($chr ne "\n");
201 debug
"rv = |$rv|, \$/ = |$/|, done = $done";
202 $rv .= <$h> unless $done;
210 untie *{$_[0]{handle
}};
216 untie *{$s->{handle
}};
217 seek $s->{handle
}, $_[0], $_[1];
221 untie *{$_[0]{handle
}};
235 IO::Unread - push more than one character back onto a filehandle
241 unread STDIN, "hello world\n";
248 C<IO::Unread> exports one function, C<unread>, which will push data back
249 onto a filehandle. Any amount of data can be pushed: if your perl is
250 built with PerlIO layers, the data is stored in a special C<:pending>
251 layer; if not, the module C<tie>s the filehandle to a class which
252 returns the unread data and unties itself.
254 =head2 unread FILEHANDLE, LIST
256 C<unread> unreads LIST onto FILEHANDLE. If LIST is omitted, C<$_> is unread.
257 Returns the number of characters unread on success, C<undef> on failure. Warnings
258 are produced under category C<io>.
260 Note that C<unread $FH, 'a', 'b'> is equivalent to
265 , ie. to C<unread $FH, 'ba'> rather than C<unread $FH, 'ab'>.
267 =head2 ungetc FILEHANDLE, STRING
269 C<ungetc> pushes the first character of STRING onto FILEHANDLE. Unlike
270 C<unread>, it does not use a C<tie> implementation if your perl doesn't
271 support PerlIO layers; rather it calls your I<ungetc(3)>. This is only
272 guarenteed to support one character of pushback, and then only if it is
273 the last character that was read from the handle.
277 None by default; C<unread>, C<ungetc> on request.
281 C<ungetc> is subject to the whims of your libc if you're not using
286 Copyright 2003 Ben Morrow <ben@morrow.me.uk>
288 This library is free software; you can redistribute it and/or modify
289 it under the same terms as Perl itself.
293 L<PerlIO>, L<perltie>, L<ungetc(3)>