FileHandle::Unget - Online Linux Manual PageSection : 3
Updated : 2021-01-27
Source : perl v5.32.1
Note : User Contributed Perl Documentation

NAMEFileHandle::Unget − FileHandle which supports multi−byte unget

SYNOPSIS​ use FileHandle::Unget; ​ ​ # open file handle ​ my $fh = FileHandle::Unget−>new("file") ​ or die "cannot open filehandle: $!"; ​ ​ my $buffer; ​ read($fh,$buffer,100); ​ print $buffer; ​ ​ print <$fh>; ​ ​ $fh−>close;

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

METHODSThe methods for this package are the same as those of the FileHandle package, with the following exceptions. new ( ARGS ) The constructor is exactly the same as that of FileHandle, except that you can also call it with an existing IO::Handle object to attach unget semantics to a pre-existing handle. $fh>ungetc ( ORD ) Pushes a character with the given ordinal value back onto the given handle's input stream. This method can be called more than once in a row to put multiple values back on the stream. Memory usage is equal to the total number of bytes pushed back. $fh>ungets ( BUF ) Pushes a buffer back onto the given handle's input stream. This method can be called more than once in a row to put multiple buffers of characters back on the stream. Memory usage is equal to the total number of bytes pushed back. The buffer is not processed in any way\*(--managing end-of-line characters and whatnot is your responsibility. $fh>buffer ( [BUF] ) Get or set the pushback buffer directly. $fh>input_record_separator ( STRING ) Get or set the per-filehandle input record separator. If an argument is specified, the input record separator for the filehandle is made independent of the global $/. Until this method is called (and after clear_input_record_separator is called) the global $/ is used. Note that a return value of undef is ambiguous. It can either mean that this method has never been called with an argument, or it can mean that it was called with an argument of undef. $fh>clear_input_record_separator () Clear the per-filehandle input record separator. This removes the per-filehandle input record separator semantics, reverting the filehandle to the normal global $/ semantics. tell ( $fh ) tell returns the actual file position minus the length of the unget buffer. If you read three bytes, then unget three bytes, tell will report a file position of 0. Everything works as expected if you are careful to unget the exact same bytes which you read. However, things get tricky if you unget different bytes. First, the next bytes you read won't be the actual bytes on the filehandle at the position indicated by tell. Second, tell will return a negative number if you unget more bytes than you read. (This can be problematic since this function returns −1 on error.) seek ( $fh, [POSITION], [WHENCE] ) seek defaults to the standard seek if possible, clearing the unget buffer if it succeeds. If the standard seek fails, then seek will attempt to seek within the unget buffer. Note that in this case, you will not be able to seek backward\*(--FileHandle::Unget will only save a buffer for the next bytes to be read. For example, let's say you read 10 bytes from a pipe, then unget the 10 bytes. If you seek 5 bytes forward, you won't be able to read the first five bytes. (Otherwise this module would have to keep around a lot of probably useless data!)

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

BUGSThere is a bug in Perl on Windows that is exposed if you open a stream, then check for eof, then call binmode. For example: ​ # First line ​ # Second line ​ ​ open FH, "$^X −e \"open F, '$0';binmode STDOUT;print <F>\" |"; ​ ​ eof(FH); ​ binmode(FH); ​ ​ print "First line:", scalar <FH>, "\n"; ​ print "Second line:", scalar <FH>, "\n"; ​ ​ close FH; One solution is to make sure that you only call binmode immediately after opening the filehandle. I'm not aware of any workaround for this bug that FileHandle::Unget could implement. However, the module does detect this situation and prints a warning. Contact david@coppit.org for bug reports and suggestions.

AUTHORDavid Coppit <david@coppit.org>.

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

SEE ALSOMail::Mbox::MessageParser for an example of how to use this package.
0
Johanes Gumabo
Data Size   :   15,163 byte
man-FileHandle::Unget.3pmBuild   :   2024-12-05, 20:55   :  
Visitor Screen   :   x
Visitor Counter ( page / site )   :   6 / 166,127
Visitor ID   :     :  
Visitor IP   :   3.131.37.82   :  
Visitor Provider   :   AMAZON-02   :  
Provider Position ( lat x lon )   :   39.962500 x -83.006100   :   x
Provider Accuracy Radius ( km )   :   1000   :  
Provider City   :   Columbus   :  
Provider Province   :   Ohio ,   :   ,
Provider Country   :   United States   :  
Provider Continent   :   North America   :  
Visitor Recorder   :   Version   :  
Visitor Recorder   :   Library   :  
Online Linux Manual Page   :   Version   :   Online Linux Manual Page - Fedora.40 - march=x86-64 - mtune=generic - 24.12.05
Online Linux Manual Page   :   Library   :   lib_c - 24.10.03 - march=x86-64 - mtune=generic - Fedora.40
Online Linux Manual Page   :   Library   :   lib_m - 24.10.03 - march=x86-64 - mtune=generic - Fedora.40
Data Base   :   Version   :   Online Linux Manual Page Database - 24.04.13 - march=x86-64 - mtune=generic - fedora-38
Data Base   :   Library   :   lib_c - 23.02.07 - march=x86-64 - mtune=generic - fedora.36

Very long time ago, I have the best tutor, Wenzel Svojanovsky . If someone knows the email address of Wenzel Svojanovsky , please send an email to johanes_gumabo@yahoo.co.id .
If error, please print screen and send to johanes_gumabo@yahoo.co.id
Under development. Support me via PayPal.

ERROR : Need New Coding :         (parse_manual_page_|249|FileHandle::Unget.3pm|36/37|el══─{─══.|.el══─{─══. ds -- \|\(em\| )         (htmlprn|149|FileHandle::Unget.3pm|36/37|.el══─{─══. ds --  —  |.el══─{─══. ds -- \|\(em\| )         (parse_manual_page_|249|FileHandle::Unget.3pm|43|br══─}─══|'br══─}─══ )         (htmlprn|149|FileHandle::Unget.3pm|43|'br══─}─══ |'br══─}─══ )         (rof_escape_sequence|91|FileHandle::Unget.3pm|135|\*(--managing end-of-line characters and |The buffer is not processed in any way\*(--managing end-of-line characters and )         (rof_escape_sequence|91|FileHandle::Unget.3pm|177|\*(--FileHandle::Unget will only save a buffer for the next bytes to be |backward\*(--FileHandle::Unget will only save a buffer for the next bytes to be )