From: Marcus Holland-Moritz Date: Thu, 31 Jul 2003 22:39:01 +0000 (+0200) Subject: [DOC PATCH] pod/perlguts.pod X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a938121844a85ea4fc6c37d2a1e11ff1930e084d;p=p5sagit%2Fp5-mst-13.2.git [DOC PATCH] pod/perlguts.pod From: "Marcus Holland-Moritz" Message-ID: <004a01c357a3$c71f9f50$0c2f1fac@R2D2> p4raw-id: //depot/perl@20388 --- 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