1String(3) User Contributed Perl Documentation String(3)
2
3
4
6 IO::String - Emulate file interface for in-core strings
7
9 use IO::String;
10 $io = IO::String->new;
11 $io = IO::String->new($var);
12 tie *IO, 'IO::String';
13
14 # read data
15 <$io>;
16 $io->getline;
17 read($io, $buf, 100);
18
19 # write data
20 print $io "string\n";
21 $io->print(@data);
22 syswrite($io, $buf, 100);
23
24 select $io;
25 printf "Some text %s\n", $str;
26
27 # seek
28 $pos = $io->getpos;
29 $io->setpos(0); # rewind
30 $io->seek(-30, -1);
31 seek($io, 0, 0);
32
34 The "IO::String" module provides the "IO::File" interface for in-core
35 strings. An "IO::String" object can be attached to a string, and makes
36 it possible to use the normal file operations for reading or writing
37 data, as well as for seeking to various locations of the string. This
38 is useful when you want to use a library module that only provides an
39 interface to file handles on data that you have in a string variable.
40
41 Note that perl-5.8 and better has built-in support for "in memory"
42 files, which are set up by passing a reference instead of a filename to
43 the open() call. The reason for using this module is that it makes the
44 code backwards compatible with older versions of Perl.
45
46 The "IO::String" module provides an interface compatible with
47 "IO::File" as distributed with IO-1.20, but the following methods are
48 not available: new_from_fd, fdopen, format_write, format_page_number,
49 format_lines_per_page, format_lines_left, format_name, format_top_name.
50
51 The following methods are specific to the "IO::String" class:
52
53 $io = IO::String->new
54 $io = IO::String->new( $string )
55 The constructor returns a newly-created "IO::String" object. It
56 takes an optional argument, which is the string to read from or
57 write into. If no $string argument is given, then an internal
58 buffer (initially empty) is allocated.
59
60 The "IO::String" object returned is tied to itself. This means
61 that you can use most Perl I/O built-ins on it too: readline, <>,
62 getc, print, printf, syswrite, sysread, close.
63
64 $io->open
65 $io->open( $string )
66 Attaches an existing IO::String object to some other $string, or
67 allocates a new internal buffer (if no argument is given). The
68 position is reset to 0.
69
70 $io->string_ref
71 Returns a reference to the string that is attached to the
72 "IO::String" object. Most useful when you let the "IO::String"
73 create an internal buffer to write into.
74
75 $io->pad
76 $io->pad( $char )
77 Specifies the padding to use if the string is extended by either
78 the seek() or truncate() methods. It is a single character and
79 defaults to "\0".
80
81 $io->pos
82 $io->pos( $newpos )
83 Yet another interface for reading and setting the current
84 read/write position within the string (the normal
85 getpos/setpos/tell/seek methods are also available). The pos()
86 method always returns the old position, and if you pass it an
87 argument it sets the new position.
88
89 There is (deliberately) a difference between the setpos() and
90 seek() methods in that seek() extends the string (with the
91 specified padding) if you go to a location past the end, whereas
92 setpos() just snaps back to the end. If truncate() is used to
93 extend the string, then it works as seek().
94
96 In Perl versions < 5.6, the TIEHANDLE interface was incomplete. If you
97 use such a Perl, then seek(), tell(), eof(), fileno(), binmode() will
98 not do anything on an "IO::String" handle. See perltie for details.
99
101 IO::File, IO::Stringy, "open" in perlfunc
102
104 Copyright 1998-2005 Gisle Aas.
105
106 This library is free software; you can redistribute it and/or modify it
107 under the same terms as Perl itself.
108
109
110
111perl v5.32.0 2020-07-28 String(3)