++ed by:

7 PAUSE users
4 non-PAUSE users.

Author image Gisle Aas


IO::String - Emulate IO::File interface for in-core strings


 use IO::String;
 $io = IO::String->new;
 $io = IO::String->new($var);
 tie *IO, 'IO::String';

 # read data
 read($io, $buf, 100);

 # write data
 print $io "string\n";
 syswrite($io, $buf, 100);

 select $io;
 printf "Some text %s\n", $str;

 # seek
 $pos = $io->getpos;
 $io->setpos(0);        # rewind
 $io->seek(-30, -1);
 seek($io, 0, 0);


The IO::String module provide the IO::File interface for in-core strings. An IO::String object can be attached to a string, and will make it possible to use the normal file operations for reading or writing data, as well as seeking to various locations of the string. The main reason you might want to do this, is if you have some other library module that only provide an interface to file handles, and you want to keep all the stuff in memory.

The IO::String module provide an interface compatible with IO::File as distributed with IO-1.20, but the following methods are not available; new_from_fd, fdopen, format_write, format_page_number, format_lines_per_page, format_lines_left, format_name, format_top_name.

The following methods are specific for the IO::String class:

$io = IO::String->new( [$string] )

The constructor returns a newly created IO::String object. It takes an optional argument which is the string to read from or write into. If no $string argument is given, then an internal buffer (initially empty) is allocated.

The IO::String object returned will be tied to itself. This means that you can use most perl IO builtins on it too; readline, <>, getc, print, printf, syswrite, sysread, close.

$io->open( [$string] )

Attach an existing IO::String object to some other $string, or allocate a new internal buffer (if no argument is given). The position is reset back to 0.


This method will return a reference to the string that is attached to the IO::String object. Most useful when you let the IO::String create an internal buffer to write into.

$io->pad( [$char] )

The pad() method makes it possible to specify the padding to use if the string is extended by either the seek() or truncate() methods. It is a single character and defaults to "\0".

$io->pos( [$newpos] )

Yet another interface for reading and setting the current read/write position within the string (the normal getpos/setpos/tell/seek methods are also available). The pos() method will always return the old position, and if you pass it an argument it will set the new position.

There is (deliberately) a difference between the setpos() and seek() methods in that seek() will extend the string (with the specified padding) if you go to a location past the end, while setpos() will just snap back to the end. If truncate() is used to extend the string, then it works as seek().

One more difference compared to IO::Handle, is that the write() and syswrite() methods allow the length argument to be left out.


The perl version < 5.6 the TIEHANDLE interface was still not complete. If you use such a perl seek(), tell(), eof(), fileno(), binmode() will not do anything on a IO::String handle. See perltie for details.


IO::File, IO::Stringy


Copyright 1998-2002 Gisle Aas.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.