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 38d7569..ce8c182 100644 (file)
@@ -89,16 +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>, though
-this is rather less efficient than using a local variable.  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:
 
@@ -263,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:
@@ -1025,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:
 
@@ -1236,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.
@@ -1503,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
 
@@ -2171,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
@@ -2183,7 +2199,8 @@ the type.  Can do overlapping moves.  See also C<Copy>.
 
 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
-declare a local variable and use that instead.
+either declare a local variable and use that instead or to use the C<SvPV_nolen>
+macro.
 
 =item New
 
@@ -2854,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)
 
@@ -2952,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)
 
@@ -3013,7 +3030,7 @@ Checks the B<private> setting.  Use C<SvPOK>.
 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, int len )
+       char*   SvPV (SV* sv, int len)
 
 =item SvPV_force
 
@@ -3022,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
 
@@ -3389,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)