.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Imager::IO 3pm" .TH Imager::IO 3pm 2024-04-13 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Imager::IO \- Imager's io_layer object. .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 2 \& # Imager supplies Imager::IO objects to various callbacks \& my $IO = ...; \& \& my $count = $IO\->write($data); \& my $count = $IO\->read($buffer, $max_count); \& my $position = $IO\->seek($offset, $whence); \& my $status = $IO\->close; .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" Imager uses an abstraction when dealing with image files to allow the same code to work with disk files, in memory data and callbacks. .PP If you're writing an Imager file handler your code will be passed an Imager::IO object to write to or read from. .PP Note that Imager::IO can only work with collections of bytes \- if you need to read UTF\-8 data you will need to read the bytes and decode them. If you want to write UTF\-8 data you will need to encode your characters to bytes and write the bytes. .IX Xref "UTF-8 Unicode" .SH CONSTRUCTORS .IX Header "CONSTRUCTORS" .IP new_fd($fd) 4 .IX Item "new_fd($fd)" Create a new I/O layer based on a file descriptor. .Sp .Vb 1 \& my $io = Imager::IO\->new(fileno($fh)); .Ve .IP new_buffer($data) 4 .IX Item "new_buffer($data)" Create a new I/O layer based on a memory buffer. .Sp Buffer I/O layers are read only. .Sp \&\f(CW$data\fR can either a simple octet string, or a reference to an octet string. If \f(CW$data\fR contains characters with a code point above \&\f(CW0xFF\fR an exception will be thrown. .ie n .IP "new_cb($writecb, $readcb, $seekcb, $closecb)" 4 .el .IP "new_cb($writecb, \f(CW$readcb\fR, \f(CW$seekcb\fR, \f(CW$closecb\fR)" 4 .IX Item "new_cb($writecb, $readcb, $seekcb, $closecb)" Create a new I/O layer based on callbacks. See "I/O Callbacks" in Imager::Files for details on the behavior of the callbacks. .IP new_fh($fh) 4 .IX Item "new_fh($fh)" Create a new I/O layer based on a perl file handle. .IP \fBnew_bufchain()\fR 4 .IX Item "new_bufchain()" Create a new \f(CW\*(C`bufchain\*(C'\fR based I/O layer. This accumulates the file data as a chain of buffers starting from an empty stream. .Sp Use the "\fBslurp()\fR" method to retrieve the accumulated content into a perl string. .SH "BUFFERED I/O METHODS" .IX Header "BUFFERED I/O METHODS" These methods use buffered I/O to improve performance unless you call \&\fBset_buffered()\fR to disable buffering. .PP Prior to Imager 0.86 the write and read methods performed raw I/O. .IP write($data) 4 .IX Item "write($data)" Call to write to the file. Returns the number of bytes written. The data provided may contain only characters \ex00 to \exFF \- characters outside this range will cause this method to \fBcroak()\fR. .Sp If you supply a UTF\-8 flagged string it will be converted to a byte string, which may have a performance impact. .Sp Returns \-1 on error, though in most cases if the result of the write isn't the number of bytes supplied you'll want to treat it as an error anyway. .ie n .IP "read($buffer, $size)" 4 .el .IP "read($buffer, \f(CW$size\fR)" 4 .IX Item "read($buffer, $size)" .Vb 2 \& my $buffer; \& my $count = $io\->read($buffer, $max_bytes); .Ve .Sp Reads up to \fR\f(CI$max_bytes\fR\fI\fR bytes from the current position in the file and stores them in \fI\fR\f(CI$buffer\fR\fI\fR. Returns the number of bytes read on success or an empty list on failure. Note that a read of zero bytes is \fBnot\fR a failure, this indicates end of file. .IP read2($size) 4 .IX Item "read2($size)" .Vb 1 \& my $buffer = $io\->read2($max_bytes); .Ve .Sp An alternative interface to read, that might be simpler to use in some cases. .Sp Returns the data read or an empty list. At end of file the data read will be an empty string. .ie n .IP "seek($offset, $whence)" 4 .el .IP "seek($offset, \f(CW$whence\fR)" 4 .IX Item "seek($offset, $whence)" .Vb 1 \& my $new_position = $io\->seek($offset, $whence); .Ve .Sp Seek to a new position in the file. Possible values for \fR\f(CI$whence\fR\fI\fR are: .RS 4 .IP \(bu 4 \&\f(CW\*(C`SEEK_SET\*(C'\fR \- \fR\f(CI$offset\fR\fI\fR is the new position in the file. .IP \(bu 4 \&\f(CW\*(C`SEEK_CUR\*(C'\fR \- \fR\f(CI$offset\fR\fI\fR is the offset from the current position in the file. .IP \(bu 4 \&\f(CW\*(C`SEEK_END\*(C'\fR \- \fR\f(CI$offset\fR\fI\fR is the offset relative to the end of the file. .RE .RS 4 .Sp Note that seeking past the end of the file may or may not result in an error. .Sp Any buffered output will be flushed, if flushing fails, \fBseek()\fR will return \-1. .Sp Returns the new position in the file, or \-1 on error. .RE .IP \fBgetc()\fR 4 .IX Item "getc()" Return the next byte from the stream. .Sp Returns the ordinal of the byte or \-1 on error or end of file. .Sp .Vb 3 \& while ((my $c = $io\->getc) != \-1) { \& print chr($c); \& } .Ve .IP \fBnextc()\fR 4 .IX Item "nextc()" Discard the next byte from the stream. .Sp Returns nothing. .IP \fBgets()\fR 4 .IX Item "gets()" .PD 0 .IP gets($max_size) 4 .IX Item "gets($max_size)" .ie n .IP "gets($max_size, $end_of_line)" 4 .el .IP "gets($max_size, \f(CW$end_of_line\fR)" 4 .IX Item "gets($max_size, $end_of_line)" .PD Returns the next line of input from the stream, as terminated by \&\f(CW\*(C`end_of_line\*(C'\fR. .Sp The default \f(CW\*(C`max_size\*(C'\fR is 8192. .Sp The default \f(CW\*(C`end_of_line\*(C'\fR is \f(CW\*(C`ord "\en"\*(C'\fR. .Sp Returns nothing if the stream is in error or at end of file. .Sp Returns the line as a string, including the line terminator (if one was found) on success. .Sp .Vb 3 \& while (defined(my $line = $io\->gets)) { \& # do something with $line \& } .Ve .IP \fBpeekc()\fR 4 .IX Item "peekc()" Return the buffered next character from the stream, loading the buffer if necessary. .Sp For an unbuffered stream a buffer will be setup and loaded with a single character. .Sp Returns the ordinal of the byte or \-1 on error or end of file. .Sp .Vb 1 \& my $c = $io\->peekc; .Ve .IP peekn($size) 4 .IX Item "peekn($size)" Returns up to the next \f(CW\*(C`size\*(C'\fR bytes from the file as a string. .Sp Only up to the stream buffer size bytes (currently 8192) can be peeked. .Sp This method ignores the buffering state of the stream. .Sp Returns nothing on EOF. .Sp .Vb 4 \& my $s = $io\->peekn(4); \& if ($s =~ /^(II|MM)\e*\e0/) { \& print "TIFF image"; \& } .Ve .IP putc($code) 4 .IX Item "putc($code)" Write a single character to the stream. .Sp Returns \f(CW\*(C`code\*(C'\fR on success, or \-1 on failure. .IP \fBclose()\fR 4 .IX Item "close()" .Vb 1 \& my $result = $io\->close; .Ve .Sp Call when you're done with the file. If the IO object is connected to a file this won't close the file handle, but buffers may be flushed (if any). .Sp Returns 0 on success, \-1 on failure. .IP \fBeof()\fR 4 .IX Item "eof()" .Vb 1 \& $io\->eof .Ve .Sp Test if the stream is at end of file. No further read requests will be passed to your read callback until you \fBseek()\fR. .IP \fBerror()\fR 4 .IX Item "error()" Test if the stream has encountered a read or write error. .Sp .Vb 3 \& my $data = $io\->read2(100); \& $io\->error \& and die "Failed"; .Ve .Sp When the stream has the error flag set no further read or write requests will be passed to your callbacks until you seek. .IP \fBflush()\fR 4 .IX Item "flush()" .Vb 2 \& $io\->flush \& or die "Flush error"; .Ve .Sp Flush any buffered output. This will not call lower write layers when the stream has it's error flag set. .Sp Returns a true value on success. .IP \fBis_buffered()\fR 4 .IX Item "is_buffered()" Test if buffering is enabled for this stream. .Sp Returns a true value if the stream is buffered. .IP set_buffered($enabled) 4 .IX Item "set_buffered($enabled)" If \f(CW$enabled\fR is a non-zero integer, enable buffering, other disable it. .Sp Disabling buffering will flush any buffered output, but any buffered input will be retained and consumed by input methods. .Sp Returns true if any buffered output was flushed successfully, false if there was an error flushing output. .SH "RAW I/O METHODS" .IX Header "RAW I/O METHODS" These call the underlying I/O abstraction directly. .IP \fBraw_write()\fR 4 .IX Item "raw_write()" Call to write to the file. Returns the number of bytes written. The data provided may contain only characters \ex00 to \exFF \- characters outside this range will cause this method to \fBcroak()\fR. .Sp If you supply a UTF\-8 flagged string it will be converted to a byte string, which may have a performance impact. .Sp Returns \-1 on error, though in most cases if the result of the write isn't the number of bytes supplied you'll want to treat it as an error anyway. .IP \fBraw_read()\fR 4 .IX Item "raw_read()" .Vb 2 \& my $buffer; \& my $count = $io\->raw_read($buffer, $max_bytes); .Ve .Sp Reads up to \fR\f(CI$max_bytes\fR\fI\fR bytes from the current position in the file and stores them in \fI\fR\f(CI$buffer\fR\fI\fR. Returns the number of bytes read on success or an empty list on failure. Note that a read of zero bytes is \fBnot\fR a failure, this indicates end of file. .IP \fBraw_read2()\fR 4 .IX Item "raw_read2()" .Vb 1 \& my $buffer = $io\->raw_read2($max_bytes); .Ve .Sp An alternative interface to raw_read, that might be simpler to use in some cases. .Sp Returns the data read or an empty list. .IP \fBraw_seek()\fR 4 .IX Item "raw_seek()" .Vb 1 \& my $new_position = $io\->raw_seek($offset, $whence); .Ve .Sp Seek to a new position in the file. Possible values for \fR\f(CI$whence\fR\fI\fR are: .RS 4 .IP \(bu 4 \&\f(CW\*(C`SEEK_SET\*(C'\fR \- \fR\f(CI$offset\fR\fI\fR is the new position in the file. .IP \(bu 4 \&\f(CW\*(C`SEEK_CUR\*(C'\fR \- \fR\f(CI$offset\fR\fI\fR is the offset from the current position in the file. .IP \(bu 4 \&\f(CW\*(C`SEEK_END\*(C'\fR \- \fR\f(CI$offset\fR\fI\fR is the offset relative to the end of the file. .RE .RS 4 .Sp Note that seeking past the end of the file may or may not result in an error. .Sp Returns the new position in the file, or \-1 on error. .RE .IP \fBraw_close()\fR 4 .IX Item "raw_close()" .Vb 1 \& my $result = $io\->raw_close; .Ve .Sp Call when you're done with the file. If the IO object is connected to a file this won't close the file handle. .Sp Returns 0 on success, \-1 on failure. .SH "UTILITY METHODS" .IX Header "UTILITY METHODS" .IP \fBslurp()\fR 4 .IX Item "slurp()" Retrieve the data accumulated from an I/O layer object created with the \fBnew_bufchain()\fR method. .Sp .Vb 1 \& my $data = $io\->slurp; .Ve .IP \fBdump()\fR 4 .IX Item "dump()" Dump the internal buffering state of the I/O object to \f(CW\*(C`stderr\*(C'\fR. .Sp .Vb 1 \& $io\->dump(); .Ve .SH AUTHOR .IX Header "AUTHOR" Tony Cook .SH "SEE ALSO" .IX Header "SEE ALSO" Imager, Imager::Files