This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
provide SvPV_nolen(sv) to avoid use of PL_na
[perl5.git] / pod / perlguts.pod
index ec48594..ce8c182 100644 (file)
@@ -89,15 +89,19 @@ To access the actual value that an SV points to, you can use the macros:
     SvIV(SV*)
     SvNV(SV*)
     SvPV(SV*, STRLEN len)
+    SvPV_nolen(SV*)
 
 which will automatically coerce the actual scalar type into an IV, double,
 or string.
 
 In the C<SvPV> macro, the length of the string returned is placed into the
-variable C<len> (this is a macro, so you do I<not> use C<&len>).  If you do not
-care what the length of the data is, use the global variable C<PL_na>.  Remember,
-however, that Perl allows arbitrary strings of data that may both contain
-NULs and might not be terminated by a NUL.
+variable C<len> (this is a macro, so you do I<not> use C<&len>).  If you do
+not care what the length of the data is, use the C<SvPV_nolen> macro.
+Historically the C<SvPV> macro with the global variable C<PL_na> has been
+used in this case.  But that can be quite inefficient because C<PL_na> must
+be accessed in thread-local storage in threaded Perl.  In any case, remember
+that Perl allows arbitrary strings of data that may both contain NULs and
+might not be terminated by a NUL.
 
 If you want to know if the scalar value is TRUE, you can use:
 
@@ -262,9 +266,9 @@ return value.
 The C<av_clear> function deletes all the elements in the AV* array, but
 does not actually delete the array itself.  The C<av_undef> function will
 delete all the elements in the array plus the array itself.  The
-C<av_extend> function extends the array so that it contains C<key>
-elements.  If C<key> is less than the current length of the array, then
-nothing is done.
+C<av_extend> function extends the array so that it contains at least C<key+1>
+elements.  If C<key+1> is less than the currently allocated length of the array,
+then nothing is done.
 
 If you know the name of an array variable, you can get a pointer to its AV
 by using the following:
@@ -350,11 +354,13 @@ This returns NULL if the variable does not exist.
 
 The hash algorithm is defined in the C<PERL_HASH(hash, key, klen)> macro:
 
-    i = klen;
     hash = 0;
-    s = key;
-    while (i--)
-       hash = hash * 33 + *s++;
+    while (klen--)
+       hash = (hash * 33) + *key++;
+    hash = hash + (hash >> 5);                 /* after 5.006 */
+
+The last step was added in version 5.006 to improve distribution of
+lower bits in the resulting hash value.
 
 See L<Understanding the Magic of Tied Hashes and Arrays> for more
 information on how to use the hash access functions on tied hashes.
@@ -1022,13 +1028,13 @@ There is a way to achieve a similar task from C via Perl API: create a
 I<pseudo-block>, and arrange for some changes to be automatically
 undone at the end of it, either explicit, or via a non-local exit (via
 die()). A I<block>-like construct is created by a pair of
-C<ENTER>/C<LEAVE> macros (see L<perlcall/EXAMPLE/"Returning a
-Scalar">).  Such a construct may be created specially for some
-important localized task, or an existing one (like boundaries of
-enclosing Perl subroutine/block, or an existing pair for freeing TMPs)
-may be used. (In the second case the overhead of additional
-localization must be almost negligible.) Note that any XSUB is
-automatically enclosed in an C<ENTER>/C<LEAVE> pair.
+C<ENTER>/C<LEAVE> macros (see L<perlcall/"Returning a Scalar">).
+Such a construct may be created specially for some important localized
+task, or an existing one (like boundaries of enclosing Perl
+subroutine/block, or an existing pair for freeing TMPs) may be
+used. (In the second case the overhead of additional localization must
+be almost negligible.) Note that any XSUB is automatically enclosed in
+an C<ENTER>/C<LEAVE> pair.
 
 Inside such a I<pseudo-block> the following service is available:
 
@@ -1233,7 +1239,12 @@ consult L<perlcall>.
 
 =head2 Memory Allocation
 
-It is suggested that you use the version of malloc that is distributed
+All memory meant to be used with the Perl API functions should be manipulated
+using the macros described in this section.  The macros provide the necessary
+transparency between differences in the actual malloc implementation that is
+used within perl.
+
+It is suggested that you enable the version of malloc that is distributed
 with Perl.  It keeps pools of various sizes of unallocated memory in
 order to satisfy allocation requests more quickly.  However, on some
 platforms, it may cause spurious malloc or free errors.
@@ -1500,7 +1511,7 @@ It is strongly recommended that all Perl API functions that don't begin
 with C<perl> be referenced with an explicit C<Perl_> prefix.
 
 The sort order of the listing is case insensitive, with any
-occurrences of '_' ignored for the the purpose of sorting.
+occurrences of '_' ignored for the purpose of sorting.
 
 =over 8
 
@@ -1634,7 +1645,7 @@ the SV which holds the name of the sub being debugged.  This is the C
 variable which corresponds to Perl's $DB::sub variable.  See C<PL_DBsingle>.
 The sub name can be found by
 
-       SvPV( GvSV( PL_DBsub ), PL_na )
+       SvPV( GvSV( PL_DBsub ), len )
 
 =item PL_DBtrace
 
@@ -1854,7 +1865,8 @@ Returns the key slot of the hash entry as a C<char*> value, doing any
 necessary dereferencing of possibly C<SV*> keys.  The length of
 the string is placed in C<len> (this is a macro, so do I<not> use
 C<&len>).  If you do not care about what the length of the key is,
-you may use the global variable C<PL_na>.  Remember though, that hash
+you may use the global variable C<PL_na>, though this is rather less
+efficient than using a local variable.  Remember though, that hash
 keys in perl are free to contain embedded nulls, so using C<strlen()>
 or similar is not a good way to find the length of hash keys.
 This is very similar to the C<SvPV()> macro described elsewhere in
@@ -1895,15 +1907,6 @@ Clears a hash, making it empty.
 
        void    hv_clear (HV* tb)
 
-=item hv_delayfree_ent
-
-Releases a hash entry, such as while iterating though the hash, but
-delays actual freeing of key and value until the end of the current
-statement (or thereabouts) with C<sv_2mortal>.  See C<hv_iternext>
-and C<hv_free_ent>.
-
-       void    hv_delayfree_ent (HV* hv, HE* entry)
-
 =item hv_delete
 
 Deletes a key/value pair in the hash.  The value SV is removed from the hash
@@ -1963,13 +1966,6 @@ information on how to use this function on tied hashes.
 
        HE*     hv_fetch_ent  (HV* tb, SV* key, I32 lval, U32 hash)
 
-=item hv_free_ent
-
-Releases a hash entry, such as while iterating though the hash.  See
-C<hv_iternext> and C<hv_delayfree_ent>.
-
-       void    hv_free_ent (HV* hv, HE* entry)
-
 =item hv_iterinit
 
 Prepares a starting point to traverse a hash table.
@@ -2183,6 +2179,14 @@ Do magic after a value is assigned to the SV.  See C<sv_magic>.
 
        int     mg_set (SV* sv)
 
+=item modglobal
+
+C<modglobal> is a general purpose, interpreter global HV for use by
+extensions that need to keep information on a per-interpreter basis.
+In a pinch, it can also be used as a symbol table for extensions
+to share data among each other.  It is a good idea to use keys
+prefixed by the package name of the extension that owns the data.
+
 =item Move
 
 The XSUB-writer's interface to the C C<memmove> function.  The C<s> is the
@@ -2193,8 +2197,10 @@ the type.  Can do overlapping moves.  See also C<Copy>.
 
 =item PL_na
 
-A variable which may be used with C<SvPV> to tell Perl to calculate the
-string length.
+A convenience variable which is typically used with C<SvPV> when one doesn't
+care about the length of the string.  It is usually more efficient to
+either declare a local variable and use that instead or to use the C<SvPV_nolen>
+macro.
 
 =item New
 
@@ -2794,7 +2800,7 @@ indicated number of bytes (remember to reserve space for an extra
 trailing NUL character).  Calls C<sv_grow> to perform the expansion if
 necessary.  Returns a pointer to the character buffer.
 
-       char*   SvGROW( SV* sv, int len )
+       char*   SvGROW( SV* sv, STRLEN len )
 
 =item sv_grow
 
@@ -2865,13 +2871,13 @@ will return false.
 
 =item SvIV
 
-Returns the integer which is in the SV.
+Coerces the given SV to an integer and returns it.
 
        int SvIV (SV* sv)
 
 =item SvIVX
 
-Returns the integer which is stored in the SV.
+Returns the integer which is stored in the SV, assuming SvIOK is true.
 
        int     SvIVX (SV* sv)
 
@@ -2963,13 +2969,13 @@ B<private> setting.  Use C<SvNOK>.
 
 =item SvNV
 
-Returns the double which is stored in the SV.
+Coerce the given SV to a double and return it.
 
        double  SvNV (SV* sv)
 
 =item SvNVX
 
-Returns the double which is stored in the SV.
+Returns the double which is stored in the SV, assuming SvNOK is true.
 
        double  SvNVX (SV* sv)
 
@@ -3022,10 +3028,9 @@ Checks the B<private> setting.  Use C<SvPOK>.
 =item SvPV
 
 Returns a pointer to the string in the SV, or a stringified form of the SV
-if the SV does not contain a string.  If C<len> is C<PL_na> then Perl will
-handle the length on its own.  Handles 'get' magic.
+if the SV does not contain a string.  Handles 'get' magic.
 
-       char*   SvPV (SV* sv, int len )
+       char*   SvPV (SV* sv, int len)
 
 =item SvPV_force
 
@@ -3034,6 +3039,12 @@ want force if you are going to update the SvPVX directly.
 
        char*   SvPV_force(SV* sv, int len)
 
+=item SvPV_nolen
+
+Returns a pointer to the string in the SV, or a stringified form of the SV
+if the SV does not contain a string.  Handles 'get' magic.
+
+       char*   SvPV (SV* sv)
 
 =item SvPVX
 
@@ -3401,13 +3412,13 @@ appending it.
 
 =item SvUV
 
-Returns the unsigned integer which is in the SV.
+Coerces the given SV to an unsigned integer and returns it.
 
        UV      SvUV(SV* sv)
 
 =item SvUVX
 
-Returns the unsigned integer which is stored in the SV.
+Returns the unsigned integer which is stored in the SV, assuming SvIOK is true.
 
        UV      SvUVX(SV* sv)