Pair Networks


Imager::IO - Imager's io_layer object.


  # 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;


Imager uses an abstraction when dealing with image files to allow the same code to work with disk files, in memory data and callbacks.

If you're writing an Imager file handler your code will be passed an Imager::IO object to write to or read from.



Call to write to the file. Returns the number of bytes written. The data provided may contain only characters \x00 to \xFF - characters outside this range will cause this method to croak().

If you supply a UTF-8 flagged string it will be converted to a byte string, which may have a performance impact.

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.

  my $buffer;
  my $count = $io->read($buffer, $max_bytes);

Reads up to $max_bytes bytes from the current position in the file and stores them in $buffer. Returns the number of bytes read on success or an empty list on failure. Note that a read of zero bytes is not a failure, this indicates end of file.

  my $buffer = $io->read2($max_bytes);

An alternative interface to read, that might be simpler to use in some cases.

Returns the data read or an empty list.

  my $new_position = $io->seek($offset, $whence);

Seek to a new position in the file. Possible values for $whence are:

  • SEEK_SET - $offset is the new position in the file.
  • SEEK_CUR - $offset is the offset from the current position in the file.
  • SEEK_END - $offset is the offset relative to the end of the file.

Note that seeking past the end of the file may or may not result in an error.

Returns the new position in the file, or -1 on error.

  my $result = $io->close;

Call when you're 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).

Returns 0 on success, -1 on failure.


Tony Cook <>


Imager, Imager::Files