IO::Moose::Seekable - Reimplementation of IO::Seekable with improvements
package My::IO; use Moose; extends 'IO::Moose::Handle'; with 'IO::Moose::Seekable'; package main; my $stdin = My::IO->new( file => \*STDIN, mode => 'r' ); print $stdin->slurp; print $stdin->tell, "\n";
This class provides an interface mostly compatible with IO::Seekable. The differences:
Thrown whether method is called with wrong argument.
Thrown whether fatal error is occurred by core function.
Seek the file to position pos, relative to whence:
pos is absolute position. (Seek relative to the start of the file)
pos is an offset from the current position. (Seek relative to current)
pos is an offset from the end of the file. (Seek relative to end)
The SEEK_* constants can be imported from the Fcntl module if you don't wish to use the numbers 0, 1 or 2 in your code. The SEEK_* constants are more portable.
Returns self object on success or throws an exception.
use Fcntl ':seek'; $file->seek(0, SEEK_END); $file->say("*** End of file");
Uses the system call lseek(2) directly so it can be used with sysread and syswrite methods.
Returns the new position or throws an exception.
Returns the current file position, or throws an exception on error.
Returns a value that represents the current position of the file. This method is implemented with tell method.
Goes to the position stored previously with getpos method. Returns this object on success, throws an exception on failure. This method is implemented with seek method.
$pos = $file->getpos; $file->print("something\n"); $file->setpos($pos); print $file->readline; # prints "something"
The API is not stable yet and can be changed in future.
Piotr Roszatycki <email@example.com>
Copyright 2008, 2009 by Piotr Roszatycki <firstname.lastname@example.org>.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.