FileHa...::Unget(3pm)
π FileHandle::Unget β the manual, but make it make sense π
okay bestie, so this is FileHandle::Unget, straight out of the User Contributed Perl Documentation pile. it's a FileHandle that can put more than one little byte back on the stream, and honestly? iconic behavior. π¦
β¨ NAME β¨
FileHandle::Unget β a FileHandle which supports multi-byte unget. yes, MORE than one byte. we're not doing the single-byte struggle anymore. π
πΈ SYNOPSIS πΈ
here's the whole vibe, copy-paste ready, no cap:
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;
like. open it, read a hundred bytes, print 'em, then read the rest line by line, then close. slay. β¨
π DESCRIPTION π
so FileHandle::Unget operates exactly the same as FileHandle, periodt β the ONLY glow-up is that it gives you a version of ungetc that lets you unget more than one character. it also hands you ungets so you can unget a whole string. multi-byte pushback, we love to see it. π«Ά
this module is useful when your filehandle is pointing at a stream where you can't just seek() backwards. some operating systems do support multi-byte ungetc(), but bestie, that is not guaranteed. use this module if you want a portable solution. also, on some operating systems, eof() will not be reset if you ungetc after having read all the way to the end of the file. rude, but true. π
NOTE: usingsysread()withungetc()and other buffering functions is still a bad idea. like, still. even with this module. don't. π
π METHODS π
the 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 FileHandle's, except you can also call it with an existingIO::Handleobject to "attach" unget semantics to a pre-existing handle. glow-up your old handles, bestie. β¨$fh->ungetc ( ORD )β pushes a character with the given ordinal value back onto the given handle's input stream. you can call this 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. so like, push back responsibly. π$fh->ungets ( BUF )β pushes a buffer back onto the given handle's input stream. call it more than once in a row and you can stack up 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. i know. i know. it's giving main character energy and main character chores. π
$fh->buffer ( [BUF] )β get or set the pushback buffer directly. raw. unfiltered. direct access. π¦$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 afterclear_input_record_separatoris called) the global$/is used.
heads up: a return value ofundefis 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 ofundef. literally can't tell. the mystery is the point. π΅οΈ
$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. back to factory settings, bestie. π§tell ( $fh )βtellreturns the actual file position minus the length of the unget buffer. so if you read three bytes, then unget three bytes,tellwill report a file position of0. math is math.
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:
- the next bytes you read won't be the actual bytes on the filehandle at the position indicated by
tell. tellwill return a negative number if you unget more bytes than you read. (this can be problematic since this function returns-1on error.) π
seek ( $fh, [POSITION], [WHENCE] )βseekdefaults to the standard seek if possible, clearing the unget buffer if it succeeds. if the standard seek fails, thenseekwill attempt to seek within the unget buffer.
note: 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!) and honestly? fair. she's efficient. π
π COMPATIBILITY π
to 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. π
π BUGS π
there 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. so at least she tells you. π«Ά
contact david@coppit.org for bug reports and suggestions. email the man, bestie.
π«Ά AUTHOR π«Ά
David Coppit <david@coppit.org>.
π LICENSE π
this code is distributed under the GNU General Public License (GPL) Version 2. see the file LICENSE in the distribution for details.
β SEE ALSO β
Mail::Mbox::MessageParser for an example of how to use this package. iykyk. π₯
okay that's the whole thing, no notes. this page is from perl v5.36.0, dated 2023-08-06, and it lives under the User Contributed Perl Documentation umbrella. go forth and unget multiple bytes, you absolute legend. π β¨π
π¦ where this page comes from
- debian trixie: libfilehandle-unget-perl 0.1634-4 Β· GPL-2.0-only
- ubuntu noble: libfilehandle-unget-perl 0.1634-4 Β· GPL-2.0-only
- ubuntu resolute: libfilehandle-unget-perl 0.1634-4 Β· GPL-2.0-only
- ubuntu stonking: libfilehandle-unget-perl 0.1634-4 Β· GPL-2.0-only
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms