Chapter 8
Setting Values
Given an initialized SV, you can load it with any of the value types using the sv_set*
family of functions:
sv_setiv(sv, 10); // sv contains the signed integer (IV) 10
sv_setuv(sv, 10); // sv contains the unsigned integer (UV) 10
sv_setnv(sv, 10.5); // sv contains the unsigned integer (UV) 10
sv_setpv(sv, "10"); // sv contains the string value (PV) "10"
The PV forms also come in a few more flavors. There's one that uses an
sprintf() style format string:
sv_setpvf(sv, "ten: %d", 10); // sv contains the string value (PV) "ten: 10"
and one that takes a length argument to allow for strings that aren't null termi
nated or that contain null bytes:
sv_setpvn(sv, "10", 2); // sets sv to the 2 character string "10"
Direct Access
If you know the type of an SV, then you can directly access the underlying value
type using the an Sv*X macro. This is useful for two reasons it is faster since it
avoids testing the type of the data, and it is lvaluable. In C, a macro is said to be
lvaluable if it may legally be used as an lvalue. The most common lvalue is the left
hand side of an assignment operator. Sv*X allows you to efficiently set the value of
an SV without needing to call a function.
SvIVX(sv_iv) = 100; // directly set the IV inside sv_iv to 100
SvUVX(sv_uv) = 100; // directly set the UV inside sv_uv to 100
SvNVX(sv_nv) = 100.10; // directly set the NV inside sv_nv to 100.5
warn("PV: %s", SvPVX(sv_pv)); // directly access the string inside sv_pv
Note that you cannot safely use the return value from SvPVX() as an lvalue
doing so would change the string pointed to by the SV and would cause an instant
memory leak. Other bad things would happen too, because the SV structure keeps
track of more than just the pointer to the string it also tracks the length and an
offset into the string buffer where the string begins.
180
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