fix some misinformation in perlfunc.pod
[p5sagit/p5-mst-13.2.git] / pod / perlguts.pod
index f38bba3..3b10af9 100644 (file)
@@ -247,6 +247,15 @@ portion of the string between the "real" and the "fake" beginnings is
 shown in parentheses, and the values of C<SvCUR> and C<SvLEN> reflect
 the fake beginning, not the real one.
 
+Something similar to the offset hack is perfomed on AVs to enable
+efficient shifting and splicing off the beginning of the array; while
+C<AvARRAY> points to the first element in the array that is visible from
+Perl, C<AvALLOC> points to the real start of the C array. These are
+usually the same, but a C<shift> operation can be carried out by
+increasing C<AvARRAY> by one and decreasing C<AvFILL> and C<AvLEN>.
+Again, the location of the real start of the C array only comes into
+play when freeing the array. See C<av_shift> in F<av.c>.
+
 =head2 What's Really Stored in an SV?
 
 Recall that the usual method of determining the type of scalar you have is
@@ -530,10 +539,11 @@ class.  SV is returned.
 
        SV* newSVrv(SV* rv, const char* classname);
 
-Copies integer or double into an SV whose reference is C<rv>.  SV is blessed
+Copies integer, unsigned integer or double into an SV whose reference is C<rv>.  SV is blessed
 if C<classname> is non-null.
 
        SV* sv_setref_iv(SV* rv, const char* classname, IV iv);
+       SV* sv_setref_uv(SV* rv, const char* classname, UV uv);
        SV* sv_setref_nv(SV* rv, const char* classname, NV iv);
 
 Copies the pointer value (I<the address, not the string!>) into an SV whose
@@ -1117,8 +1127,20 @@ and back.
 =item C<SAVEFREESV(SV *sv)>
 
 The refcount of C<sv> would be decremented at the end of
-I<pseudo-block>. This is similar to C<sv_2mortal>, which should (?) be
-used instead.
+I<pseudo-block>.  This is similar to C<sv_2mortal> in that it is also a
+mechanism for doing a delayed C<SvREFCNT_dec>.  However, while C<sv_2mortal>
+extends the lifetime of C<sv> until the beginning of the next statement,
+C<SAVEFREESV> extends it until the end of the enclosing scope.  These
+lifetimes can be wildly different.
+
+Also compare C<SAVEMORTALIZESV>.
+
+=item C<SAVEMORTALIZESV(SV *sv)>
+
+Just like C<SAVEFREESV>, but mortalizes C<sv> at the end of the current
+scope instead of decrementing its reference count.  This usually has the
+effect of keeping C<sv> alive until the statement that called the currently
+live scope has finished executing.
 
 =item C<SAVEFREEOP(OP *op)>
 
@@ -1254,6 +1276,7 @@ to use the macros:
 
 These macros automatically adjust the stack for you, if needed.  Thus, you
 do not need to call C<EXTEND> to extend the stack.
+However, see L</Putting a C value on Perl stack>
 
 For more information, consult L<perlxs> and L<perlxstut>.
 
@@ -1354,21 +1377,6 @@ destination starting points.  Perl will move, copy, or zero out C<number>
 instances of the size of the C<type> data structure (using the C<sizeof>
 function).
 
-Here is a handy table of equivalents between ordinary C and Perl's
-memory abstraction layer:
-
-    Instead Of:                Use:
-
-    malloc                     New
-    calloc                     Newz
-    realloc                    Renew
-    memcopy                    Copy
-    memmove                    Move
-    free                       Safefree
-    strdup                     savepv
-    strndup                    savepvn (Hey, strndup doesn't exist!)
-    memcpy/*(struct foo *)    StructCopy
-
 =head2 PerlIO
 
 The most recent development releases of Perl has been experimenting with
@@ -1398,6 +1406,23 @@ The macro to put this target on stack is C<PUSHTARG>, and it is
 directly used in some opcodes, as well as indirectly in zillions of
 others, which use it via C<(X)PUSH[pni]>.
 
+Because the target is reused, you must be careful when pushing multiple
+values on the stack. The following code will not do what you think:
+
+    XPUSHi(10);
+    XPUSHi(20);
+
+This translates as "set C<TARG> to 10, push a pointer to C<TARG> onto
+the stack; set C<TARG> to 20, push a pointer to C<TARG> onto the stack".
+At the end of the operation, the stack does not contain the values 10
+and 20, but actually contains two pointers to C<TARG>, which we have set
+to 20. If you need to push multiple different values, use C<XPUSHs>,
+which bypasses C<TARG>.
+
+On a related note, if you do use C<(X)PUSH[npi]>, then you're going to
+need a C<dTARG> in your variable declarations so that the C<*PUSH*>
+macros can make use of the local variable C<TARG>. 
+
 =head2 Scratchpads
 
 The question remains on when the SVs which are I<target>s for opcodes
@@ -1651,8 +1676,8 @@ interpreter.
 Three macros control the major Perl build flavors: MULTIPLICITY,
 USE_THREADS and PERL_OBJECT.  The MULTIPLICITY build has a C structure
 that packages all the interpreter state, there is a similar thread-specific
-data structure under USE_THREADS, and the PERL_OBJECT build has a C++
-class to maintain interpreter state.  In all three cases,
+data structure under USE_THREADS, and the (now deprecated) PERL_OBJECT
+build has a C++ class to maintain interpreter state.  In all three cases,
 PERL_IMPLICIT_CONTEXT is also normally defined, and enables the
 support for passing in a "hidden" first argument that represents all three
 data structures.
@@ -1695,10 +1720,11 @@ C<pTHX_> is one of a number of macros (in perl.h) that hide the
 details of the interpreter's context.  THX stands for "thread", "this",
 or "thingy", as the case may be.  (And no, George Lucas is not involved. :-)
 The first character could be 'p' for a B<p>rototype, 'a' for B<a>rgument,
-or 'd' for B<d>eclaration.
+or 'd' for B<d>eclaration, so we have C<pTHX>, C<aTHX> and C<dTHX>, and
+their variants.
 
-When Perl is built without PERL_IMPLICIT_CONTEXT, there is no first
-argument containing the interpreter's context.  The trailing underscore
+When Perl is built without options that set PERL_IMPLICIT_CONTEXT, there is no
+first argument containing the interpreter's context.  The trailing underscore
 in the pTHX_ macro indicates that the macro expansion needs a comma
 after the context argument because other arguments follow it.  If
 PERL_IMPLICIT_CONTEXT is not defined, pTHX_ will be ignored, and the
@@ -1707,14 +1733,14 @@ macro without the trailing underscore is used when there are no additional
 explicit arguments.
 
 When a core function calls another, it must pass the context.  This
-is normally hidden via macros.  Consider C<sv_setsv>.  It expands
+is normally hidden via macros.  Consider C<sv_setsv>.  It expands into
 something like this:
 
     ifdef PERL_IMPLICIT_CONTEXT
-      define sv_setsv(a,b)     Perl_sv_setsv(aTHX_ a, b)
+      define sv_setsv(a,b)      Perl_sv_setsv(aTHX_ a, b)
       /* can't do this for vararg functions, see below */
     else
-      define sv_setsv          Perl_sv_setsv
+      define sv_setsv           Perl_sv_setsv
     endif
 
 This works well, and means that XS authors can gleefully write:
@@ -1734,8 +1760,8 @@ Under PERL_OBJECT in the core, that will translate to either:
                                        # see objXSUB.h
 
 Under PERL_OBJECT in extensions (aka PERL_CAPI), or under
-MULTIPLICITY/USE_THREADS w/ PERL_IMPLICIT_CONTEXT in both core
-and extensions, it will be:
+MULTIPLICITY/USE_THREADS with PERL_IMPLICIT_CONTEXT in both core
+and extensions, it will become:
 
     Perl_sv_setsv(aTHX_ foo, bar);     # the canonical Perl "API"
                                        # for all build flavors
@@ -1757,6 +1783,14 @@ You can ignore [pad]THX[xo] when browsing the Perl headers/sources.
 Those are strictly for use within the core.  Extensions and embedders
 need only be aware of [pad]THX.
 
+=head2 So what happened to dTHR?
+
+C<dTHR> was introduced in perl 5.005 to support the older thread model.
+The older thread model now uses the C<THX> mechanism to pass context
+pointers around, so C<dTHR> is not useful any more.  Perl 5.6.0 and
+later still have it for backward source compatibility, but it is defined
+to be a no-op.
+
 =head2 How do I use all this in extensions?
 
 When Perl is built with PERL_IMPLICIT_CONTEXT, extensions that call
@@ -1789,31 +1823,31 @@ work.
 The second, more efficient way is to use the following template for
 your Foo.xs:
 
-       #define PERL_NO_GET_CONTEXT     /* we want efficiency */
-       #include "EXTERN.h"
-       #include "perl.h"
-       #include "XSUB.h"
+        #define PERL_NO_GET_CONTEXT     /* we want efficiency */
+        #include "EXTERN.h"
+        #include "perl.h"
+        #include "XSUB.h"
 
         static my_private_function(int arg1, int arg2);
 
-       static SV *
-       my_private_function(int arg1, int arg2)
-       {
-           dTHX;       /* fetch context */
-           ... call many Perl API functions ...
-       }
+        static SV *
+        my_private_function(int arg1, int arg2)
+        {
+            dTHX;       /* fetch context */
+            ... call many Perl API functions ...
+        }
 
         [... etc ...]
 
-       MODULE = Foo            PACKAGE = Foo
+        MODULE = Foo            PACKAGE = Foo
 
-       /* typical XSUB */
+        /* typical XSUB */
 
-       void
-       my_xsub(arg)
-               int arg
-           CODE:
-               my_private_function(arg, 10);
+        void
+        my_xsub(arg)
+                int arg
+            CODE:
+                my_private_function(arg, 10);
 
 Note that the only two changes from the normal way of writing an
 extension is the addition of a C<#define PERL_NO_GET_CONTEXT> before
@@ -1828,32 +1862,32 @@ The third, even more efficient way is to ape how it is done within
 the Perl guts:
 
 
-       #define PERL_NO_GET_CONTEXT     /* we want efficiency */
-       #include "EXTERN.h"
-       #include "perl.h"
-       #include "XSUB.h"
+        #define PERL_NO_GET_CONTEXT     /* we want efficiency */
+        #include "EXTERN.h"
+        #include "perl.h"
+        #include "XSUB.h"
 
         /* pTHX_ only needed for functions that call Perl API */
         static my_private_function(pTHX_ int arg1, int arg2);
 
-       static SV *
-       my_private_function(pTHX_ int arg1, int arg2)
-       {
-           /* dTHX; not needed here, because THX is an argument */
-           ... call Perl API functions ...
-       }
+        static SV *
+        my_private_function(pTHX_ int arg1, int arg2)
+        {
+            /* dTHX; not needed here, because THX is an argument */
+            ... call Perl API functions ...
+        }
 
         [... etc ...]
 
-       MODULE = Foo            PACKAGE = Foo
+        MODULE = Foo            PACKAGE = Foo
 
-       /* typical XSUB */
+        /* typical XSUB */
 
-       void
-       my_xsub(arg)
-               int arg
-           CODE:
-               my_private_function(aTHX_ arg, 10);
+        void
+        my_xsub(arg)
+                int arg
+            CODE:
+                my_private_function(aTHX_ arg, 10);
 
 This implementation never has to fetch the context using a function
 call, since it is always passed as an extra argument.  Depending on
@@ -1864,15 +1898,34 @@ Never add a comma after C<pTHX> yourself--always use the form of the
 macro with the underscore for functions that take explicit arguments,
 or the form without the argument for functions with no explicit arguments.
 
+=head2 Should I do anything special if I call perl from multiple threads?
+
+If you create interpreters in one thread and then proceed to call them in
+another, you need to make sure perl's own Thread Local Storage (TLS) slot is
+initialized correctly in each of those threads.
+
+The C<perl_alloc> and C<perl_clone> API functions will automatically set
+the TLS slot to the interpreter they created, so that there is no need to do
+anything special if the interpreter is always accessed in the same thread that
+created it, and that thread did not create or call any other interpreters
+afterwards.  If that is not the case, you have to set the TLS slot of the
+thread before calling any functions in the Perl API on that particular
+interpreter.  This is done by calling the C<PERL_SET_CONTEXT> macro in that
+thread as the first thing you do:
+
+       /* do this before doing anything else with some_perl */
+       PERL_SET_CONTEXT(some_perl);
+
+       ... other Perl API calls on some_perl go here ...
+
 =head2 Future Plans and PERL_IMPLICIT_SYS
 
 Just as PERL_IMPLICIT_CONTEXT provides a way to bundle up everything
 that the interpreter knows about itself and pass it around, so too are
 there plans to allow the interpreter to bundle up everything it knows
 about the environment it's running on.  This is enabled with the
-PERL_IMPLICIT_SYS macro.  Currently it only works with PERL_OBJECT,
-but is mostly there for MULTIPLICITY and USE_THREADS (see inside
-iperlsys.h).
+PERL_IMPLICIT_SYS macro.  Currently it only works with PERL_OBJECT
+and USE_THREADS on Windows (see inside iperlsys.h).
 
 This allows the ability to provide an extra pointer (called the "host"
 environment) for all the system calls.  This makes it possible for
@@ -1932,7 +1985,8 @@ Other available flags are:
 
 =item s
 
-This is a static function and is defined as C<S_whatever>.
+This is a static function and is defined as C<S_whatever>, and usually
+called within the sources as C<whatever(...)>.
 
 =item n
 
@@ -1950,7 +2004,7 @@ The argument list should end with C<...>, like this:
 
     Afprd   |void   |croak          |const char* pat|...
 
-=item m
+=item M
 
 This function is part of the experimental development API, and may change 
 or disappear without notice.
@@ -1980,18 +2034,18 @@ If you are printing IVs, UVs, or NVS instead of the stdio(3) style
 formatting codes like C<%d>, C<%ld>, C<%f>, you should use the
 following macros for portability
 
-       IVdf            IV in decimal
-       UVuf            UV in decimal
-       UVof            UV in octal
-       UVxf            UV in hexadecimal
-       NVef            NV %e-like
-       NVff            NV %f-like
-       NVgf            NV %g-like
+        IVdf            IV in decimal
+        UVuf            UV in decimal
+        UVof            UV in octal
+        UVxf            UV in hexadecimal
+        NVef            NV %e-like
+        NVff            NV %f-like
+        NVgf            NV %g-like
 
 These will take care of 64-bit integers and long doubles.
 For example:
 
-       printf("IV is %"IVdf"\n", iv);
+        printf("IV is %"IVdf"\n", iv);
 
 The IVdf will expand to whatever is the correct format for the IVs.
 
@@ -2003,20 +2057,20 @@ with PTR2UV(), do not use %lx or %p.
 Because pointer size does not necessarily equal integer size,
 use the follow macros to do it right.
 
-       PTR2UV(pointer)
-       PTR2IV(pointer)
-       PTR2NV(pointer)
-       INT2PTR(pointertotype, integer)
+        PTR2UV(pointer)
+        PTR2IV(pointer)
+        PTR2NV(pointer)
+        INT2PTR(pointertotype, integer)
 
 For example:
 
-       IV  iv = ...;
-       SV *sv = INT2PTR(SV*, iv);
+        IV  iv = ...;
+        SV *sv = INT2PTR(SV*, iv);
 
 and
 
-       AV *av = ...;
-       UV  uv = PTR2UV(av);
+        AV *av = ...;
+        UV  uv = PTR2UV(av);
 
 =head2 Source Documentation