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
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
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 buf‐
56 fers 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 char‐
60 acters 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. After it is
67 called, the input record separator for the filehandle is indepen‐
68 dent of the global $/. Until this method is called (and after
69 clear_input_record_separator is called) the global $/ is used.
70
71 $fh->clear_input_record_separator ()
72 Clear the per-filehandle input record separator. This removes the
73 per-filehandle input record separator semantics, reverting the
74 filehandle to the normal global $/ semantics.
75
76 tell ( $fh )
77 "tell" returns the actual file position minus the length of the
78 unget buffer. If you read three bytes, then unget three bytes,
79 "tell" will report a file position of 0.
80
81 Everything works as expected if you are careful to unget the exact
82 same bytes which you read. However, things get tricky if you unget
83 different bytes. First, the next bytes you read won't be the
84 actual bytes on the filehandle at the position indicated by "tell".
85 Second, "tell" will return a negative number if you unget more
86 bytes than you read. (This can be problematic since this function
87 returns -1 on error.)
88
89 seek ( $fh, [POSITION], [WHENCE] )
90 "seek" defaults to the standard seek if possible, clearing the
91 unget buffer if it succeeds. If the standard seek fails, then
92 "seek" will attempt to seek within the unget buffer. Note that in
93 this case, you will not be able to seek backward--FileHandle::Unget
94 will only save a buffer for the next bytes to be read.
95
96 For example, let's say you read 10 bytes from a pipe, then unget
97 the 10 bytes. If you seek 5 bytes forward, you won't be able to
98 read the first five bytes. (Otherwise this module would have to
99 keep around a lot of probably useless data!)
100
102 To test that this module is indeed a drop-in replacement for FileHan‐
103 dle, the following modules were modified to use FileHandle::Unget, and
104 tested using "make test". They have all passed.
105
107 There is a bug in Perl on Windows that is exposed if you open a stream,
108 then check for eof, then call binmode. For example:
109
110 # First line
111 # Second line
112
113 open FH, "$^X -e \"open F, '$0';binmode STDOUT;print <F>\" ⎪";
114
115 eof(FH);
116 binmode(FH);
117
118 print "First line:", scalar <FH>, "\n";
119 print "Second line:", scalar <FH>, "\n";
120
121 close FH;
122
123 One solution is to make sure that you only call binmode immediately
124 after opening the filehandle. I'm not aware of any workaround for this
125 bug that FileHandle::Unget could implement. However, the module does
126 detect this situation and prints a warning.
127
128 Contact david@coppit.org for bug reports and suggestions.
129
131 David Coppit <david@coppit.org>.
132
134 This software is distributed under the terms of the GPL. See the file
135 "LICENSE" for more information.
136
138 Mail::Mbox::MessageParser for an example of how to use this package.
139
140
141
142perl v5.8.8 2005-01-04 FileHandle::Unget(3)