1FileHandle::Unget(3) User Contributed Perl Documentation FileHandle::Unget(3)
2
3
4
6 FileHandle::Unget - FileHandle which supports multi-byte unget
7
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
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 multi-
30 byte ungetc(), but this is not guaranteed. Use this module if you want
31 a portable solution. In addition, on some operating systems, eof() will
32 not be reset if you ungetc after having read to the end of the file.
33
34 NOTE: Using sysread() with ungetc() and other buffering functions is
35 still a bad idea.
36
38 The methods for this package are the same as those of the FileHandle
39 package, with the following exceptions.
40
41 new ( ARGS )
42 The constructor is exactly the same as that of FileHandle, except
43 that you can also call it with an existing IO::Handle object to
44 "attach" unget semantics to a pre-existing handle.
45
46 $fh->ungetc ( ORD )
47 Pushes a character with the given ordinal value back onto the given
48 handle's input stream. This method can be called more than once in
49 a row to put multiple values back on the stream. Memory usage is
50 equal to the total number of bytes pushed back.
51
52 $fh->ungets ( BUF )
53 Pushes a buffer back onto the given handle's input stream. This
54 method can be called more than once in a row to put multiple
55 buffers of characters back on the stream. Memory usage is equal to
56 the total number of bytes pushed back.
57
58 The buffer is not processed in any way--managing end-of-line
59 characters and whatnot is your responsibility.
60
61 $fh->buffer ( [BUF] )
62 Get or set the pushback buffer directly.
63
64 $fh->input_record_separator ( STRING )
65 Get or set the per-filehandle input record separator. If an
66 argument is specified, the input record separator for the
67 filehandle is made independent of the global $/. Until this method
68 is called (and after clear_input_record_separator is called) the
69 global $/ is used.
70
71 Note that a return value of "undef" is ambiguous. It can either
72 mean that this method has never been called with an argument, or it
73 can mean that it was called with an argument of "undef".
74
75 $fh->clear_input_record_separator ()
76 Clear the per-filehandle input record separator. This removes the
77 per-filehandle input record separator semantics, reverting the
78 filehandle to the normal global $/ semantics.
79
80 tell ( $fh )
81 "tell" returns the actual file position minus the length of the
82 unget buffer. If you read three bytes, then unget three bytes,
83 "tell" will report a file position of 0.
84
85 Everything works as expected if you are careful to unget the exact
86 same bytes which you read. However, things get tricky if you unget
87 different bytes. First, the next bytes you read won't be the
88 actual bytes on the filehandle at the position indicated by "tell".
89 Second, "tell" will return a negative number if you unget more
90 bytes than you read. (This can be problematic since this function
91 returns -1 on error.)
92
93 seek ( $fh, [POSITION], [WHENCE] )
94 "seek" defaults to the standard seek if possible, clearing the
95 unget buffer if it succeeds. If the standard seek fails, then
96 "seek" will attempt to seek within the unget buffer. Note that in
97 this case, you will not be able to seek backward--FileHandle::Unget
98 will only save a buffer for the next bytes to be read.
99
100 For example, let's say you read 10 bytes from a pipe, then unget
101 the 10 bytes. If you seek 5 bytes forward, you won't be able to
102 read the first five bytes. (Otherwise this module would have to
103 keep around a lot of probably useless data!)
104
106 To test that this module is indeed a drop-in replacement for
107 FileHandle, the following modules were modified to use
108 FileHandle::Unget, and tested using "make test". They have all passed.
109
111 There is a bug in Perl on Windows that is exposed if you open a stream,
112 then check for eof, then call binmode. For example:
113
114 # First line
115 # Second line
116
117 open FH, "$^X -e \"open F, '$0';binmode STDOUT;print <F>\" |";
118
119 eof(FH);
120 binmode(FH);
121
122 print "First line:", scalar <FH>, "\n";
123 print "Second line:", scalar <FH>, "\n";
124
125 close FH;
126
127 One solution is to make sure that you only call binmode immediately
128 after opening the filehandle. I'm not aware of any workaround for this
129 bug that FileHandle::Unget could implement. However, the module does
130 detect this situation and prints a warning.
131
132 Contact david@coppit.org for bug reports and suggestions.
133
135 David Coppit <david@coppit.org>.
136
138 This code is distributed under the GNU General Public License (GPL)
139 Version 2. See the file LICENSE in the distribution for details.
140
142 Mail::Mbox::MessageParser for an example of how to use this package.
143
144
145
146perl v5.38.0 2023-07-20 FileHandle::Unget(3)