X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pod%2Fperlguts.pod;h=b763dfea9aae558a6df774520e5267c36589689a;hb=376270a4000241675d2fdcb89a2b30a7bb0e3695;hp=9a411e15975650fd1def80f57835cf1a083b27b5;hpb=06f6df1768f7810ef4b2d5d7821dcccb9c7bf9e4;p=p5sagit%2Fp5-mst-13.2.git diff --git a/pod/perlguts.pod b/pod/perlguts.pod index 9a411e1..b763dfe 100644 --- a/pod/perlguts.pod +++ b/pod/perlguts.pod @@ -202,6 +202,8 @@ you can call: The scalar C value is stored in an SV instance called C. Its address can be used whenever an C is needed. +However, you have to be careful when using C<&PL_sv_undef> as a value in AVs +or HVs (see L). There are also the two values C and C, which contain boolean TRUE and FALSE values, respectively. Like C, their @@ -489,6 +491,58 @@ reference count of the stored C, which is the caller's responsibility. If these functions return a NULL value, the caller will usually have to decrement the reference count of C to avoid a memory leak. +=head2 AVs, HVs and undefined values + +Sometimes you have to store undefined values in AVs or HVs. Although +this may be a rare case, it can be tricky. That's because you're +used to using C<&PL_sv_undef> if you need an undefined SV. + +For example, intuition tells you that this XS code: + + AV *av = newAV(); + av_store( av, 0, &PL_sv_undef ); + +is equivalent to this Perl code: + + my @av; + $av[0] = undef; + +Unfortunately, this isn't true. AVs use C<&PL_sv_undef> as a marker +for indicating that an array element has not yet been initialized. +Thus, C would be true for the above Perl code, but +false for the array generated by the XS code. + +Other problems can occur when storing C<&PL_sv_undef> in HVs: + + hv_store( hv, "key", 3, &PL_sv_undef, 0 ); + +This will indeed make the value C, but if you try to modify +the value of C, you'll get the following error: + + Modification of non-creatable hash value attempted + +In perl 5.8.0, C<&PL_sv_undef> was also used to mark placeholders +in restricted hashes. This caused such hash entries not to appear +when iterating over the hash or when checking for the keys +with the C function. + +You can run into similar problems when you store C<&PL_sv_true> or +C<&PL_sv_false> into AVs or HVs. Trying to modify such elements +will give you the following error: + + Modification of a read-only value attempted + +To make a long story short, you can use the special variables +C<&PL_sv_undef>, C<&PL_sv_true> and C<&PL_sv_false> with AVs and +HVs, but you have to make sure you know what you're doing. + +Generally, if you want to store an undefined value in an AV +or HV, you should not use C<&PL_sv_undef>, but rather create a +new undefined value using the C function, for example: + + av_store( av, 42, newSV(0) ); + hv_store( hv, "foo", 3, newSV(0), 0 ); + =head2 References References are a special type of scalar that point to other data types