Chapter 8
Creation
You can create a new RV using the newRV_inc() function:
SV *sv = newSVpv("foo",0); // $sv = "foo";
SV *rv = newRV_inc(sv); // $rv = \$sv;
This function officially takes an SV* as a parameter, but it can actually be used with
any Perl type that you can cast to an SV* (such as an AV* or an HV*). This pattern is
repeated across the entire RV API instead of having separate functions for SV, AV,
and HV references, there is a single API, and you must cast everything to and from
SV*. For example, the following creates the hash of arrays data structure shown earlier:
HV *hash_of_arrays_hv = newHV();
AV *foo_av = newAV();
AV *bar_av = newAV();
// fill in arrays
push_av(foo_av, newSViv(1));
push_av(foo_av, newSViv(2));
push_av(foo_av, newSViv(3));
push_av(bar_av, newSViv(4));
push_av(bar_av, newSViv(5));
push_av(bar_av, newSViv(6));
// create references and assign to hash
hv_store(hash_of_arrays_hv, "foo", 3, newRV_inc((SV*)foo_av), 0);
hv_store(hash_of_arrays_hv, "bar", 3, newRV_inc((SV*)bar_av), 0);
Once created, an RV can be distinguished from a normal SV using the SvROK
macro. For example, this code would print ok twice after the preceding code:
if (SvROK(*(hv_fetch(hash_of_arrays_hv, "foo", 3, 0)))) printf("ok\n");
if (SvROK(*(hv_fetch(hash_of_arrays_hv, "bar", 3, 0)))) printf("ok\n");
Another way to create a reference is to use one of the sv_setref functions.
These functions take an initialized SV and one of the value types (IV, UV, and so on)
and creates a new SV. They then make the SV passed as an argument a reference to
the new SV. Here are some examples:
190
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