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. After it is
67           called, the input record separator for the filehandle is
68           independent of the global $/.  Until this method is called (and
69           after clear_input_record_separator is called) the global $/ is
70           used.
71
72       $fh->clear_input_record_separator ()
73           Clear the per-filehandle input record separator. This removes the
74           per-filehandle input record separator semantics, reverting the
75           filehandle to the normal global $/ semantics.
76
77       tell ( $fh )
78           "tell" returns the actual file position minus the length of the
79           unget buffer.  If you read three bytes, then unget three bytes,
80           "tell" will report a file position of 0.
81
82           Everything works as expected if you are careful to unget the exact
83           same bytes which you read.  However, things get tricky if you unget
84           different bytes.  First, the next bytes you read won't be the
85           actual bytes on the filehandle at the position indicated by "tell".
86           Second, "tell" will return a negative number if you unget more
87           bytes than you read. (This can be problematic since this function
88           returns -1 on error.)
89
90       seek ( $fh, [POSITION], [WHENCE] )
91           "seek" defaults to the standard seek if possible, clearing the
92           unget buffer if it succeeds. If the standard seek fails, then
93           "seek" will attempt to seek within the unget buffer. Note that in
94           this case, you will not be able to seek backward--FileHandle::Unget
95           will only save a buffer for the next bytes to be read.
96
97           For example, let's say you read 10 bytes from a pipe, then unget
98           the 10 bytes.  If you seek 5 bytes forward, you won't be able to
99           read the first five bytes.  (Otherwise this module would have to
100           keep around a lot of probably useless data!)
101

COMPATIBILITY

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

BUGS

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

AUTHOR

132       David Coppit <david@coppit.org>.
133

LICENSE

135       This software is distributed under the terms of the GPL. See the file
136       "LICENSE" for more information.
137

SEE ALSO

139       Mail::Mbox::MessageParser for an example of how to use this package.
140
141
142
143perl v5.12.0                      2009-08-09              FileHandle::Unget(3)
Impressum