Programming Perl in C
CAUTION Be careful with Sv*X macros; if you use one without first
checking that the SV is of the correct type, you might get a segmentation
fault, or worse, silently corrupt nearby data in memory!
After using an Sv*X macro to update the value inside an SV, it's often necessary
to update the type information of the SV. This is because SVs will cache conversion
values when converting between types. You need to tell the SV to invalidate any
other cached representations using a macro of the form Sv*OK_only(). For example:
SvIVX(sv_iv) = 100; // directly set the IV inside sv_iv to 100
SvIOK_only(sv_iv); // invalidate non IV representations inside sv_iv
In general it is better to use the sv_set functions rather than Sv*X macros.
However, in some cases the performance improvement can make it worth the risk.
String Functions
Just like Perl, the Perl API contains functionality to make string processing easier.
There are a set of functions for string concatenation:
sv_catpv(sv, "foo"); // adds "foo" to the end of sv
sv_catpvn(sv, "foo", 3); // adds "foo" to the end of sv, with a length arg
sv_catpvf(sv, "ten: %d", 10); // adds "ten: 10" to the end of sv
sv_catsv(sv_to, sv_from); // adds the contents of sv_from to the
// end of sv_to
Getting the length of an SV is done as follows:
STRLEN len = sv_len(sv);
If you want to grow the size of the string, do the following:
char *new_ptr = sv_grow(sv, 1024); // grows sv to 1k and returns a pointer to
// the new character buffer
Truncate the string in this manner:
SvCUR_set(sv, 10); // the SV is now 10 bytes long
Inserting a string into the middle of an SV, similar to the substr built in in Perl,
is done as follows:
18
181
1
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved