1FileHandle::Unget(3)  User Contributed Perl Documentation FileHandle::Unget(3)
2
3
4

NAME

6       FileHandle::Unget - FileHandle which supports multi-byte unget
7

SYNOPSIS

9         use FileHandle::Unget;
10
11         # open file handle
12         my $fh = FileHandle::Unget->new("file")
13           or die "cannot open filehandle: $!";
14
15         my $buffer;
16         read($fh,$buffer,100);
17         print $buffer;
18
19         print <$fh>;
20
21         $fh->close;
22

DESCRIPTION

24       FileHandle::Unget operates exactly the same as FileHandle, except that
25       it provides a version of ungetc that allows you to unget more than one
26       character.  It also provides ungets to unget a string.
27
28       This module is useful if the filehandle refers to a stream for which
29       you can't just "seek()" backwards. Some operating systems support
30       multi-byte "ungetc()", but this is not guaranteed. Use this module if
31       you want a portable solution. In addition, on some operating systems,
32       eof() will not be reset if you ungetc after having read to the end of
33       the file.
34
35       NOTE: Using "sysread()" with "ungetc()" and other buffering functions
36       is still a bad idea.
37

METHODS

39       The methods for this package are the same as those of the FileHandle
40       package, with the following exceptions.
41
42       new ( ARGS )
43           The constructor is exactly the same as that of FileHandle, except
44           that you can also call it with an existing IO::Handle object to
45           "attach" unget semantics to a pre-existing handle.
46
47       $fh->ungetc ( ORD )
48           Pushes a character with the given ordinal value back onto the given
49           handle's input stream. This method can be called more than once in
50           a row to put multiple values back on the stream. Memory usage is
51           equal to the total number of bytes pushed back.
52
53       $fh->ungets ( BUF )
54           Pushes a buffer back onto the given handle's input stream. This
55           method can be called more than once in a row to put multiple
56           buffers of characters back on the stream.  Memory usage is equal to
57           the total number of bytes pushed back.
58
59           The buffer is not processed in any way--managing end-of-line
60           characters and whatnot is your responsibility.
61
62       $fh->buffer ( [BUF] )
63           Get or set the pushback buffer directly.
64
65       $fh->input_record_separator ( STRING )
66           Get or set the per-filehandle input record separator. If an
67           argument is specified, the input record separator for the
68           filehandle is made independent of the global $/. Until this method
69           is called (and after clear_input_record_separator is called) the
70           global $/ is used.
71
72           Note that a return value of "undef" is ambiguous. It can either
73           mean that this method has never been called with an argument, or it
74           can mean that it was called with an argument of "undef".
75
76       $fh->clear_input_record_separator ()
77           Clear the per-filehandle input record separator. This removes the
78           per-filehandle input record separator semantics, reverting the
79           filehandle to the normal global $/ semantics.
80
81       tell ( $fh )
82           "tell" returns the actual file position minus the length of the
83           unget buffer.  If you read three bytes, then unget three bytes,
84           "tell" will report a file position of 0.
85
86           Everything works as expected if you are careful to unget the exact
87           same bytes which you read.  However, things get tricky if you unget
88           different bytes.  First, the next bytes you read won't be the
89           actual bytes on the filehandle at the position indicated by "tell".
90           Second, "tell" will return a negative number if you unget more
91           bytes than you read. (This can be problematic since this function
92           returns -1 on error.)
93
94       seek ( $fh, [POSITION], [WHENCE] )
95           "seek" defaults to the standard seek if possible, clearing the
96           unget buffer if it succeeds. If the standard seek fails, then
97           "seek" will attempt to seek within the unget buffer. Note that in
98           this case, you will not be able to seek backward--FileHandle::Unget
99           will only save a buffer for the next bytes to be read.
100
101           For example, let's say you read 10 bytes from a pipe, then unget
102           the 10 bytes.  If you seek 5 bytes forward, you won't be able to
103           read the first five bytes.  (Otherwise this module would have to
104           keep around a lot of probably useless data!)
105

COMPATIBILITY

107       To test that this module is indeed a drop-in replacement for
108       FileHandle, the following modules were modified to use
109       FileHandle::Unget, and tested using "make test". They have all passed.
110

BUGS

112       There is a bug in Perl on Windows that is exposed if you open a stream,
113       then check for eof, then call binmode. For example:
114
115         # First line
116         # Second line
117
118         open FH, "$^X -e \"open F, '$0';binmode STDOUT;print <F>\" |";
119
120         eof(FH);
121         binmode(FH);
122
123         print "First line:", scalar <FH>, "\n";
124         print "Second line:", scalar <FH>, "\n";
125
126         close FH;
127
128       One solution is to make sure that you only call binmode immediately
129       after opening the filehandle. I'm not aware of any workaround for this
130       bug that FileHandle::Unget could implement. However, the module does
131       detect this situation and prints a warning.
132
133       Contact david@coppit.org for bug reports and suggestions.
134

AUTHOR

136       David Coppit <david@coppit.org>.
137

LICENSE

139       This code is distributed under the GNU General Public License (GPL)
140       Version 2.  See the file LICENSE in the distribution for details.
141

SEE ALSO

143       Mail::Mbox::MessageParser for an example of how to use this package.
144
145
146
147perl v5.32.0                      2020-07-28              FileHandle::Unget(3)
Impressum