… | |
… | |
24 | =cut |
24 | =cut |
25 | |
25 | |
26 | package Convert::Scalar; |
26 | package Convert::Scalar; |
27 | |
27 | |
28 | BEGIN { |
28 | BEGIN { |
29 | $VERSION = 1.11; |
29 | $VERSION = 1.12; |
30 | @ISA = qw(Exporter); |
30 | @ISA = qw(Exporter); |
31 | @EXPORT_OK = qw(readonly readonly_on readonly_off weaken unmagic len grow extend); |
31 | @EXPORT_OK = qw(readonly readonly_on readonly_off weaken unmagic len grow extend extend_read readall writeall); |
32 | %EXPORT_TAGS = ( |
32 | %EXPORT_TAGS = ( |
33 | taint => [qw(taint untaint tainted)], |
33 | taint => [qw(taint untaint tainted)], |
34 | utf8 => [qw(utf8 utf8_on utf8_off utf8_valid utf8_upgrade utf8_downgrade utf8_encode utf8_decode utf8_length)], |
34 | utf8 => [qw(utf8 utf8_on utf8_off utf8_valid utf8_upgrade utf8_downgrade utf8_encode utf8_decode utf8_length)], |
35 | refcnt => [qw(refcnt refcnt_inc refcnt_dec refcnt_rv refcnt_inc_rv refcnt_dec_rv)], |
35 | refcnt => [qw(refcnt refcnt_inc refcnt_dec refcnt_rv refcnt_inc_rv refcnt_dec_rv)], |
36 | ok => [qw(ok uok rok pok nok niok)], |
36 | ok => [qw(ok uok rok pok nok niok)], |
… | |
… | |
140 | current length is less than the new value. This does not affect the |
140 | current length is less than the new value. This does not affect the |
141 | contents of the scalar, but is only useful to "pre-allocate" memory space |
141 | contents of the scalar, but is only useful to "pre-allocate" memory space |
142 | if you know the scalar will grow. The return value is the modified scalar |
142 | if you know the scalar will grow. The return value is the modified scalar |
143 | (the scalar is modified in-place). |
143 | (the scalar is modified in-place). |
144 | |
144 | |
145 | =item scalar = extend scalar, addlen |
145 | =item scalar = extend scalar, addlen=64 |
146 | |
146 | |
147 | Reserves enough space in the scalar so that addlen bytes can be appended |
147 | Reserves enough space in the scalar so that addlen bytes can be appended |
148 | without reallocating it. The actual contents of the scalar will not be |
148 | without reallocating it. The actual contents of the scalar will not be |
149 | affected. The modified scalar will also be returned. |
149 | affected. The modified scalar will also be returned. |
150 | |
150 | |
… | |
… | |
154 | |
154 | |
155 | If you instead use C<extend $scalar, length $shortstring>, then |
155 | If you instead use C<extend $scalar, length $shortstring>, then |
156 | Convert::Scalar will use a "size to next power of two, roughly" algorithm, |
156 | Convert::Scalar will use a "size to next power of two, roughly" algorithm, |
157 | so as the scalar grows, perl will have to resize and copy it less and less |
157 | so as the scalar grows, perl will have to resize and copy it less and less |
158 | often. |
158 | often. |
|
|
159 | |
|
|
160 | =item nread = extend_read fh, scalar, addlen=64 |
|
|
161 | |
|
|
162 | Calls C<extend scalar, addlen> to ensure some space is available, then |
|
|
163 | do the equivalent of C<sysread> to the end, to try to fill the extra |
|
|
164 | space. Returns how many bytes have been read, C<0> on EOF or undef> on |
|
|
165 | eror, just like C<sysread>. |
|
|
166 | |
|
|
167 | This function is useful to implement many protocols where you read some |
|
|
168 | data, see if it is enough to decode, and if not, read some more, where the |
|
|
169 | naive or easy way of doing this would result in bad performance. |
|
|
170 | |
|
|
171 | =item nread = read_all fh, scalar, length |
|
|
172 | |
|
|
173 | Tries to read C<length> bytes into C<scalar>. Unlike C<read> or |
|
|
174 | C<sysread>, it will try to read more bytes if not all bytes could be read |
|
|
175 | in one go (this is often called C<xread> in C). |
|
|
176 | |
|
|
177 | Returns the total nunmber of bytes read (normally C<length>, unless an |
|
|
178 | error or EOF occured), C<0> on EOF and C<undef> on errors. |
|
|
179 | |
|
|
180 | =item nwritten = write_all fh, scalar |
|
|
181 | |
|
|
182 | Like C<readall>, but for writes - the equivalent of the C<xwrite> function |
|
|
183 | often seen in C. |
159 | |
184 | |
160 | =item refcnt scalar[, newrefcnt] |
185 | =item refcnt scalar[, newrefcnt] |
161 | |
186 | |
162 | Returns the current reference count of the given scalar and optionally sets it to |
187 | Returns the current reference count of the given scalar and optionally sets it to |
163 | the given reference count. |
188 | the given reference count. |