home *** CD-ROM | disk | FTP | other *** search
/ Amiga Magazin: Amiga-CD 1996 July / AMIGA_1996_7.BIN / ausgabe_7_96 / pd-programmierung / perl5_002bin.lha / man / catp / perlcall.0 < prev    next >
Text File  |  1996-03-02  |  136KB  |  2,311 lines

  1.  
  2.  
  3.  
  4. PERLCALL(1)    User Contributed Perl Documentation    PERLCALL(1)
  5.  
  6.  
  7. NNNNAAAAMMMMEEEE
  8.        perlcall - Perl calling conventions from C
  9.  
  10. DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  11.        The purpose of this document is to show you how to call
  12.        Perl subroutines directly from C, i.e. how to write
  13.        _c_a_l_l_b_a_c_k_s.
  14.  
  15.        Apart from discussing the C interface provided by Perl for
  16.        writing callbacks the document uses a series of examples
  17.        to show how the interface actually works in practice.  In
  18.        addition some techniques for coding callbacks are covered.
  19.  
  20.        Examples where callbacks are necessary include
  21.  
  22.        +o An Error Handler
  23.             You have created an XSUB interface to an
  24.             application's C API.
  25.  
  26.             A fairly common feature in applications is to allow
  27.             you to define a C function that will be called
  28.             whenever something nasty occurs. What we would like
  29.             is to be able to specify a Perl subroutine that will
  30.             be called instead.
  31.  
  32.        +o An Event Driven Program
  33.             The classic example of where callbacks are used is
  34.             when writing an event driven program like for an X
  35.             windows application.  In this case your register
  36.             functions to be called whenever specific events
  37.             occur, e.g. a mouse button is pressed, the cursor
  38.             moves into a window or a menu item is selected.
  39.  
  40.        Although the techniques described here are applicable when
  41.        embedding Perl in a C program, this is not the primary
  42.        goal of this document.  There are other details that must
  43.        be considered and are specific to embedding Perl. For
  44.        details on embedding Perl in C refer to the _p_e_r_l_e_m_b_e_d
  45.        manpage.
  46.  
  47.        Before you launch yourself head first into the rest of
  48.        this document, it would be a good idea to have read the
  49.        following two documents - the _p_e_r_l_x_s manpage and the
  50.        _p_e_r_l_g_u_t_s manpage.
  51.  
  52. TTTTHHHHEEEE PPPPEEEERRRRLLLL____CCCCAAAALLLLLLLL FFFFUUUUNNNNCCCCTTTTIIIIOOOONNNNSSSS
  53.        Although this stuff is easier to explain using examples,
  54.        you first need be aware of a few important definitions.
  55.  
  56.        Perl has a number of C functions that allow you to call
  57.        Perl subroutines.  They are
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64. 29/Jan/96                perl 5.002 with                        1
  65.  
  66.  
  67.  
  68.  
  69.  
  70. PERLCALL(1)    User Contributed Perl Documentation    PERLCALL(1)
  71.  
  72.  
  73.            IIII33332222 ppppeeeerrrrllll____ccccaaaallllllll____ssssvvvv((((SSSSVVVV**** ssssvvvv,,,, IIII33332222 ffffllllaaaaggggssss)))) ;;;;
  74.            IIII33332222 ppppeeeerrrrllll____ccccaaaallllllll____ppppvvvv((((cccchhhhaaaarrrr ****ssssuuuubbbbnnnnaaaammmmeeee,,,, IIII33332222 ffffllllaaaaggggssss)))) ;;;;
  75.            IIII33332222 ppppeeeerrrrllll____ccccaaaallllllll____mmmmeeeetttthhhhoooodddd((((cccchhhhaaaarrrr ****mmmmeeeetttthhhhnnnnaaaammmmeeee,,,, IIII33332222 ffffllllaaaaggggssss)))) ;;;;
  76.            IIII33332222 ppppeeeerrrrllll____ccccaaaallllllll____aaaarrrrggggvvvv((((cccchhhhaaaarrrr ****ssssuuuubbbbnnnnaaaammmmeeee,,,, IIII33332222 ffffllllaaaaggggssss,,,, rrrreeeeggggiiiisssstttteeeerrrr cccchhhhaaaarrrr ********aaaarrrrggggvvvv)))) ;;;;
  77.  
  78.        The key function is _p_e_r_l___c_a_l_l___s_v.  All the other functions
  79.        are fairly simple wrappers which make it easier to call
  80.        Perl subroutines in special cases. At the end of the day
  81.        they will all call _p_e_r_l___c_a_l_l___s_v to actually invoke the
  82.        Perl subroutine.
  83.  
  84.        All the _p_e_r_l___c_a_l_l___* functions have a ffffllllaaaaggggssss parameter which
  85.        is used to pass a bit mask of options to Perl.  This bit
  86.        mask operates identically for each of the functions.  The
  87.        settings available in the bit mask are discussed in the
  88.        section on _F_L_A_G _V_A_L_U_E_S.
  89.  
  90.        Each of the functions will now be discussed in turn.
  91.  
  92.        ppppeeeerrrrllll____ccccaaaallllllll____ssssvvvv
  93.             _p_e_r_l___c_a_l_l___s_v takes two parameters, the first, ssssvvvv, is
  94.             an SV*.  This allows you to specify the Perl
  95.             subroutine to be called either as a C string (which
  96.             has first been converted to an SV) or a reference to
  97.             a subroutine. The section, _U_s_i_n_g _p_e_r_l___c_a_l_l___s_v, shows
  98.             how you can make use of _p_e_r_l___c_a_l_l___s_v.
  99.  
  100.        ppppeeeerrrrllll____ccccaaaallllllll____ppppvvvv
  101.             The function, _p_e_r_l___c_a_l_l___p_v, is similar to
  102.             _p_e_r_l___c_a_l_l___s_v except it expects its first parameter to
  103.             be a C char* which identifies the Perl subroutine you
  104.             want to call, e.g. ppppeeeerrrrllll____ccccaaaallllllll____ppppvvvv((((""""ffffrrrreeeedddd"""",,,, 0000)))).  If the
  105.             subroutine you want to call is in another package,
  106.             just include the package name in the string, e.g.
  107.             """"ppppkkkkgggg::::::::ffffrrrreeeedddd"""".
  108.  
  109.        ppppeeeerrrrllll____ccccaaaallllllll____mmmmeeeetttthhhhoooodddd
  110.             The function _p_e_r_l___c_a_l_l___m_e_t_h_o_d is used to call a
  111.             method from a Perl class.  The parameter mmmmeeeetttthhhhnnnnaaaammmmeeee
  112.             corresponds to the name of the method to be called.
  113.             Note that the class that the method belongs to is
  114.             passed on the Perl stack rather than in the parameter
  115.             list. This class can be either the name of the class
  116.             (for a static method) or a reference to an object
  117.             (for a virtual method).  See the _p_e_r_l_o_b_j manpage for
  118.             more information on static and virtual methods and
  119.             the section on _U_s_i_n_g _p_e_r_l___c_a_l_l___m_e_t_h_o_d for an example
  120.             of using _p_e_r_l___c_a_l_l___m_e_t_h_o_d.
  121.  
  122.        ppppeeeerrrrllll____ccccaaaallllllll____aaaarrrrggggvvvv
  123.             _p_e_r_l___c_a_l_l___a_r_g_v calls the Perl subroutine specified by
  124.             the C string stored in the ssssuuuubbbbnnnnaaaammmmeeee parameter. It also
  125.             takes the usual ffffllllaaaaggggssss parameter.  The final
  126.             parameter, aaaarrrrggggvvvv, consists of a NULL terminated list
  127.  
  128.  
  129.  
  130. 29/Jan/96                perl 5.002 with                        2
  131.  
  132.  
  133.  
  134.  
  135.  
  136. PERLCALL(1)    User Contributed Perl Documentation    PERLCALL(1)
  137.  
  138.  
  139.             of C strings to be passed as parameters to the Perl
  140.             subroutine.  See _U_s_i_n_g _p_e_r_l___c_a_l_l___a_r_g_v.
  141.  
  142.        All the functions return an integer. This is a count of
  143.        the number of items returned by the Perl subroutine. The
  144.        actual items returned by the subroutine are stored on the
  145.        Perl stack.
  146.  
  147.        As a general rule you should _a_l_w_a_y_s check the return value
  148.        from these functions.  Even if you are expecting only a
  149.        particular number of values to be returned from the Perl
  150.        subroutine, there is nothing to stop someone from doing
  151.        something unexpected - don't say you haven't been warned.
  152.  
  153. FFFFLLLLAAAAGGGG VVVVAAAALLLLUUUUEEEESSSS
  154.        The ffffllllaaaaggggssss parameter in all the _p_e_r_l___c_a_l_l___* functions is a
  155.        bit mask which can consist of any combination of the
  156.        symbols defined below, OR'ed together.
  157.  
  158.        GGGG____SSSSCCCCAAAALLLLAAAARRRR
  159.  
  160.        Calls the Perl subroutine in a scalar context.  This is
  161.        the default context flag setting for all the _p_e_r_l___c_a_l_l___*
  162.        functions.
  163.  
  164.        This flag has 2 effects
  165.  
  166.        1.   it indicates to the subroutine being called that it
  167.             is executing in a scalar context (if it executes
  168.             _w_a_n_t_a_r_r_a_y the result will be false).
  169.  
  170.        2.   it ensures that only a scalar is actually returned
  171.             from the subroutine.  The subroutine can, of course,
  172.             ignore the _w_a_n_t_a_r_r_a_y and return a list anyway. If so,
  173.             then only the last element of the list will be
  174.             returned.
  175.  
  176.        The value returned by the _p_e_r_l___c_a_l_l___* function indicates
  177.        how may items have been returned by the Perl subroutine -
  178.        in this case it will be either 0 or 1.
  179.  
  180.        If 0, then you have specified the G_DISCARD flag.
  181.  
  182.        If 1, then the item actually returned by the Perl
  183.        subroutine will be stored on the Perl stack - the section
  184.        _R_e_t_u_r_n_i_n_g _a _S_c_a_l_a_r shows how to access this value on the
  185.        stack.  Remember that regardless of how many items the
  186.        Perl subroutine returns, only the last one will be
  187.        accessible from the stack - think of the case where only
  188.        one value is returned as being a list with only one
  189.        element.  Any other items that were returned will not
  190.        exist by the time control returns from the _p_e_r_l___c_a_l_l___*
  191.        function.  The section _R_e_t_u_r_n_i_n_g _a _l_i_s_t _i_n _a _s_c_a_l_a_r
  192.        _c_o_n_t_e_x_t shows an example of this behaviour.
  193.  
  194.  
  195.  
  196. 29/Jan/96                perl 5.002 with                        3
  197.  
  198.  
  199.  
  200.  
  201.  
  202. PERLCALL(1)    User Contributed Perl Documentation    PERLCALL(1)
  203.  
  204.  
  205.        GGGG____AAAARRRRRRRRAAAAYYYY
  206.  
  207.        Calls the Perl subroutine in a list context.
  208.  
  209.        As with G_SCALAR, this flag has 2 effects
  210.  
  211.        1.   it indicates to the subroutine being called that it
  212.             is executing in an array context (if it executes
  213.             _w_a_n_t_a_r_r_a_y the result will be true).
  214.  
  215.        2.   it ensures that all items returned from the
  216.             subroutine will be accessible when control returns
  217.             from the _p_e_r_l___c_a_l_l___* function.
  218.  
  219.        The value returned by the _p_e_r_l___c_a_l_l___* function indicates
  220.        how may items have been returned by the Perl subroutine.
  221.  
  222.        If 0, the you have specified the G_DISCARD flag.
  223.  
  224.        If not 0, then it will be a count of the number of items
  225.        returned by the subroutine. These items will be stored on
  226.        the Perl stack.  The section _R_e_t_u_r_n_i_n_g _a _l_i_s_t _o_f _v_a_l_u_e_s
  227.        gives an example of using the G_ARRAY flag and the
  228.        mechanics of accessing the returned items from the Perl
  229.        stack.
  230.  
  231.        GGGG____DDDDIIIISSSSCCCCAAAARRRRDDDD
  232.  
  233.        By default, the _p_e_r_l___c_a_l_l___* functions place the items
  234.        returned from by the Perl subroutine on the stack.  If you
  235.        are not interested in these items, then setting this flag
  236.        will make Perl get rid of them automatically for you.
  237.        Note that it is still possible to indicate a context to
  238.        the Perl subroutine by using either G_SCALAR or G_ARRAY.
  239.  
  240.        If you do not set this flag then it is _v_e_r_y important that
  241.        you make sure that any temporaries (i.e. parameters passed
  242.        to the Perl subroutine and values returned from the
  243.        subroutine) are disposed of yourself.  The section
  244.        _R_e_t_u_r_n_i_n_g _a _S_c_a_l_a_r gives details of how to explicitly
  245.        dispose of these temporaries and the section _U_s_i_n_g _P_e_r_l _t_o
  246.        _d_i_s_p_o_s_e _o_f _t_e_m_p_o_r_a_r_i_e_s discusses the specific
  247.        circumstances where you can ignore the problem and let
  248.        Perl deal with it for you.
  249.  
  250.        GGGG____NNNNOOOOAAAARRRRGGGGSSSS
  251.  
  252.        Whenever a Perl subroutine is called using one of the
  253.        _p_e_r_l___c_a_l_l___* functions, it is assumed by default that
  254.        parameters are to be passed to the subroutine.  If you are
  255.        not passing any parameters to the Perl subroutine, you can
  256.        save a bit of time by setting this flag.  It has the
  257.        effect of not creating the @@@@____ array for the Perl
  258.        subroutine.
  259.  
  260.  
  261.  
  262. 29/Jan/96                perl 5.002 with                        4
  263.  
  264.  
  265.  
  266.  
  267.  
  268. PERLCALL(1)    User Contributed Perl Documentation    PERLCALL(1)
  269.  
  270.  
  271.        Although the functionality provided by this flag may seem
  272.        straightforward, it should be used only if there is a good
  273.        reason to do so.  The reason for being cautious is that
  274.        even if you have specified the G_NOARGS flag, it is still
  275.        possible for the Perl subroutine that has been called to
  276.        think that you have passed it parameters.
  277.  
  278.        In fact, what can happen is that the Perl subroutine you
  279.        have called can access the @@@@____ array from a previous Perl
  280.        subroutine.  This will occur when the code that is
  281.        executing the _p_e_r_l___c_a_l_l___* function has itself been called
  282.        from another Perl subroutine. The code below illustrates
  283.        this
  284.  
  285.            ssssuuuubbbb ffffrrrreeeedddd
  286.              {{{{ pppprrrriiiinnnntttt """"@@@@____\\\\nnnn""""  }}}}
  287.  
  288.            ssssuuuubbbb jjjjooooeeee
  289.              {{{{ &&&&ffffrrrreeeedddd }}}}
  290.  
  291.            &&&&jjjjooooeeee((((1111,,,,2222,,,,3333)))) ;;;;
  292.  
  293.        This will print
  294.  
  295.            1111 2222 3333
  296.  
  297.        What has happened is that ffffrrrreeeedddd accesses the @@@@____ array which
  298.        belongs to jjjjooooeeee.
  299.  
  300.        GGGG____EEEEVVVVAAAALLLL
  301.  
  302.        It is possible for the Perl subroutine you are calling to
  303.        terminate abnormally, e.g. by calling _d_i_e explicitly or by
  304.        not actually existing.  By default, when either of these
  305.        of events occurs, the process will terminate immediately.
  306.        If though, you want to trap this type of event, specify
  307.        the G_EVAL flag.  It will put an _e_v_a_l _{ _} around the
  308.        subroutine call.
  309.  
  310.        Whenever control returns from the _p_e_r_l___c_a_l_l___* function you
  311.        need to check the $$$$@@@@ variable as you would in a normal
  312.        Perl script.
  313.  
  314.        The value returned from the _p_e_r_l___c_a_l_l___* function is
  315.        dependent on what other flags have been specified and
  316.        whether an error has occurred.  Here are all the different
  317.        cases that can occur
  318.  
  319.        +o    If the _p_e_r_l___c_a_l_l___* function returns normally, then
  320.             the value returned is as specified in the previous
  321.             sections.
  322.  
  323.        +o    If G_DISCARD is specified, the return value will
  324.             always be 0.
  325.  
  326.  
  327.  
  328. 29/Jan/96                perl 5.002 with                        5
  329.  
  330.  
  331.  
  332.  
  333.  
  334. PERLCALL(1)    User Contributed Perl Documentation    PERLCALL(1)
  335.  
  336.  
  337.        +o    If G_ARRAY is specified _a_n_d an error has occurred,
  338.             the return value will always be 0.
  339.  
  340.        +o    If G_SCALAR is specified _a_n_d an error has occurred,
  341.             the return value will be 1 and the value on the top
  342.             of the stack will be _u_n_d_e_f. This means that if you
  343.             have already detected the error by checking $$$$@@@@ and
  344.             you want the program to continue, you must remember
  345.             to pop the _u_n_d_e_f from the stack.
  346.  
  347.        See _U_s_i_n_g _G___E_V_A_L for details of using G_EVAL.
  348.  
  349.        GGGG____KKKKEEEEEEEEPPPPEEEERRRRRRRR
  350.  
  351.        You may have noticed that using the G_EVAL flag described
  352.        above will aaaallllwwwwaaaayyyyssss clear the $$$$@@@@ variable and set it to a
  353.        string describing the error iff there was an error in the
  354.        called code.  This unqualified resetting of $$$$@@@@ can be
  355.        problematic in the reliable identification of errors using
  356.        the eeeevvvvaaaallll {{{{}}}} mechanism, because the possibility exists that
  357.        perl will call other code (end of block processing code,
  358.        for example) between the time the error causes $$$$@@@@ to be
  359.        set within eeeevvvvaaaallll {{{{}}}}, and the subsequent statement which
  360.        checks for the value of $$$$@@@@ gets executed in the user's
  361.        script.
  362.  
  363.        This scenario will mostly be applicable to code that is
  364.        meant to be called from within destructors, asynchronous
  365.        callbacks, signal handlers, ________DDDDIIIIEEEE________ or ________WWWWAAAARRRRNNNN________ hooks, and
  366.        ttttiiiieeee functions.  In such situations, you will not want to
  367.        clear $$$$@@@@ at all, but simply to append any new errors to
  368.        any existing value of $$$$@@@@.
  369.  
  370.        The G_KEEPERR flag is meant to be used in conjunction with
  371.        G_EVAL in _p_e_r_l___c_a_l_l___* functions that are used to implement
  372.        such code.  This flag has no effect when G_EVAL is not
  373.        used.
  374.  
  375.        When G_KEEPERR is used, any errors in the called code will
  376.        be prefixed with the string "\t(in cleanup)", and appended
  377.        to the current value of $$$$@@@@.
  378.  
  379.        The G_KEEPERR flag was introduced in Perl version 5.002.
  380.  
  381.        See _U_s_i_n_g _G___K_E_E_P_E_R_R for an example of a situation that
  382.        warrants the use of this flag.
  383.  
  384.        DDDDeeeetttteeeerrrrmmmmiiiinnnniiiinnnngggg tttthhhheeee CCCCoooonnnntttteeeexxxxtttt
  385.  
  386.        As mentioned above, you can determine the context of the
  387.        currently executing subroutine in Perl with _w_a_n_t_a_r_r_a_y. The
  388.        equivalent test can be made in C by using the GGGGIIIIMMMMMMMMEEEE macro.
  389.        This will return GGGG____SSSSCCCCAAAALLLLAAAARRRR if you have been called in a
  390.        scalar context and GGGG____AAAARRRRRRRRAAAAYYYY if in an array context. An
  391.  
  392.  
  393.  
  394. 29/Jan/96                perl 5.002 with                        6
  395.  
  396.  
  397.  
  398.  
  399.  
  400. PERLCALL(1)    User Contributed Perl Documentation    PERLCALL(1)
  401.  
  402.  
  403.        example of using the GGGGIIIIMMMMMMMMEEEE macro is shown in section _U_s_i_n_g
  404.        _G_I_M_M_E.
  405.  
  406. KKKKNNNNOOOOWWWWNNNN PPPPRRRROOOOBBBBLLLLEEEEMMMMSSSS
  407.        This section outlines all known problems that exist in the
  408.        _p_e_r_l___c_a_l_l___* functions.
  409.  
  410.        1.   If you are intending to make use of both the G_EVAL
  411.             and G_SCALAR flags in your code, use a version of
  412.             Perl greater than 5.000.  There is a bug in version
  413.             5.000 of Perl which means that the combination of
  414.             these two flags will not work as described in the
  415.             section _F_L_A_G _V_A_L_U_E_S.
  416.  
  417.             Specifically, if the two flags are used when calling
  418.             a subroutine and that subroutine does not call _d_i_e,
  419.             the value returned by _p_e_r_l___c_a_l_l___* will be wrong.
  420.  
  421.        2.   In Perl 5.000 and 5.001 there is a problem with using
  422.             _p_e_r_l___c_a_l_l___* if the Perl sub you are calling attempts
  423.             to trap a _d_i_e.
  424.  
  425.             The symptom of this problem is that the called Perl
  426.             sub will continue to completion, but whenever it
  427.             attempts to pass control back to the XSUB, the
  428.             program will immediately terminate.
  429.  
  430.             For example, say you want to call this Perl sub
  431.  
  432.                 ssssuuuubbbb ffffrrrreeeedddd
  433.                 {{{{
  434.                     eeeevvvvaaaallll {{{{ ddddiiiieeee """"FFFFaaaattttaaaallll EEEErrrrrrrroooorrrr"""" ;;;; }}}}
  435.                     pppprrrriiiinnnntttt """"TTTTrrrraaaappppppppeeeedddd eeeerrrrrrrroooorrrr:::: $$$$@@@@\\\\nnnn""""
  436.                         iiiiffff $$$$@@@@ ;;;;
  437.                 }}}}
  438.  
  439.             via this XSUB
  440.  
  441.                 vvvvooooiiiidddd
  442.                 CCCCaaaallllllll____ffffrrrreeeedddd(((())))
  443.                     CCCCOOOODDDDEEEE::::
  444.                     PPPPUUUUSSSSHHHHMMMMAAAARRRRKKKK((((sssspppp)))) ;;;;
  445.                     ppppeeeerrrrllll____ccccaaaallllllll____ppppvvvv((((""""ffffrrrreeeedddd"""",,,, GGGG____DDDDIIIISSSSCCCCAAAARRRRDDDD||||GGGG____NNNNOOOOAAAARRRRGGGGSSSS)))) ;;;;
  446.                     ffffpppprrrriiiinnnnttttffff((((ssssttttddddeeeerrrrrrrr,,,, """"bbbbaaaacccckkkk iiiinnnn CCCCaaaallllllll____ffffrrrreeeedddd\\\\nnnn"""")))) ;;;;
  447.  
  448.             When CCCCaaaallllllll____ffffrrrreeeedddd is executed it will print
  449.  
  450.                 TTTTrrrraaaappppppppeeeedddd eeeerrrrrrrroooorrrr:::: FFFFaaaattttaaaallll EEEErrrrrrrroooorrrr
  451.  
  452.             As control never returns to CCCCaaaallllllll____ffffrrrreeeedddd, the """"bbbbaaaacccckkkk iiiinnnn
  453.             CCCCaaaallllllll____ffffrrrreeeedddd"""" string will not get printed.
  454.  
  455.             To work around this problem, you can either upgrade
  456.             to Perl 5.002 (or later), or use the G_EVAL flag with
  457.  
  458.  
  459.  
  460. 29/Jan/96                perl 5.002 with                        7
  461.  
  462.  
  463.  
  464.  
  465.  
  466. PERLCALL(1)    User Contributed Perl Documentation    PERLCALL(1)
  467.  
  468.  
  469.             _p_e_r_l___c_a_l_l___* as shown below
  470.  
  471.                 vvvvooooiiiidddd
  472.                 CCCCaaaallllllll____ffffrrrreeeedddd(((())))
  473.                     CCCCOOOODDDDEEEE::::
  474.                     PPPPUUUUSSSSHHHHMMMMAAAARRRRKKKK((((sssspppp)))) ;;;;
  475.                     ppppeeeerrrrllll____ccccaaaallllllll____ppppvvvv((((""""ffffrrrreeeedddd"""",,,, GGGG____EEEEVVVVAAAALLLL||||GGGG____DDDDIIIISSSSCCCCAAAARRRRDDDD||||GGGG____NNNNOOOOAAAARRRRGGGGSSSS)))) ;;;;
  476.                     ffffpppprrrriiiinnnnttttffff((((ssssttttddddeeeerrrrrrrr,,,, """"bbbbaaaacccckkkk iiiinnnn CCCCaaaallllllll____ffffrrrreeeedddd\\\\nnnn"""")))) ;;;;
  477.  
  478.  
  479. EEEEXXXXAAAAMMMMPPPPLLLLEEEESSSS
  480.        Enough of the definition talk, let's have a few examples.
  481.  
  482.        Perl provides many macros to assist in accessing the Perl
  483.        stack.  Wherever possible, these macros should always be
  484.        used when interfacing to Perl internals.  Hopefully this
  485.        should make the code less vulnerable to any changes made
  486.        to Perl in the future.
  487.  
  488.        Another point worth noting is that in the first series of
  489.        examples I have made use of only the _p_e_r_l___c_a_l_l___p_v
  490.        function.  This has been done to keep the code simpler and
  491.        ease you into the topic.  Wherever possible, if the choice
  492.        is between using _p_e_r_l___c_a_l_l___p_v and _p_e_r_l___c_a_l_l___s_v, you should
  493.        always try to use _p_e_r_l___c_a_l_l___s_v.  See _U_s_i_n_g _p_e_r_l___c_a_l_l___s_v
  494.        for details.
  495.  
  496.        NNNNoooo PPPPaaaarrrraaaammmmeeeetttteeeerrrrssss,,,, NNNNooootttthhhhiiiinnnngggg rrrreeeettttuuuurrrrnnnneeeedddd
  497.  
  498.        This first trivial example will call a Perl subroutine,
  499.        _P_r_i_n_t_U_I_D, to print out the UID of the process.
  500.  
  501.            ssssuuuubbbb PPPPrrrriiiinnnnttttUUUUIIIIDDDD
  502.            {{{{
  503.                pppprrrriiiinnnntttt """"UUUUIIIIDDDD iiiissss $$$$<<<<\\\\nnnn"""" ;;;;
  504.            }}}}
  505.  
  506.        and here is a C function to call it
  507.  
  508.            ssssttttaaaattttiiiicccc vvvvooooiiiidddd
  509.            ccccaaaallllllll____PPPPrrrriiiinnnnttttUUUUIIIIDDDD(((())))
  510.            {{{{
  511.                ddddSSSSPPPP ;;;;
  512.  
  513.                PPPPUUUUSSSSHHHHMMMMAAAARRRRKKKK((((sssspppp)))) ;;;;
  514.                ppppeeeerrrrllll____ccccaaaallllllll____ppppvvvv((((""""PPPPrrrriiiinnnnttttUUUUIIIIDDDD"""",,,, GGGG____DDDDIIIISSSSCCCCAAAARRRRDDDD||||GGGG____NNNNOOOOAAAARRRRGGGGSSSS)))) ;;;;
  515.            }}}}
  516.  
  517.        Simple, eh.
  518.  
  519.        A few points to note about this example.
  520.  
  521.        1.   Ignore ddddSSSSPPPP and PPPPUUUUSSSSHHHHMMMMAAAARRRRKKKK((((sssspppp)))) for now. They will be
  522.             discussed in the next example.
  523.  
  524.  
  525.  
  526. 29/Jan/96                perl 5.002 with                        8
  527.  
  528.  
  529.  
  530.  
  531.  
  532. PERLCALL(1)    User Contributed Perl Documentation    PERLCALL(1)
  533.  
  534.  
  535.        2.   We aren't passing any parameters to _P_r_i_n_t_U_I_D so
  536.             G_NOARGS can be specified.
  537.  
  538.        3.   We aren't interested in anything returned from
  539.             _P_r_i_n_t_U_I_D, so G_DISCARD is specified. Even if _P_r_i_n_t_U_I_D
  540.             was changed to actually return some _v_a_l_u_e(s), having
  541.             specified G_DISCARD will mean that they will be wiped
  542.             by the time control returns from _p_e_r_l___c_a_l_l___p_v.
  543.  
  544.        4.   As _p_e_r_l___c_a_l_l___p_v is being used, the Perl subroutine is
  545.             specified as a C string. In this case the subroutine
  546.             name has been 'hard-wired' into the code.
  547.  
  548.        5.   Because we specified G_DISCARD, it is not necessary
  549.             to check the value returned from _p_e_r_l___c_a_l_l___p_v. It
  550.             will always be 0.
  551.  
  552.        PPPPaaaassssssssiiiinnnngggg PPPPaaaarrrraaaammmmeeeetttteeeerrrrssss
  553.  
  554.        Now let's make a slightly more complex example. This time
  555.        we want to call a Perl subroutine, LLLLeeeeffffttttSSSSttttrrrriiiinnnngggg, which will
  556.        take 2 parameters - a string ($$$$ssss) and an integer ($$$$nnnn).
  557.        The subroutine will simply print the first $$$$nnnn characters
  558.        of the string.
  559.  
  560.        So the Perl subroutine would look like this
  561.  
  562.            ssssuuuubbbb LLLLeeeeffffttttSSSSttttrrrriiiinnnngggg
  563.            {{{{
  564.                mmmmyyyy(((($$$$ssss,,,, $$$$nnnn)))) ==== @@@@____ ;;;;
  565.                pppprrrriiiinnnntttt ssssuuuubbbbssssttttrrrr(((($$$$ssss,,,, 0000,,,, $$$$nnnn)))),,,, """"\\\\nnnn"""" ;;;;
  566.            }}}}
  567.  
  568.        The C function required to call _L_e_f_t_S_t_r_i_n_g would look like
  569.        this.
  570.  
  571.            ssssttttaaaattttiiiicccc vvvvooooiiiidddd
  572.            ccccaaaallllllll____LLLLeeeeffffttttSSSSttttrrrriiiinnnngggg((((aaaa,,,, bbbb))))
  573.            cccchhhhaaaarrrr **** aaaa ;;;;
  574.            iiiinnnntttt bbbb ;;;;
  575.            {{{{
  576.                ddddSSSSPPPP ;;;;
  577.  
  578.                PPPPUUUUSSSSHHHHMMMMAAAARRRRKKKK((((sssspppp)))) ;;;;
  579.                XXXXPPPPUUUUSSSSHHHHssss((((ssssvvvv____2222mmmmoooorrrrttttaaaallll((((nnnneeeewwwwSSSSVVVVppppvvvv((((aaaa,,,, 0000))))))))))));;;;
  580.                XXXXPPPPUUUUSSSSHHHHssss((((ssssvvvv____2222mmmmoooorrrrttttaaaallll((((nnnneeeewwwwSSSSVVVViiiivvvv((((bbbb))))))))))));;;;
  581.                PPPPUUUUTTTTBBBBAAAACCCCKKKK ;;;;
  582.  
  583.                ppppeeeerrrrllll____ccccaaaallllllll____ppppvvvv((((""""LLLLeeeeffffttttSSSSttttrrrriiiinnnngggg"""",,,, GGGG____DDDDIIIISSSSCCCCAAAARRRRDDDD))));;;;
  584.            }}}}
  585.  
  586.        Here are a few notes on the C function _c_a_l_l___L_e_f_t_S_t_r_i_n_g.
  587.  
  588.  
  589.  
  590.  
  591.  
  592. 29/Jan/96                perl 5.002 with                        9
  593.  
  594.  
  595.  
  596.  
  597.  
  598. PERLCALL(1)    User Contributed Perl Documentation    PERLCALL(1)
  599.  
  600.  
  601.        1.   Parameters are passed to the Perl subroutine using
  602.             the Perl stack.  This is the purpose of the code
  603.             beginning with the line ddddSSSSPPPP and ending with the line
  604.             PPPPUUUUTTTTBBBBAAAACCCCKKKK.
  605.  
  606.        2.   If you are going to put something onto the Perl
  607.             stack, you need to know where to put it. This is the
  608.             purpose of the macro ddddSSSSPPPP - it declares and
  609.             initializes a _l_o_c_a_l copy of the Perl stack pointer.
  610.  
  611.             All the other macros which will be used in this
  612.             example require you to have used this macro.
  613.  
  614.             The exception to this rule is if you are calling a
  615.             Perl subroutine directly from an XSUB function. In
  616.             this case it is not necessary to explicitly use the
  617.             ddddSSSSPPPP macro - it will be declared for you
  618.             automatically.
  619.  
  620.        3.   Any parameters to be pushed onto the stack should be
  621.             bracketed by the PPPPUUUUSSSSHHHHMMMMAAAARRRRKKKK and PPPPUUUUTTTTBBBBAAAACCCCKKKK macros.  The
  622.             purpose of these two macros, in this context, is to
  623.             automatically count the number of parameters you are
  624.             pushing. Then whenever Perl is creating the @@@@____ array
  625.             for the subroutine, it knows how big to make it.
  626.  
  627.             The PPPPUUUUSSSSHHHHMMMMAAAARRRRKKKK macro tells Perl to make a mental note
  628.             of the current stack pointer. Even if you aren't
  629.             passing any parameters (like the example shown in the
  630.             section _N_o _P_a_r_a_m_e_t_e_r_s_, _N_o_t_h_i_n_g _r_e_t_u_r_n_e_d) you must
  631.             still call the PPPPUUUUSSSSHHHHMMMMAAAARRRRKKKK macro before you can call any
  632.             of the _p_e_r_l___c_a_l_l___* functions - Perl still needs to
  633.             know that there are no parameters.
  634.  
  635.             The PPPPUUUUTTTTBBBBAAAACCCCKKKK macro sets the global copy of the stack
  636.             pointer to be the same as our local copy. If we
  637.             didn't do this _p_e_r_l___c_a_l_l___p_v wouldn't know where the
  638.             two parameters we pushed were - remember that up to
  639.             now all the stack pointer manipulation we have done
  640.             is with our local copy, _n_o_t the global copy.
  641.  
  642.        4.   The only flag specified this time is G_DISCARD. Since
  643.             we are passing 2 parameters to the Perl subroutine
  644.             this time, we have not specified G_NOARGS.
  645.  
  646.        5.   Next, we come to XPUSHs. This is where the parameters
  647.             actually get pushed onto the stack. In this case we
  648.             are pushing a string and an integer.
  649.  
  650.             See the section the section on _X_S_U_B_'_S _a_n_d _t_h_e
  651.             _A_r_g_u_m_e_n_t _S_t_a_c_k in the _p_e_r_l_g_u_t_s manpage for details on
  652.             how the XPUSH macros work.
  653.  
  654.  
  655.  
  656.  
  657.  
  658. 29/Jan/96                perl 5.002 with                       10
  659.  
  660.  
  661.  
  662.  
  663.  
  664. PERLCALL(1)    User Contributed Perl Documentation    PERLCALL(1)
  665.  
  666.  
  667.        6.   Finally, _L_e_f_t_S_t_r_i_n_g can now be called via the
  668.             _p_e_r_l___c_a_l_l___p_v function.
  669.  
  670.        RRRReeeettttuuuurrrrnnnniiiinnnngggg aaaa SSSSccccaaaallllaaaarrrr
  671.  
  672.        Now for an example of dealing with the items returned from
  673.        a Perl subroutine.
  674.  
  675.        Here is a Perl subroutine, _A_d_d_e_r,  which takes 2 integer
  676.        parameters and simply returns their sum.
  677.  
  678.            ssssuuuubbbb AAAAddddddddeeeerrrr
  679.            {{{{
  680.                mmmmyyyy(((($$$$aaaa,,,, $$$$bbbb)))) ==== @@@@____ ;;;;
  681.                $$$$aaaa ++++ $$$$bbbb ;;;;
  682.            }}}}
  683.  
  684.        Since we are now concerned with the return value from
  685.        _A_d_d_e_r, the C function required to call it is now a bit
  686.        more complex.
  687.  
  688.            ssssttttaaaattttiiiicccc vvvvooooiiiidddd
  689.            ccccaaaallllllll____AAAAddddddddeeeerrrr((((aaaa,,,, bbbb))))
  690.            iiiinnnntttt aaaa ;;;;
  691.            iiiinnnntttt bbbb ;;;;
  692.            {{{{
  693.                ddddSSSSPPPP ;;;;
  694.                iiiinnnntttt ccccoooouuuunnnntttt ;;;;
  695.  
  696.                EEEENNNNTTTTEEEERRRR ;;;;
  697.                SSSSAAAAVVVVEEEETTTTMMMMPPPPSSSS;;;;
  698.  
  699.                PPPPUUUUSSSSHHHHMMMMAAAARRRRKKKK((((sssspppp)))) ;;;;
  700.                XXXXPPPPUUUUSSSSHHHHssss((((ssssvvvv____2222mmmmoooorrrrttttaaaallll((((nnnneeeewwwwSSSSVVVViiiivvvv((((aaaa))))))))))));;;;
  701.                XXXXPPPPUUUUSSSSHHHHssss((((ssssvvvv____2222mmmmoooorrrrttttaaaallll((((nnnneeeewwwwSSSSVVVViiiivvvv((((bbbb))))))))))));;;;
  702.                PPPPUUUUTTTTBBBBAAAACCCCKKKK ;;;;
  703.  
  704.                ccccoooouuuunnnntttt ==== ppppeeeerrrrllll____ccccaaaallllllll____ppppvvvv((((""""AAAAddddddddeeeerrrr"""",,,, GGGG____SSSSCCCCAAAALLLLAAAARRRR))));;;;
  705.  
  706.                SSSSPPPPAAAAGGGGAAAAIIIINNNN ;;;;
  707.  
  708.                iiiiffff ((((ccccoooouuuunnnntttt !!!!==== 1111))))
  709.                    ccccrrrrooooaaaakkkk((((""""BBBBiiiigggg ttttrrrroooouuuubbbblllleeee\\\\nnnn"""")))) ;;;;
  710.  
  711.                pppprrrriiiinnnnttttffff ((((""""TTTThhhheeee ssssuuuummmm ooooffff %%%%dddd aaaannnndddd %%%%dddd iiiissss %%%%dddd\\\\nnnn"""",,,, aaaa,,,, bbbb,,,, PPPPOOOOPPPPiiii)))) ;;;;
  712.  
  713.                PPPPUUUUTTTTBBBBAAAACCCCKKKK ;;;;
  714.                FFFFRRRREEEEEEEETTTTMMMMPPPPSSSS ;;;;
  715.                LLLLEEEEAAAAVVVVEEEE ;;;;
  716.            }}}}
  717.  
  718.        Points to note this time are
  719.  
  720.  
  721.  
  722.  
  723.  
  724. 29/Jan/96                perl 5.002 with                       11
  725.  
  726.  
  727.  
  728.  
  729.  
  730. PERLCALL(1)    User Contributed Perl Documentation    PERLCALL(1)
  731.  
  732.  
  733.        1.   The only flag specified this time was G_SCALAR. That
  734.             means the @@@@____ array will be created and that the value
  735.             returned by _A_d_d_e_r will still exist after the call to
  736.             _p_e_r_l___c_a_l_l___p_v.
  737.  
  738.        2.   Because we are interested in what is returned from
  739.             _A_d_d_e_r we cannot specify G_DISCARD. This means that we
  740.             will have to tidy up the Perl stack and dispose of
  741.             any temporary values ourselves. This is the purpose
  742.             of
  743.  
  744.                 EEEENNNNTTTTEEEERRRR ;;;;
  745.                 SSSSAAAAVVVVEEEETTTTMMMMPPPPSSSS ;;;;
  746.  
  747.             at the start of the function, and
  748.  
  749.                 FFFFRRRREEEEEEEETTTTMMMMPPPPSSSS ;;;;
  750.                 LLLLEEEEAAAAVVVVEEEE ;;;;
  751.  
  752.             at the end. The EEEENNNNTTTTEEEERRRR/SSSSAAAAVVVVEEEETTTTMMMMPPPPSSSS pair creates a
  753.             boundary for any temporaries we create.  This means
  754.             that the temporaries we get rid of will be limited to
  755.             those which were created after these calls.
  756.  
  757.             The FFFFRRRREEEEEEEETTTTMMMMPPPPSSSS/LLLLEEEEAAAAVVVVEEEE pair will get rid of any values
  758.             returned by the Perl subroutine, plus it will also
  759.             dump the mortal SV's we have created.  Having
  760.             EEEENNNNTTTTEEEERRRR/SSSSAAAAVVVVEEEETTTTMMMMPPPPSSSS at the beginning of the code makes
  761.             sure that no other mortals are destroyed.
  762.  
  763.             Think of these macros as working a bit like using {{{{
  764.             and }}}} in Perl to limit the scope of local variables.
  765.  
  766.             See the section _U_s_i_n_g _P_e_r_l _t_o _d_i_s_p_o_s_e _o_f _t_e_m_p_o_r_a_r_i_e_s
  767.             for details of an alternative to using these macros.
  768.  
  769.        3.   The purpose of the macro SSSSPPPPAAAAGGGGAAAAIIIINNNN is to refresh the
  770.             local copy of the stack pointer. This is necessary
  771.             because it is possible that the memory allocated to
  772.             the Perl stack has been re-allocated whilst in the
  773.             _p_e_r_l___c_a_l_l___p_v call.
  774.  
  775.             If you are making use of the Perl stack pointer in
  776.             your code you must always refresh the your local copy
  777.             using SPAGAIN whenever you make use of the
  778.             _p_e_r_l___c_a_l_l___* functions or any other Perl internal
  779.             function.
  780.  
  781.        4.   Although only a single value was expected to be
  782.             returned from _A_d_d_e_r, it is still good practice to
  783.             check the return code from _p_e_r_l___c_a_l_l___p_v anyway.
  784.  
  785.             Expecting a single value is not quite the same as
  786.             knowing that there will be one. If someone modified
  787.  
  788.  
  789.  
  790. 29/Jan/96                perl 5.002 with                       12
  791.  
  792.  
  793.  
  794.  
  795.  
  796. PERLCALL(1)    User Contributed Perl Documentation    PERLCALL(1)
  797.  
  798.  
  799.             _A_d_d_e_r to return a list and we didn't check for that
  800.             possibility and take appropriate action the Perl
  801.             stack would end up in an inconsistent state. That is
  802.             something you _r_e_a_l_l_y don't want to ever happen.
  803.  
  804.        5.   The PPPPOOOOPPPPiiii macro is used here to pop the return value
  805.             from the stack.  In this case we wanted an integer,
  806.             so PPPPOOOOPPPPiiii was used.
  807.  
  808.             Here is the complete list of POP macros available,
  809.             along with the types they return.
  810.  
  811.                 PPPPOOOOPPPPssss        SSSSVVVV
  812.                 PPPPOOOOPPPPpppp        ppppooooiiiinnnntttteeeerrrr
  813.                 PPPPOOOOPPPPnnnn        ddddoooouuuubbbblllleeee
  814.                 PPPPOOOOPPPPiiii        iiiinnnntttteeeeggggeeeerrrr
  815.                 PPPPOOOOPPPPllll        lllloooonnnngggg
  816.  
  817.  
  818.        6.   The final PPPPUUUUTTTTBBBBAAAACCCCKKKK is used to leave the Perl stack in
  819.             a consistent state before exiting the function.  This
  820.             is necessary because when we popped the return value
  821.             from the stack with PPPPOOOOPPPPiiii it updated only our local
  822.             copy of the stack pointer.  Remember, PPPPUUUUTTTTBBBBAAAACCCCKKKK sets
  823.             the global stack pointer to be the same as our local
  824.             copy.
  825.  
  826.        RRRReeeettttuuuurrrrnnnniiiinnnngggg aaaa lllliiiisssstttt ooooffff vvvvaaaalllluuuueeeessss
  827.  
  828.        Now, let's extend the previous example to return both the
  829.        sum of the parameters and the difference.
  830.  
  831.        Here is the Perl subroutine
  832.  
  833.            ssssuuuubbbb AAAAddddddddSSSSuuuubbbbttttrrrraaaacccctttt
  834.            {{{{
  835.               mmmmyyyy(((($$$$aaaa,,,, $$$$bbbb)))) ==== @@@@____ ;;;;
  836.               (((($$$$aaaa++++$$$$bbbb,,,, $$$$aaaa----$$$$bbbb)))) ;;;;
  837.            }}}}
  838.  
  839.        and this is the C function
  840.  
  841.            ssssttttaaaattttiiiicccc vvvvooooiiiidddd
  842.            ccccaaaallllllll____AAAAddddddddSSSSuuuubbbbttttrrrraaaacccctttt((((aaaa,,,, bbbb))))
  843.            iiiinnnntttt aaaa ;;;;
  844.            iiiinnnntttt bbbb ;;;;
  845.            {{{{
  846.                ddddSSSSPPPP ;;;;
  847.                iiiinnnntttt ccccoooouuuunnnntttt ;;;;
  848.  
  849.                EEEENNNNTTTTEEEERRRR ;;;;
  850.                SSSSAAAAVVVVEEEETTTTMMMMPPPPSSSS;;;;
  851.  
  852.  
  853.  
  854.  
  855.  
  856. 29/Jan/96                perl 5.002 with                       13
  857.  
  858.  
  859.  
  860.  
  861.  
  862. PERLCALL(1)    User Contributed Perl Documentation    PERLCALL(1)
  863.  
  864.  
  865.                PPPPUUUUSSSSHHHHMMMMAAAARRRRKKKK((((sssspppp)))) ;;;;
  866.                XXXXPPPPUUUUSSSSHHHHssss((((ssssvvvv____2222mmmmoooorrrrttttaaaallll((((nnnneeeewwwwSSSSVVVViiiivvvv((((aaaa))))))))))));;;;
  867.                XXXXPPPPUUUUSSSSHHHHssss((((ssssvvvv____2222mmmmoooorrrrttttaaaallll((((nnnneeeewwwwSSSSVVVViiiivvvv((((bbbb))))))))))));;;;
  868.                PPPPUUUUTTTTBBBBAAAACCCCKKKK ;;;;
  869.  
  870.                ccccoooouuuunnnntttt ==== ppppeeeerrrrllll____ccccaaaallllllll____ppppvvvv((((""""AAAAddddddddSSSSuuuubbbbttttrrrraaaacccctttt"""",,,, GGGG____AAAARRRRRRRRAAAAYYYY))));;;;
  871.  
  872.                SSSSPPPPAAAAGGGGAAAAIIIINNNN ;;;;
  873.  
  874.                iiiiffff ((((ccccoooouuuunnnntttt !!!!==== 2222))))
  875.                    ccccrrrrooooaaaakkkk((((""""BBBBiiiigggg ttttrrrroooouuuubbbblllleeee\\\\nnnn"""")))) ;;;;
  876.  
  877.                pppprrrriiiinnnnttttffff ((((""""%%%%dddd ---- %%%%dddd ==== %%%%dddd\\\\nnnn"""",,,, aaaa,,,, bbbb,,,, PPPPOOOOPPPPiiii)))) ;;;;
  878.                pppprrrriiiinnnnttttffff ((((""""%%%%dddd ++++ %%%%dddd ==== %%%%dddd\\\\nnnn"""",,,, aaaa,,,, bbbb,,,, PPPPOOOOPPPPiiii)))) ;;;;
  879.  
  880.                PPPPUUUUTTTTBBBBAAAACCCCKKKK ;;;;
  881.                FFFFRRRREEEEEEEETTTTMMMMPPPPSSSS ;;;;
  882.                LLLLEEEEAAAAVVVVEEEE ;;;;
  883.            }}}}
  884.  
  885.        If _c_a_l_l___A_d_d_S_u_b_t_r_a_c_t is called like this
  886.  
  887.            ccccaaaallllllll____AAAAddddddddSSSSuuuubbbbttttrrrraaaacccctttt((((7777,,,, 4444)))) ;;;;
  888.  
  889.        then here is the output
  890.  
  891.            7777 ---- 4444 ==== 3333
  892.            7777 ++++ 4444 ==== 11111111
  893.  
  894.        Notes
  895.  
  896.        1.   We wanted array context, so G_ARRAY was used.
  897.  
  898.        2.   Not surprisingly PPPPOOOOPPPPiiii is used twice this time because
  899.             we were retrieving 2 values from the stack. The
  900.             important thing to note is that when using the PPPPOOOOPPPP****
  901.             macros they come off the stack in _r_e_v_e_r_s_e order.
  902.  
  903.        RRRReeeettttuuuurrrrnnnniiiinnnngggg aaaa lllliiiisssstttt iiiinnnn aaaa ssssccccaaaallllaaaarrrr ccccoooonnnntttteeeexxxxtttt
  904.  
  905.        Say the Perl subroutine in the previous section was called
  906.        in a scalar context, like this
  907.  
  908.            ssssttttaaaattttiiiicccc vvvvooooiiiidddd
  909.            ccccaaaallllllll____AAAAddddddddSSSSuuuubbbbSSSSccccaaaallllaaaarrrr((((aaaa,,,, bbbb))))
  910.            iiiinnnntttt aaaa ;;;;
  911.            iiiinnnntttt bbbb ;;;;
  912.            {{{{
  913.                ddddSSSSPPPP ;;;;
  914.                iiiinnnntttt ccccoooouuuunnnntttt ;;;;
  915.                iiiinnnntttt iiii ;;;;
  916.  
  917.                EEEENNNNTTTTEEEERRRR ;;;;
  918.                SSSSAAAAVVVVEEEETTTTMMMMPPPPSSSS;;;;
  919.  
  920.  
  921.  
  922. 29/Jan/96                perl 5.002 with                       14
  923.  
  924.  
  925.  
  926.  
  927.  
  928. PERLCALL(1)    User Contributed Perl Documentation    PERLCALL(1)
  929.  
  930.  
  931.                PPPPUUUUSSSSHHHHMMMMAAAARRRRKKKK((((sssspppp)))) ;;;;
  932.                XXXXPPPPUUUUSSSSHHHHssss((((ssssvvvv____2222mmmmoooorrrrttttaaaallll((((nnnneeeewwwwSSSSVVVViiiivvvv((((aaaa))))))))))));;;;
  933.                XXXXPPPPUUUUSSSSHHHHssss((((ssssvvvv____2222mmmmoooorrrrttttaaaallll((((nnnneeeewwwwSSSSVVVViiiivvvv((((bbbb))))))))))));;;;
  934.                PPPPUUUUTTTTBBBBAAAACCCCKKKK ;;;;
  935.  
  936.                ccccoooouuuunnnntttt ==== ppppeeeerrrrllll____ccccaaaallllllll____ppppvvvv((((""""AAAAddddddddSSSSuuuubbbbttttrrrraaaacccctttt"""",,,, GGGG____SSSSCCCCAAAALLLLAAAARRRR))));;;;
  937.  
  938.                SSSSPPPPAAAAGGGGAAAAIIIINNNN ;;;;
  939.  
  940.                pppprrrriiiinnnnttttffff ((((""""IIIItttteeeemmmmssss RRRReeeettttuuuurrrrnnnneeeedddd ==== %%%%dddd\\\\nnnn"""",,,, ccccoooouuuunnnntttt)))) ;;;;
  941.  
  942.                ffffoooorrrr ((((iiii ==== 1111 ;;;; iiii <<<<==== ccccoooouuuunnnntttt ;;;; ++++++++iiii))))
  943.                    pppprrrriiiinnnnttttffff ((((""""VVVVaaaalllluuuueeee %%%%dddd ==== %%%%dddd\\\\nnnn"""",,,, iiii,,,, PPPPOOOOPPPPiiii)))) ;;;;
  944.  
  945.                PPPPUUUUTTTTBBBBAAAACCCCKKKK ;;;;
  946.                FFFFRRRREEEEEEEETTTTMMMMPPPPSSSS ;;;;
  947.                LLLLEEEEAAAAVVVVEEEE ;;;;
  948.            }}}}
  949.  
  950.        The other modification made is that _c_a_l_l___A_d_d_S_u_b_S_c_a_l_a_r will
  951.        print the number of items returned from the Perl
  952.        subroutine and their value (for simplicity it assumes that
  953.        they are integer).  So if _c_a_l_l___A_d_d_S_u_b_S_c_a_l_a_r is called
  954.  
  955.            ccccaaaallllllll____AAAAddddddddSSSSuuuubbbbSSSSccccaaaallllaaaarrrr((((7777,,,, 4444)))) ;;;;
  956.  
  957.        then the output will be
  958.  
  959.            IIIItttteeeemmmmssss RRRReeeettttuuuurrrrnnnneeeedddd ==== 1111
  960.            VVVVaaaalllluuuueeee 1111 ==== 3333
  961.  
  962.        In this case the main point to note is that only the last
  963.        item in the list returned from the subroutine, _A_d_d_e_r
  964.        actually made it back to _c_a_l_l___A_d_d_S_u_b_S_c_a_l_a_r.
  965.  
  966.        RRRReeeettttuuuurrrrnnnniiiinnnngggg DDDDaaaattttaaaa ffffrrrroooommmm PPPPeeeerrrrllll vvvviiiiaaaa tttthhhheeee ppppaaaarrrraaaammmmeeeetttteeeerrrr lllliiiisssstttt
  967.  
  968.        It is also possible to return values directly via the
  969.        parameter list - whether it is actually desirable to do it
  970.        is another matter entirely.
  971.  
  972.        The Perl subroutine, _I_n_c, below takes 2 parameters and
  973.        increments each directly.
  974.  
  975.            ssssuuuubbbb IIIInnnncccc
  976.            {{{{
  977.                ++++++++ $$$$____[[[[0000]]]] ;;;;
  978.                ++++++++ $$$$____[[[[1111]]]] ;;;;
  979.            }}}}
  980.  
  981.        and here is a C function to call it.
  982.  
  983.  
  984.  
  985.  
  986.  
  987.  
  988. 29/Jan/96                perl 5.002 with                       15
  989.  
  990.  
  991.  
  992.  
  993.  
  994. PERLCALL(1)    User Contributed Perl Documentation    PERLCALL(1)
  995.  
  996.  
  997.            ssssttttaaaattttiiiicccc vvvvooooiiiidddd
  998.            ccccaaaallllllll____IIIInnnncccc((((aaaa,,,, bbbb))))
  999.            iiiinnnntttt aaaa ;;;;
  1000.            iiiinnnntttt bbbb ;;;;
  1001.            {{{{
  1002.                ddddSSSSPPPP ;;;;
  1003.                iiiinnnntttt ccccoooouuuunnnntttt ;;;;
  1004.                SSSSVVVV **** ssssvvvvaaaa ;;;;
  1005.                SSSSVVVV **** ssssvvvvbbbb ;;;;
  1006.  
  1007.                EEEENNNNTTTTEEEERRRR ;;;;
  1008.                SSSSAAAAVVVVEEEETTTTMMMMPPPPSSSS;;;;
  1009.  
  1010.                ssssvvvvaaaa ==== ssssvvvv____2222mmmmoooorrrrttttaaaallll((((nnnneeeewwwwSSSSVVVViiiivvvv((((aaaa)))))))) ;;;;
  1011.                ssssvvvvbbbb ==== ssssvvvv____2222mmmmoooorrrrttttaaaallll((((nnnneeeewwwwSSSSVVVViiiivvvv((((bbbb)))))))) ;;;;
  1012.  
  1013.                PPPPUUUUSSSSHHHHMMMMAAAARRRRKKKK((((sssspppp)))) ;;;;
  1014.                XXXXPPPPUUUUSSSSHHHHssss((((ssssvvvvaaaa))));;;;
  1015.                XXXXPPPPUUUUSSSSHHHHssss((((ssssvvvvbbbb))));;;;
  1016.                PPPPUUUUTTTTBBBBAAAACCCCKKKK ;;;;
  1017.  
  1018.                ccccoooouuuunnnntttt ==== ppppeeeerrrrllll____ccccaaaallllllll____ppppvvvv((((""""IIIInnnncccc"""",,,, GGGG____DDDDIIIISSSSCCCCAAAARRRRDDDD))));;;;
  1019.  
  1020.                iiiiffff ((((ccccoooouuuunnnntttt !!!!==== 0000))))
  1021.                    ccccrrrrooooaaaakkkk ((((""""ccccaaaallllllll____IIIInnnncccc:::: eeeexxxxppppeeeecccctttteeeedddd 0000 vvvvaaaalllluuuueeeessss ffffrrrroooommmm ''''IIIInnnncccc'''',,,, ggggooootttt %%%%dddd\\\\nnnn"""",,,,
  1022.                           ccccoooouuuunnnntttt)))) ;;;;
  1023.  
  1024.                pppprrrriiiinnnnttttffff ((((""""%%%%dddd ++++ 1111 ==== %%%%dddd\\\\nnnn"""",,,, aaaa,,,, SSSSvvvvIIIIVVVV((((ssssvvvvaaaa)))))))) ;;;;
  1025.                pppprrrriiiinnnnttttffff ((((""""%%%%dddd ++++ 1111 ==== %%%%dddd\\\\nnnn"""",,,, bbbb,,,, SSSSvvvvIIIIVVVV((((ssssvvvvbbbb)))))))) ;;;;
  1026.  
  1027.                FFFFRRRREEEEEEEETTTTMMMMPPPPSSSS ;;;;
  1028.                LLLLEEEEAAAAVVVVEEEE ;;;;
  1029.            }}}}
  1030.  
  1031.        To be able to access the two parameters that were pushed
  1032.        onto the stack after they return from _p_e_r_l___c_a_l_l___p_v it is
  1033.        necessary to make a note of their addresses - thus the two
  1034.        variables ssssvvvvaaaa and ssssvvvvbbbb.
  1035.  
  1036.        The reason this is necessary is that the area of the Perl
  1037.        stack which held them will very likely have been
  1038.        overwritten by something else by the time control returns
  1039.        from _p_e_r_l___c_a_l_l___p_v.
  1040.  
  1041.        UUUUssssiiiinnnngggg GGGG____EEEEVVVVAAAALLLL
  1042.  
  1043.        Now an example using G_EVAL. Below is a Perl subroutine
  1044.        which computes the difference of its 2 parameters. If this
  1045.        would result in a negative result, the subroutine calls
  1046.        _d_i_e.
  1047.  
  1048.            ssssuuuubbbb SSSSuuuubbbbttttrrrraaaacccctttt
  1049.            {{{{
  1050.                mmmmyyyy (((($$$$aaaa,,,, $$$$bbbb)))) ==== @@@@____ ;;;;
  1051.  
  1052.  
  1053.  
  1054. 29/Jan/96                perl 5.002 with                       16
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060. PERLCALL(1)    User Contributed Perl Documentation    PERLCALL(1)
  1061.  
  1062.  
  1063.                ddddiiiieeee """"ddddeeeeaaaatttthhhh ccccaaaannnn bbbbeeee ffffaaaattttaaaallll\\\\nnnn"""" iiiiffff $$$$aaaa <<<< $$$$bbbb ;;;;
  1064.  
  1065.                $$$$aaaa ---- $$$$bbbb ;;;;
  1066.            }}}}
  1067.  
  1068.        and some C to call it
  1069.  
  1070.            ssssttttaaaattttiiiicccc vvvvooooiiiidddd
  1071.            ccccaaaallllllll____SSSSuuuubbbbttttrrrraaaacccctttt((((aaaa,,,, bbbb))))
  1072.            iiiinnnntttt aaaa ;;;;
  1073.            iiiinnnntttt bbbb ;;;;
  1074.            {{{{
  1075.                ddddSSSSPPPP ;;;;
  1076.                iiiinnnntttt ccccoooouuuunnnntttt ;;;;
  1077.  
  1078.                EEEENNNNTTTTEEEERRRR ;;;;
  1079.                SSSSAAAAVVVVEEEETTTTMMMMPPPPSSSS;;;;
  1080.  
  1081.                PPPPUUUUSSSSHHHHMMMMAAAARRRRKKKK((((sssspppp)))) ;;;;
  1082.                XXXXPPPPUUUUSSSSHHHHssss((((ssssvvvv____2222mmmmoooorrrrttttaaaallll((((nnnneeeewwwwSSSSVVVViiiivvvv((((aaaa))))))))))));;;;
  1083.                XXXXPPPPUUUUSSSSHHHHssss((((ssssvvvv____2222mmmmoooorrrrttttaaaallll((((nnnneeeewwwwSSSSVVVViiiivvvv((((bbbb))))))))))));;;;
  1084.                PPPPUUUUTTTTBBBBAAAACCCCKKKK ;;;;
  1085.  
  1086.                ccccoooouuuunnnntttt ==== ppppeeeerrrrllll____ccccaaaallllllll____ppppvvvv((((""""SSSSuuuubbbbttttrrrraaaacccctttt"""",,,, GGGG____EEEEVVVVAAAALLLL||||GGGG____SSSSCCCCAAAALLLLAAAARRRR))));;;;
  1087.  
  1088.                SSSSPPPPAAAAGGGGAAAAIIIINNNN ;;;;
  1089.  
  1090.                ////**** CCCChhhheeeecccckkkk tttthhhheeee eeeevvvvaaaallll ffffiiiirrrrsssstttt ****////
  1091.                iiiiffff ((((SSSSvvvvTTTTRRRRUUUUEEEE((((GGGGvvvvSSSSVVVV((((eeeerrrrrrrrggggvvvv))))))))))))
  1092.                {{{{
  1093.                    pppprrrriiiinnnnttttffff ((((""""UUUUhhhh oooohhhh ---- %%%%ssss\\\\nnnn"""",,,, SSSSvvvvPPPPVVVV((((GGGGvvvvSSSSVVVV((((eeeerrrrrrrrggggvvvv)))),,,, nnnnaaaa)))))))) ;;;;
  1094.                    PPPPOOOOPPPPssss ;;;;
  1095.                }}}}
  1096.                eeeellllsssseeee
  1097.                {{{{
  1098.                    iiiiffff ((((ccccoooouuuunnnntttt !!!!==== 1111))))
  1099.                       ccccrrrrooooaaaakkkk((((""""ccccaaaallllllll____SSSSuuuubbbbttttrrrraaaacccctttt:::: wwwwaaaannnntttteeeedddd 1111 vvvvaaaalllluuuueeee ffffrrrroooommmm ''''SSSSuuuubbbbttttrrrraaaacccctttt'''',,,, ggggooootttt %%%%dddd\\\\nnnn"""",,,,
  1100.                                ccccoooouuuunnnntttt)))) ;;;;
  1101.  
  1102.                    pppprrrriiiinnnnttttffff ((((""""%%%%dddd ---- %%%%dddd ==== %%%%dddd\\\\nnnn"""",,,, aaaa,,,, bbbb,,,, PPPPOOOOPPPPiiii)))) ;;;;
  1103.                }}}}
  1104.  
  1105.                PPPPUUUUTTTTBBBBAAAACCCCKKKK ;;;;
  1106.                FFFFRRRREEEEEEEETTTTMMMMPPPPSSSS ;;;;
  1107.                LLLLEEEEAAAAVVVVEEEE ;;;;
  1108.            }}}}
  1109.  
  1110.        If _c_a_l_l___S_u_b_t_r_a_c_t is called thus
  1111.  
  1112.            ccccaaaallllllll____SSSSuuuubbbbttttrrrraaaacccctttt((((4444,,,, 5555))))
  1113.  
  1114.        the following will be printed
  1115.  
  1116.            UUUUhhhh oooohhhh ---- ddddeeeeaaaatttthhhh ccccaaaannnn bbbbeeee ffffaaaattttaaaallll
  1117.  
  1118.  
  1119.  
  1120. 29/Jan/96                perl 5.002 with                       17
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126. PERLCALL(1)    User Contributed Perl Documentation    PERLCALL(1)
  1127.  
  1128.  
  1129.        Notes
  1130.  
  1131.        1.   We want to be able to catch the _d_i_e so we have used
  1132.             the G_EVAL flag.  Not specifying this flag would mean
  1133.             that the program would terminate immediately at the
  1134.             _d_i_e statement in the subroutine _S_u_b_t_r_a_c_t.
  1135.  
  1136.        2.   The code
  1137.  
  1138.                 iiiiffff ((((SSSSvvvvTTTTRRRRUUUUEEEE((((GGGGvvvvSSSSVVVV((((eeeerrrrrrrrggggvvvv))))))))))))
  1139.                 {{{{
  1140.                     pppprrrriiiinnnnttttffff ((((""""UUUUhhhh oooohhhh ---- %%%%ssss\\\\nnnn"""",,,, SSSSvvvvPPPPVVVV((((GGGGvvvvSSSSVVVV((((eeeerrrrrrrrggggvvvv)))),,,, nnnnaaaa)))))))) ;;;;
  1141.                     PPPPOOOOPPPPssss ;;;;
  1142.                 }}}}
  1143.  
  1144.             is the direct equivalent of this bit of Perl
  1145.  
  1146.                 pppprrrriiiinnnntttt """"UUUUhhhh oooohhhh ---- $$$$@@@@\\\\nnnn"""" iiiiffff $$$$@@@@ ;;;;
  1147.  
  1148.             eeeerrrrrrrrggggvvvv is a perl global of type GGGGVVVV **** that points to
  1149.             the symbol table entry containing the error.
  1150.             GGGGvvvvSSSSVVVV((((eeeerrrrrrrrggggvvvv)))) therefore refers to the C equivalent of
  1151.             $$$$@@@@.
  1152.  
  1153.        3.   Note that the stack is popped using PPPPOOOOPPPPssss in the block
  1154.             where SSSSvvvvTTTTRRRRUUUUEEEE((((GGGGvvvvSSSSVVVV((((eeeerrrrrrrrggggvvvv)))))))) is true.  This is necessary
  1155.             because whenever a _p_e_r_l___c_a_l_l___* function invoked with
  1156.             G_EVAL|G_SCALAR returns an error, the top of the
  1157.             stack holds the value _u_n_d_e_f. Since we want the
  1158.             program to continue after detecting this error, it is
  1159.             essential that the stack is tidied up by removing the
  1160.             _u_n_d_e_f.
  1161.  
  1162.        UUUUssssiiiinnnngggg GGGG____KKKKEEEEEEEEPPPPEEEERRRRRRRR
  1163.  
  1164.        Consider this rather facetious example, where we have used
  1165.        an XS version of the call_Subtract example above inside a
  1166.        destructor:
  1167.  
  1168.            ppppaaaacccckkkkaaaaggggeeee FFFFoooooooo;;;;
  1169.            ssssuuuubbbb nnnneeeewwww {{{{ bbbblllleeeessssssss {{{{}}}},,,, $$$$____[[[[0000]]]] }}}}
  1170.            ssssuuuubbbb SSSSuuuubbbbttttrrrraaaacccctttt {{{{
  1171.                mmmmyyyy(((($$$$aaaa,,,,$$$$bbbb)))) ==== @@@@____;;;;
  1172.                ddddiiiieeee """"ddddeeeeaaaatttthhhh ccccaaaannnn bbbbeeee ffffaaaattttaaaallll"""" iiiiffff $$$$aaaa <<<< $$$$bbbb ;;;;
  1173.                $$$$aaaa ---- $$$$bbbb;;;;
  1174.            }}}}
  1175.            ssssuuuubbbb DDDDEEEESSSSTTTTRRRROOOOYYYY {{{{ ccccaaaallllllll____SSSSuuuubbbbttttrrrraaaacccctttt((((5555,,,, 4444))));;;; }}}}
  1176.            ssssuuuubbbb ffffoooooooo {{{{ ddddiiiieeee """"ffffoooooooo ddddiiiieeeessss"""";;;; }}}}
  1177.  
  1178.            ppppaaaacccckkkkaaaaggggeeee mmmmaaaaiiiinnnn;;;;
  1179.            eeeevvvvaaaallll {{{{ FFFFoooooooo---->>>>nnnneeeewwww---->>>>ffffoooooooo }}}};;;;
  1180.            pppprrrriiiinnnntttt """"SSSSaaaawwww:::: $$$$@@@@"""" iiiiffff $$$$@@@@;;;;             #### sssshhhhoooouuuulllldddd bbbbeeee,,,, bbbbuuuutttt iiiissssnnnn''''tttt
  1181.  
  1182.        This example will fail to recognize that an error occurred
  1183.  
  1184.  
  1185.  
  1186. 29/Jan/96                perl 5.002 with                       18
  1187.  
  1188.  
  1189.  
  1190.  
  1191.  
  1192. PERLCALL(1)    User Contributed Perl Documentation    PERLCALL(1)
  1193.  
  1194.  
  1195.        inside the eeeevvvvaaaallll {{{{}}}}.  Here's why: the call_Subtract code
  1196.        got executed while perl was cleaning up temporaries when
  1197.        exiting the eval block, and since call_Subtract is
  1198.        implemented with _p_e_r_l___c_a_l_l___p_v using the G_EVAL flag, it
  1199.        promptly reset $$$$@@@@.  This results in the failure of the
  1200.        outermost test for $$$$@@@@, and thereby the failure of the
  1201.        error trap.
  1202.  
  1203.        Appending the G_KEEPERR flag, so that the _p_e_r_l___c_a_l_l___p_v
  1204.        call in call_Subtract reads:
  1205.  
  1206.                ccccoooouuuunnnntttt ==== ppppeeeerrrrllll____ccccaaaallllllll____ppppvvvv((((""""SSSSuuuubbbbttttrrrraaaacccctttt"""",,,, GGGG____EEEEVVVVAAAALLLL||||GGGG____SSSSCCCCAAAALLLLAAAARRRR||||GGGG____KKKKEEEEEEEEPPPPEEEERRRRRRRR))));;;;
  1207.  
  1208.        will preserve the error and restore reliable error
  1209.        handling.
  1210.  
  1211.        UUUUssssiiiinnnngggg ppppeeeerrrrllll____ccccaaaallllllll____ssssvvvv
  1212.  
  1213.        In all the previous examples I have 'hard-wired' the name
  1214.        of the Perl subroutine to be called from C.  Most of the
  1215.        time though, it is more convenient to be able to specify
  1216.        the name of the Perl subroutine from within the Perl
  1217.        script.
  1218.  
  1219.        Consider the Perl code below
  1220.  
  1221.            ssssuuuubbbb ffffrrrreeeedddd
  1222.            {{{{
  1223.                pppprrrriiiinnnntttt """"HHHHeeeelllllllloooo tttthhhheeeerrrreeee\\\\nnnn"""" ;;;;
  1224.            }}}}
  1225.  
  1226.            CCCCaaaallllllllSSSSuuuubbbbPPPPVVVV((((""""ffffrrrreeeedddd"""")))) ;;;;
  1227.  
  1228.        Here is a snippet of XSUB which defines _C_a_l_l_S_u_b_P_V.
  1229.  
  1230.            vvvvooooiiiidddd
  1231.            CCCCaaaallllllllSSSSuuuubbbbPPPPVVVV((((nnnnaaaammmmeeee))))
  1232.                cccchhhhaaaarrrr ****  nnnnaaaammmmeeee
  1233.                CCCCOOOODDDDEEEE::::
  1234.                PPPPUUUUSSSSHHHHMMMMAAAARRRRKKKK((((sssspppp)))) ;;;;
  1235.                ppppeeeerrrrllll____ccccaaaallllllll____ppppvvvv((((nnnnaaaammmmeeee,,,, GGGG____DDDDIIIISSSSCCCCAAAARRRRDDDD||||GGGG____NNNNOOOOAAAARRRRGGGGSSSS)))) ;;;;
  1236.  
  1237.        That is fine as far as it goes. The thing is, the Perl
  1238.        subroutine can be specified only as a string.  For Perl 4
  1239.        this was adequate, but Perl 5 allows references to
  1240.        subroutines and anonymous subroutines.  This is where
  1241.        _p_e_r_l___c_a_l_l___s_v is useful.
  1242.  
  1243.        The code below for _C_a_l_l_S_u_b_S_V is identical to _C_a_l_l_S_u_b_P_V
  1244.        except that the nnnnaaaammmmeeee parameter is now defined as an SV*
  1245.        and we use _p_e_r_l___c_a_l_l___s_v instead of _p_e_r_l___c_a_l_l___p_v.
  1246.  
  1247.  
  1248.  
  1249.  
  1250.  
  1251.  
  1252. 29/Jan/96                perl 5.002 with                       19
  1253.  
  1254.  
  1255.  
  1256.  
  1257.  
  1258. PERLCALL(1)    User Contributed Perl Documentation    PERLCALL(1)
  1259.  
  1260.  
  1261.            vvvvooooiiiidddd
  1262.            CCCCaaaallllllllSSSSuuuubbbbSSSSVVVV((((nnnnaaaammmmeeee))))
  1263.                SSSSVVVV ****    nnnnaaaammmmeeee
  1264.                CCCCOOOODDDDEEEE::::
  1265.                PPPPUUUUSSSSHHHHMMMMAAAARRRRKKKK((((sssspppp)))) ;;;;
  1266.                ppppeeeerrrrllll____ccccaaaallllllll____ssssvvvv((((nnnnaaaammmmeeee,,,, GGGG____DDDDIIIISSSSCCCCAAAARRRRDDDD||||GGGG____NNNNOOOOAAAARRRRGGGGSSSS)))) ;;;;
  1267.  
  1268.        Since we are using an SV to call _f_r_e_d the following can
  1269.        all be used
  1270.  
  1271.            CCCCaaaallllllllSSSSuuuubbbbSSSSVVVV((((""""ffffrrrreeeedddd"""")))) ;;;;
  1272.            CCCCaaaallllllllSSSSuuuubbbbSSSSVVVV((((\\\\&&&&ffffrrrreeeedddd)))) ;;;;
  1273.            $$$$rrrreeeeffff ==== \\\\&&&&ffffrrrreeeedddd ;;;;
  1274.            CCCCaaaallllllllSSSSuuuubbbbSSSSVVVV(((($$$$rrrreeeeffff)))) ;;;;
  1275.            CCCCaaaallllllllSSSSuuuubbbbSSSSVVVV(((( ssssuuuubbbb {{{{ pppprrrriiiinnnntttt """"HHHHeeeelllllllloooo tttthhhheeeerrrreeee\\\\nnnn"""" }}}} )))) ;;;;
  1276.  
  1277.        As you can see, _p_e_r_l___c_a_l_l___s_v gives you much greater
  1278.        flexibility in how you can specify the Perl subroutine.
  1279.  
  1280.        You should note that if it is necessary to store the SV
  1281.        (nnnnaaaammmmeeee in the example above) which corresponds to the Perl
  1282.        subroutine so that it can be used later in the program, it
  1283.        not enough to just store a copy of the pointer to the SV.
  1284.        Say the code above had been like this
  1285.  
  1286.            ssssttttaaaattttiiiicccc SSSSVVVV **** rrrreeeemmmmeeeemmmmbbbbeeeerrrrSSSSuuuubbbb ;;;;
  1287.  
  1288.            vvvvooooiiiidddd
  1289.            SSSSaaaavvvveeeeSSSSuuuubbbb1111((((nnnnaaaammmmeeee))))
  1290.                SSSSVVVV ****    nnnnaaaammmmeeee
  1291.                CCCCOOOODDDDEEEE::::
  1292.                rrrreeeemmmmeeeemmmmbbbbeeeerrrrSSSSuuuubbbb ==== nnnnaaaammmmeeee ;;;;
  1293.  
  1294.            vvvvooooiiiidddd
  1295.            CCCCaaaallllllllSSSSaaaavvvveeeeddddSSSSuuuubbbb1111(((())))
  1296.                CCCCOOOODDDDEEEE::::
  1297.                PPPPUUUUSSSSHHHHMMMMAAAARRRRKKKK((((sssspppp)))) ;;;;
  1298.                ppppeeeerrrrllll____ccccaaaallllllll____ssssvvvv((((rrrreeeemmmmeeeemmmmbbbbeeeerrrrSSSSuuuubbbb,,,, GGGG____DDDDIIIISSSSCCCCAAAARRRRDDDD||||GGGG____NNNNOOOOAAAARRRRGGGGSSSS)))) ;;;;
  1299.  
  1300.        The reason this is wrong is that by the time you come to
  1301.        use the pointer rrrreeeemmmmeeeemmmmbbbbeeeerrrrSSSSuuuubbbb in CCCCaaaallllllllSSSSaaaavvvveeeeddddSSSSuuuubbbb1111, it may or
  1302.        may not still refer to the Perl subroutine that was
  1303.        recorded in SSSSaaaavvvveeeeSSSSuuuubbbb1111.  This is particularly true for these
  1304.        cases
  1305.  
  1306.            SSSSaaaavvvveeeeSSSSuuuubbbb1111((((\\\\&&&&ffffrrrreeeedddd)))) ;;;;
  1307.            CCCCaaaallllllllSSSSaaaavvvveeeeddddSSSSuuuubbbb1111(((()))) ;;;;
  1308.  
  1309.            SSSSaaaavvvveeeeSSSSuuuubbbb1111(((( ssssuuuubbbb {{{{ pppprrrriiiinnnntttt """"HHHHeeeelllllllloooo tttthhhheeeerrrreeee\\\\nnnn"""" }}}} )))) ;;;;
  1310.            CCCCaaaallllllllSSSSaaaavvvveeeeddddSSSSuuuubbbb1111(((()))) ;;;;
  1311.  
  1312.        By the time each of the SSSSaaaavvvveeeeSSSSuuuubbbb1111 statements above have
  1313.        been executed, the SV*'s which corresponded to the
  1314.        parameters will no longer exist.  Expect an error message
  1315.  
  1316.  
  1317.  
  1318. 29/Jan/96                perl 5.002 with                       20
  1319.  
  1320.  
  1321.  
  1322.  
  1323.  
  1324. PERLCALL(1)    User Contributed Perl Documentation    PERLCALL(1)
  1325.  
  1326.  
  1327.        from Perl of the form
  1328.  
  1329.            CCCCaaaannnn''''tttt uuuusssseeee aaaannnn uuuunnnnddddeeeeffffiiiinnnneeeedddd vvvvaaaalllluuuueeee aaaassss aaaa ssssuuuubbbbrrrroooouuuuttttiiiinnnneeee rrrreeeeffffeeeerrrreeeennnncccceeee aaaatttt ............
  1330.  
  1331.        for each of the CCCCaaaallllllllSSSSaaaavvvveeeeddddSSSSuuuubbbb1111 lines.
  1332.  
  1333.        Similarly, with this code
  1334.  
  1335.            $$$$rrrreeeeffff ==== \\\\&&&&ffffrrrreeeedddd ;;;;
  1336.            SSSSaaaavvvveeeeSSSSuuuubbbb1111(((($$$$rrrreeeeffff)))) ;;;;
  1337.            $$$$rrrreeeeffff ==== 44447777 ;;;;
  1338.            CCCCaaaallllllllSSSSaaaavvvveeeeddddSSSSuuuubbbb1111(((()))) ;;;;
  1339.  
  1340.        you can expect one of these messages (which you actually
  1341.        get is dependant on the version of Perl you are using)
  1342.  
  1343.            NNNNooootttt aaaa CCCCOOOODDDDEEEE rrrreeeeffffeeeerrrreeeennnncccceeee aaaatttt ............
  1344.            UUUUnnnnddddeeeeffffiiiinnnneeeedddd ssssuuuubbbbrrrroooouuuuttttiiiinnnneeee &&&&mmmmaaaaiiiinnnn::::::::44447777 ccccaaaalllllllleeeedddd ............
  1345.  
  1346.        The variable $$$$rrrreeeeffff may have referred to the subroutine ffffrrrreeeedddd
  1347.        whenever the call to SSSSaaaavvvveeeeSSSSuuuubbbb1111 was made but by the time
  1348.        CCCCaaaallllllllSSSSaaaavvvveeeeddddSSSSuuuubbbb1111 gets called it now holds the number 44447777.
  1349.        Since we saved only a pointer to the original SV in
  1350.        SSSSaaaavvvveeeeSSSSuuuubbbb1111, any changes to $$$$rrrreeeeffff will be tracked by the
  1351.        pointer rrrreeeemmmmeeeemmmmbbbbeeeerrrrSSSSuuuubbbb. This means that whenever
  1352.        CCCCaaaallllllllSSSSaaaavvvveeeeddddSSSSuuuubbbb1111 gets called, it will attempt to execute the
  1353.        code which is referenced by the SV* rrrreeeemmmmeeeemmmmbbbbeeeerrrrSSSSuuuubbbb.  In this
  1354.        case though, it now refers to the integer 44447777, so expect
  1355.        Perl to complain loudly.
  1356.  
  1357.        A similar but more subtle problem is illustrated with this
  1358.        code
  1359.  
  1360.            $$$$rrrreeeeffff ==== \\\\&&&&ffffrrrreeeedddd ;;;;
  1361.            SSSSaaaavvvveeeeSSSSuuuubbbb1111(((($$$$rrrreeeeffff)))) ;;;;
  1362.            $$$$rrrreeeeffff ==== \\\\&&&&jjjjooooeeee ;;;;
  1363.            CCCCaaaallllllllSSSSaaaavvvveeeeddddSSSSuuuubbbb1111(((()))) ;;;;
  1364.  
  1365.        This time whenever CCCCaaaallllllllSSSSaaaavvvveeeeddddSSSSuuuubbbb1111 get called it will
  1366.        execute the Perl subroutine jjjjooooeeee (assuming it exists)
  1367.        rather than ffffrrrreeeedddd as was originally requested in the call
  1368.        to SSSSaaaavvvveeeeSSSSuuuubbbb1111.
  1369.  
  1370.        To get around these problems it is necessary to take a
  1371.        full copy of the SV.  The code below shows SSSSaaaavvvveeeeSSSSuuuubbbb2222
  1372.        modified to do that
  1373.  
  1374.            ssssttttaaaattttiiiicccc SSSSVVVV **** kkkkeeeeeeeeppppSSSSuuuubbbb ==== ((((SSSSVVVV****))))NNNNUUUULLLLLLLL ;;;;
  1375.  
  1376.  
  1377.  
  1378.  
  1379.  
  1380.  
  1381.  
  1382.  
  1383.  
  1384. 29/Jan/96                perl 5.002 with                       21
  1385.  
  1386.  
  1387.  
  1388.  
  1389.  
  1390. PERLCALL(1)    User Contributed Perl Documentation    PERLCALL(1)
  1391.  
  1392.  
  1393.            vvvvooooiiiidddd
  1394.            SSSSaaaavvvveeeeSSSSuuuubbbb2222((((nnnnaaaammmmeeee))))
  1395.                SSSSVVVV ****    nnnnaaaammmmeeee
  1396.                CCCCOOOODDDDEEEE::::
  1397.                ////**** TTTTaaaakkkkeeee aaaa ccccooooppppyyyy ooooffff tttthhhheeee ccccaaaallllllllbbbbaaaacccckkkk ****////
  1398.                iiiiffff ((((kkkkeeeeeeeeppppSSSSuuuubbbb ======== ((((SSSSVVVV****))))NNNNUUUULLLLLLLL))))
  1399.                    ////**** FFFFiiiirrrrsssstttt ttttiiiimmmmeeee,,,, ssssoooo ccccrrrreeeeaaaatttteeee aaaa nnnneeeewwww SSSSVVVV ****////
  1400.                    kkkkeeeeeeeeppppSSSSuuuubbbb ==== nnnneeeewwwwSSSSVVVVssssvvvv((((nnnnaaaammmmeeee)))) ;;;;
  1401.                eeeellllsssseeee
  1402.                    ////**** BBBBeeeeeeeennnn hhhheeeerrrreeee bbbbeeeeffffoooorrrreeee,,,, ssssoooo oooovvvveeeerrrrwwwwrrrriiiitttteeee ****////
  1403.                    SSSSvvvvSSSSeeeettttSSSSVVVV((((kkkkeeeeeeeeppppSSSSuuuubbbb,,,, nnnnaaaammmmeeee)))) ;;;;
  1404.  
  1405.            vvvvooooiiiidddd
  1406.            CCCCaaaallllllllSSSSaaaavvvveeeeddddSSSSuuuubbbb2222(((())))
  1407.                CCCCOOOODDDDEEEE::::
  1408.                PPPPUUUUSSSSHHHHMMMMAAAARRRRKKKK((((sssspppp)))) ;;;;
  1409.                ppppeeeerrrrllll____ccccaaaallllllll____ssssvvvv((((kkkkeeeeeeeeppppSSSSuuuubbbb,,,, GGGG____DDDDIIIISSSSCCCCAAAARRRRDDDD||||GGGG____NNNNOOOOAAAARRRRGGGGSSSS)))) ;;;;
  1410.  
  1411.        In order to avoid creating a new SV every time SSSSaaaavvvveeeeSSSSuuuubbbb2222 is
  1412.        called, the function first checks to see if it has been
  1413.        called before.  If not, then space for a new SV is
  1414.        allocated and the reference to the Perl subroutine, nnnnaaaammmmeeee
  1415.        is copied to the variable kkkkeeeeeeeeppppSSSSuuuubbbb in one operation using
  1416.        nnnneeeewwwwSSSSVVVVssssvvvv.  Thereafter, whenever SSSSaaaavvvveeeeSSSSuuuubbbb2222 is called the
  1417.        existing SV, kkkkeeeeeeeeppppSSSSuuuubbbb, is overwritten with the new value
  1418.        using SSSSvvvvSSSSeeeettttSSSSVVVV.
  1419.  
  1420.        UUUUssssiiiinnnngggg ppppeeeerrrrllll____ccccaaaallllllll____aaaarrrrggggvvvv
  1421.  
  1422.        Here is a Perl subroutine which prints whatever parameters
  1423.        are passed to it.
  1424.  
  1425.            ssssuuuubbbb PPPPrrrriiiinnnnttttLLLLiiiisssstttt
  1426.            {{{{
  1427.                mmmmyyyy((((@@@@lllliiiisssstttt)))) ==== @@@@____ ;;;;
  1428.  
  1429.                ffffoooorrrreeeeaaaacccchhhh ((((@@@@lllliiiisssstttt)))) {{{{ pppprrrriiiinnnntttt """"$$$$____\\\\nnnn"""" }}}}
  1430.            }}}}
  1431.  
  1432.        and here is an example of _p_e_r_l___c_a_l_l___a_r_g_v which will call
  1433.        _P_r_i_n_t_L_i_s_t.
  1434.  
  1435.            ssssttttaaaattttiiiicccc cccchhhhaaaarrrr **** wwwwoooorrrrddddssss[[[[]]]] ==== {{{{""""aaaallllpppphhhhaaaa"""",,,, """"bbbbeeeettttaaaa"""",,,, """"ggggaaaammmmmmmmaaaa"""",,,, """"ddddeeeellllttttaaaa"""",,,, NNNNUUUULLLLLLLL}}}} ;;;;
  1436.  
  1437.            ssssttttaaaattttiiiicccc vvvvooooiiiidddd
  1438.            ccccaaaallllllll____PPPPrrrriiiinnnnttttLLLLiiiisssstttt(((())))
  1439.            {{{{
  1440.                ddddSSSSPPPP ;;;;
  1441.  
  1442.                ppppeeeerrrrllll____ccccaaaallllllll____aaaarrrrggggvvvv((((""""PPPPrrrriiiinnnnttttLLLLiiiisssstttt"""",,,, GGGG____DDDDIIIISSSSCCCCAAAARRRRDDDD,,,, wwwwoooorrrrddddssss)))) ;;;;
  1443.            }}}}
  1444.  
  1445.        Note that it is not necessary to call PPPPUUUUSSSSHHHHMMMMAAAARRRRKKKK in this
  1446.        instance.  This is because _p_e_r_l___c_a_l_l___a_r_g_v will do it for
  1447.  
  1448.  
  1449.  
  1450. 29/Jan/96                perl 5.002 with                       22
  1451.  
  1452.  
  1453.  
  1454.  
  1455.  
  1456. PERLCALL(1)    User Contributed Perl Documentation    PERLCALL(1)
  1457.  
  1458.  
  1459.        you.
  1460.  
  1461.        UUUUssssiiiinnnngggg ppppeeeerrrrllll____ccccaaaallllllll____mmmmeeeetttthhhhoooodddd
  1462.  
  1463.        Consider the following Perl code
  1464.  
  1465.            {{{{
  1466.                ppppaaaacccckkkkaaaaggggeeee MMMMiiiinnnneeee ;;;;
  1467.  
  1468.                ssssuuuubbbb nnnneeeewwww
  1469.                {{{{
  1470.                    mmmmyyyy(((($$$$ttttyyyyppppeeee)))) ==== sssshhhhiiiifffftttt ;;;;
  1471.                    bbbblllleeeessssssss [[[[@@@@____]]]]
  1472.                }}}}
  1473.  
  1474.                ssssuuuubbbb DDDDiiiissssppppllllaaaayyyy
  1475.                {{{{
  1476.                    mmmmyyyy (((($$$$sssseeeellllffff,,,, $$$$iiiinnnnddddeeeexxxx)))) ==== @@@@____ ;;;;
  1477.                    pppprrrriiiinnnntttt """"$$$$iiiinnnnddddeeeexxxx:::: $$$$$$$$sssseeeellllffff[[[[$$$$iiiinnnnddddeeeexxxx]]]]\\\\nnnn"""" ;;;;
  1478.                }}}}
  1479.  
  1480.                ssssuuuubbbb PPPPrrrriiiinnnnttttIIIIDDDD
  1481.                {{{{
  1482.                    mmmmyyyy(((($$$$ccccllllaaaassssssss)))) ==== @@@@____ ;;;;
  1483.                    pppprrrriiiinnnntttt """"TTTThhhhiiiissss iiiissss CCCCllllaaaassssssss $$$$ccccllllaaaassssssss vvvveeeerrrrssssiiiioooonnnn 1111....0000\\\\nnnn"""" ;;;;
  1484.                }}}}
  1485.            }}}}
  1486.  
  1487.        It just implements a very simple class to manage an array.
  1488.        Apart from the constructor, nnnneeeewwww, it declares methods, one
  1489.        static and one virtual. The static method, PPPPrrrriiiinnnnttttIIIIDDDD, simply
  1490.        prints out the class name and a version number. The
  1491.        virtual method, DDDDiiiissssppppllllaaaayyyy, prints out a single element of
  1492.        the array.  Here is an all Perl example of using it.
  1493.  
  1494.            $$$$aaaa ==== nnnneeeewwww MMMMiiiinnnneeee ((((''''rrrreeeedddd'''',,,, ''''ggggrrrreeeeeeeennnn'''',,,, ''''bbbblllluuuueeee'''')))) ;;;;
  1495.            $$$$aaaa---->>>>DDDDiiiissssppppllllaaaayyyy((((1111)))) ;;;;
  1496.            PPPPrrrriiiinnnnttttIIIIDDDD MMMMiiiinnnneeee;;;;
  1497.  
  1498.        will print
  1499.  
  1500.            1111:::: ggggrrrreeeeeeeennnn
  1501.            TTTThhhhiiiissss iiiissss CCCCllllaaaassssssss MMMMiiiinnnneeee vvvveeeerrrrssssiiiioooonnnn 1111....0000
  1502.  
  1503.        Calling a Perl method from C is fairly straightforward.
  1504.        The following things are required
  1505.  
  1506.        +o    a reference to the object for a virtual method or the
  1507.             name of the class for a static method.
  1508.  
  1509.        +o    the name of the method.
  1510.  
  1511.        +o    any other parameters specific to the method.
  1512.  
  1513.  
  1514.  
  1515.  
  1516. 29/Jan/96                perl 5.002 with                       23
  1517.  
  1518.  
  1519.  
  1520.  
  1521.  
  1522. PERLCALL(1)    User Contributed Perl Documentation    PERLCALL(1)
  1523.  
  1524.  
  1525.        Here is a simple XSUB which illustrates the mechanics of
  1526.        calling both the PPPPrrrriiiinnnnttttIIIIDDDD and DDDDiiiissssppppllllaaaayyyy methods from C.
  1527.  
  1528.            vvvvooooiiiidddd
  1529.            ccccaaaallllllll____MMMMeeeetttthhhhoooodddd((((rrrreeeeffff,,,, mmmmeeeetttthhhhoooodddd,,,, iiiinnnnddddeeeexxxx))))
  1530.                SSSSVVVV ****    rrrreeeeffff
  1531.                cccchhhhaaaarrrr ****  mmmmeeeetttthhhhoooodddd
  1532.                iiiinnnntttt             iiiinnnnddddeeeexxxx
  1533.                CCCCOOOODDDDEEEE::::
  1534.                PPPPUUUUSSSSHHHHMMMMAAAARRRRKKKK((((sssspppp))));;;;
  1535.                XXXXPPPPUUUUSSSSHHHHssss((((rrrreeeeffff))));;;;
  1536.                XXXXPPPPUUUUSSSSHHHHssss((((ssssvvvv____2222mmmmoooorrrrttttaaaallll((((nnnneeeewwwwSSSSVVVViiiivvvv((((iiiinnnnddddeeeexxxx)))))))))))) ;;;;
  1537.                PPPPUUUUTTTTBBBBAAAACCCCKKKK;;;;
  1538.  
  1539.                ppppeeeerrrrllll____ccccaaaallllllll____mmmmeeeetttthhhhoooodddd((((mmmmeeeetttthhhhoooodddd,,,, GGGG____DDDDIIIISSSSCCCCAAAARRRRDDDD)))) ;;;;
  1540.  
  1541.            vvvvooooiiiidddd
  1542.            ccccaaaallllllll____PPPPrrrriiiinnnnttttIIIIDDDD((((ccccllllaaaassssssss,,,, mmmmeeeetttthhhhoooodddd))))
  1543.                cccchhhhaaaarrrr ****  ccccllllaaaassssssss
  1544.                cccchhhhaaaarrrr ****  mmmmeeeetttthhhhoooodddd
  1545.                CCCCOOOODDDDEEEE::::
  1546.                PPPPUUUUSSSSHHHHMMMMAAAARRRRKKKK((((sssspppp))));;;;
  1547.                XXXXPPPPUUUUSSSSHHHHssss((((ssssvvvv____2222mmmmoooorrrrttttaaaallll((((nnnneeeewwwwSSSSVVVVppppvvvv((((ccccllllaaaassssssss,,,, 0000)))))))))))) ;;;;
  1548.                PPPPUUUUTTTTBBBBAAAACCCCKKKK;;;;
  1549.  
  1550.                ppppeeeerrrrllll____ccccaaaallllllll____mmmmeeeetttthhhhoooodddd((((mmmmeeeetttthhhhoooodddd,,,, GGGG____DDDDIIIISSSSCCCCAAAARRRRDDDD)))) ;;;;
  1551.  
  1552.        So the methods PPPPrrrriiiinnnnttttIIIIDDDD and DDDDiiiissssppppllllaaaayyyy can be invoked like
  1553.        this
  1554.  
  1555.            $$$$aaaa ==== nnnneeeewwww MMMMiiiinnnneeee ((((''''rrrreeeedddd'''',,,, ''''ggggrrrreeeeeeeennnn'''',,,, ''''bbbblllluuuueeee'''')))) ;;;;
  1556.            ccccaaaallllllll____MMMMeeeetttthhhhoooodddd(((($$$$aaaa,,,, ''''DDDDiiiissssppppllllaaaayyyy'''',,,, 1111)))) ;;;;
  1557.            ccccaaaallllllll____PPPPrrrriiiinnnnttttIIIIDDDD((((''''MMMMiiiinnnneeee'''',,,, ''''PPPPrrrriiiinnnnttttIIIIDDDD'''')))) ;;;;
  1558.  
  1559.        The only thing to note is that in both the static and
  1560.        virtual methods, the method name is not passed via the
  1561.        stack - it is used as the first parameter to
  1562.        _p_e_r_l___c_a_l_l___m_e_t_h_o_d.
  1563.  
  1564.        UUUUssssiiiinnnngggg GGGGIIIIMMMMMMMMEEEE
  1565.  
  1566.        Here is a trivial XSUB which prints the context in which
  1567.        it is currently executing.
  1568.  
  1569.            vvvvooooiiiidddd
  1570.            PPPPrrrriiiinnnnttttCCCCoooonnnntttteeeexxxxtttt(((())))
  1571.                CCCCOOOODDDDEEEE::::
  1572.                iiiiffff ((((GGGGIIIIMMMMMMMMEEEE ======== GGGG____SSSSCCCCAAAALLLLAAAARRRR))))
  1573.                    pppprrrriiiinnnnttttffff ((((""""CCCCoooonnnntttteeeexxxxtttt iiiissss SSSSccccaaaallllaaaarrrr\\\\nnnn"""")))) ;;;;
  1574.                eeeellllsssseeee
  1575.                    pppprrrriiiinnnnttttffff ((((""""CCCCoooonnnntttteeeexxxxtttt iiiissss AAAArrrrrrrraaaayyyy\\\\nnnn"""")))) ;;;;
  1576.  
  1577.        and here is some Perl to test it
  1578.  
  1579.  
  1580.  
  1581.  
  1582. 29/Jan/96                perl 5.002 with                       24
  1583.  
  1584.  
  1585.  
  1586.  
  1587.  
  1588. PERLCALL(1)    User Contributed Perl Documentation    PERLCALL(1)
  1589.  
  1590.  
  1591.            $$$$aaaa ==== PPPPrrrriiiinnnnttttCCCCoooonnnntttteeeexxxxtttt ;;;;
  1592.            @@@@aaaa ==== PPPPrrrriiiinnnnttttCCCCoooonnnntttteeeexxxxtttt ;;;;
  1593.  
  1594.        The output from that will be
  1595.  
  1596.            CCCCoooonnnntttteeeexxxxtttt iiiissss SSSSccccaaaallllaaaarrrr
  1597.            CCCCoooonnnntttteeeexxxxtttt iiiissss AAAArrrrrrrraaaayyyy
  1598.  
  1599.  
  1600.        UUUUssssiiiinnnngggg PPPPeeeerrrrllll ttttoooo ddddiiiissssppppoooosssseeee ooooffff tttteeeemmmmppppoooorrrraaaarrrriiiieeeessss
  1601.  
  1602.        In the examples given to date, any temporaries created in
  1603.        the callback (i.e. parameters passed on the stack to the
  1604.        _p_e_r_l___c_a_l_l___* function or values returned via the stack)
  1605.        have been freed by one of these methods
  1606.  
  1607.        +o    specifying the G_DISCARD flag with _p_e_r_l___c_a_l_l___*.
  1608.  
  1609.        +o    explicitly disposed of using the EEEENNNNTTTTEEEERRRR/SSSSAAAAVVVVEEEETTTTMMMMPPPPSSSS -
  1610.             FFFFRRRREEEEEEEETTTTMMMMPPPPSSSS/LLLLEEEEAAAAVVVVEEEE pairing.
  1611.  
  1612.        There is another method which can be used, namely letting
  1613.        Perl do it for you automatically whenever it regains
  1614.        control after the callback has terminated.  This is done
  1615.        by simply not using the
  1616.  
  1617.            EEEENNNNTTTTEEEERRRR ;;;;
  1618.            SSSSAAAAVVVVEEEETTTTMMMMPPPPSSSS ;;;;
  1619.            ............
  1620.            FFFFRRRREEEEEEEETTTTMMMMPPPPSSSS ;;;;
  1621.            LLLLEEEEAAAAVVVVEEEE ;;;;
  1622.  
  1623.        sequence in the callback (and not, of course, specifying
  1624.        the G_DISCARD flag).
  1625.  
  1626.        If you are going to use this method you have to be aware
  1627.        of a possible memory leak which can arise under very
  1628.        specific circumstances.  To explain these circumstances
  1629.        you need to know a bit about the flow of control between
  1630.        Perl and the callback routine.
  1631.  
  1632.        The examples given at the start of the document (an error
  1633.        handler and an event driven program) are typical of the
  1634.        two main sorts of flow control that you are likely to
  1635.        encounter with callbacks.  There is a very important
  1636.        distinction between them, so pay attention.
  1637.  
  1638.        In the first example, an error handler, the flow of
  1639.        control could be as follows.  You have created an
  1640.        interface to an external library.  Control can reach the
  1641.        external library like this
  1642.  
  1643.            ppppeeeerrrrllll -------->>>> XXXXSSSSUUUUBBBB -------->>>> eeeexxxxtttteeeerrrrnnnnaaaallll lllliiiibbbbrrrraaaarrrryyyy
  1644.  
  1645.  
  1646.  
  1647.  
  1648. 29/Jan/96                perl 5.002 with                       25
  1649.  
  1650.  
  1651.  
  1652.  
  1653.  
  1654. PERLCALL(1)    User Contributed Perl Documentation    PERLCALL(1)
  1655.  
  1656.  
  1657.        Whilst control is in the library, an error condition
  1658.        occurs. You have previously set up a Perl callback to
  1659.        handle this situation, so it will get executed. Once the
  1660.        callback has finished, control will drop back to Perl
  1661.        again.  Here is what the flow of control will be like in
  1662.        that situation
  1663.  
  1664.            ppppeeeerrrrllll -------->>>> XXXXSSSSUUUUBBBB -------->>>> eeeexxxxtttteeeerrrrnnnnaaaallll lllliiiibbbbrrrraaaarrrryyyy
  1665.                              ............
  1666.                              eeeerrrrrrrroooorrrr ooooccccccccuuuurrrrssss
  1667.                              ............
  1668.                              eeeexxxxtttteeeerrrrnnnnaaaallll lllliiiibbbbrrrraaaarrrryyyy -------->>>> ppppeeeerrrrllll____ccccaaaallllllll -------->>>> ppppeeeerrrrllll
  1669.                                                                  ||||
  1670.            ppppeeeerrrrllll <<<<-------- XXXXSSSSUUUUBBBB <<<<-------- eeeexxxxtttteeeerrrrnnnnaaaallll lllliiiibbbbrrrraaaarrrryyyy <<<<-------- ppppeeeerrrrllll____ccccaaaallllllll <<<<----------------++++
  1671.  
  1672.        After processing of the error using _p_e_r_l___c_a_l_l___* is
  1673.        completed, control reverts back to Perl more or less
  1674.        immediately.
  1675.  
  1676.        In the diagram, the further right you go the more deeply
  1677.        nested the scope is.  It is only when control is back with
  1678.        perl on the extreme left of the diagram that you will have
  1679.        dropped back to the enclosing scope and any temporaries
  1680.        you have left hanging around will be freed.
  1681.  
  1682.        In the second example, an event driven program, the flow
  1683.        of control will be more like this
  1684.  
  1685.            ppppeeeerrrrllll -------->>>> XXXXSSSSUUUUBBBB -------->>>> eeeevvvveeeennnntttt hhhhaaaannnnddddlllleeeerrrr
  1686.                              ............
  1687.                              eeeevvvveeeennnntttt hhhhaaaannnnddddlllleeeerrrr -------->>>> ppppeeeerrrrllll____ccccaaaallllllll -------->>>> ppppeeeerrrrllll
  1688.                                                               ||||
  1689.                              eeeevvvveeeennnntttt hhhhaaaannnnddddlllleeeerrrr <<<<-------- ppppeeeerrrrllll____ccccaaaallllllll --------<<<<--------++++
  1690.                              ............
  1691.                              eeeevvvveeeennnntttt hhhhaaaannnnddddlllleeeerrrr -------->>>> ppppeeeerrrrllll____ccccaaaallllllll -------->>>> ppppeeeerrrrllll
  1692.                                                               ||||
  1693.                              eeeevvvveeeennnntttt hhhhaaaannnnddddlllleeeerrrr <<<<-------- ppppeeeerrrrllll____ccccaaaallllllll --------<<<<--------++++
  1694.                              ............
  1695.                              eeeevvvveeeennnntttt hhhhaaaannnnddddlllleeeerrrr -------->>>> ppppeeeerrrrllll____ccccaaaallllllll -------->>>> ppppeeeerrrrllll
  1696.                                                               ||||
  1697.                              eeeevvvveeeennnntttt hhhhaaaannnnddddlllleeeerrrr <<<<-------- ppppeeeerrrrllll____ccccaaaallllllll --------<<<<--------++++
  1698.  
  1699.        In this case the flow of control can consist of only the
  1700.        repeated sequence
  1701.  
  1702.            eeeevvvveeeennnntttt hhhhaaaannnnddddlllleeeerrrr -------->>>> ppppeeeerrrrllll____ccccaaaallllllll -------->>>> ppppeeeerrrrllll
  1703.  
  1704.        for the practically the complete duration of the program.
  1705.        This means that control may _n_e_v_e_r drop back to the
  1706.        surrounding scope in Perl at the extreme left.
  1707.  
  1708.        So what is the big problem? Well, if you are expecting
  1709.        Perl to tidy up those temporaries for you, you might be in
  1710.        for a long wait.  For Perl to actually dispose of your
  1711.  
  1712.  
  1713.  
  1714. 29/Jan/96                perl 5.002 with                       26
  1715.  
  1716.  
  1717.  
  1718.  
  1719.  
  1720. PERLCALL(1)    User Contributed Perl Documentation    PERLCALL(1)
  1721.  
  1722.  
  1723.        temporaries, control must drop back to the enclosing scope
  1724.        at some stage.  In the event driven scenario that may
  1725.        never happen.  This means that as time goes on, your
  1726.        program will create more and more temporaries, none of
  1727.        which will ever be freed. As each of these temporaries
  1728.        consumes some memory your program will eventually consume
  1729.        all the available memory in your system - kapow!
  1730.  
  1731.        So here is the bottom line - if you are sure that control
  1732.        will revert back to the enclosing Perl scope fairly
  1733.        quickly after the end of your callback, then it isn't
  1734.        absolutely necessary to explicitly dispose of any
  1735.        temporaries you may have created. Mind you, if you are at
  1736.        all uncertain about what to do, it doesn't do any harm to
  1737.        tidy up anyway.
  1738.  
  1739.        SSSSttttrrrraaaatttteeeeggggiiiieeeessss ffffoooorrrr ssssttttoooorrrriiiinnnngggg CCCCaaaallllllllbbbbaaaacccckkkk CCCCoooonnnntttteeeexxxxtttt IIIInnnnffffoooorrrrmmmmaaaattttiiiioooonnnn
  1740.  
  1741.        Potentially one of the trickiest problems to overcome when
  1742.        designing a callback interface can be figuring out how to
  1743.        store the mapping between the C callback function and the
  1744.        Perl equivalent.
  1745.  
  1746.        To help understand why this can be a real problem first
  1747.        consider how a callback is set up in an all C environment.
  1748.        Typically a C API will provide a function to register a
  1749.        callback.  This will expect a pointer to a function as one
  1750.        of its parameters.  Below is a call to a hypothetical
  1751.        function rrrreeeeggggiiiisssstttteeeerrrr____ffffaaaattttaaaallll which registers the C function to
  1752.        get called when a fatal error occurs.
  1753.  
  1754.            rrrreeeeggggiiiisssstttteeeerrrr____ffffaaaattttaaaallll((((ccccbbbb1111)))) ;;;;
  1755.  
  1756.        The single parameter ccccbbbb1111 is a pointer to a function, so
  1757.        you must have defined ccccbbbb1111 in your code, say something like
  1758.        this
  1759.  
  1760.            ssssttttaaaattttiiiicccc vvvvooooiiiidddd
  1761.            ccccbbbb1111(((())))
  1762.            {{{{
  1763.                pppprrrriiiinnnnttttffff ((((""""FFFFaaaattttaaaallll EEEErrrrrrrroooorrrr\\\\nnnn"""")))) ;;;;
  1764.                eeeexxxxiiiitttt((((1111)))) ;;;;
  1765.            }}}}
  1766.  
  1767.        Now change that to call a Perl subroutine instead
  1768.  
  1769.            ssssttttaaaattttiiiicccc SSSSVVVV **** ccccaaaallllllllbbbbaaaacccckkkk ==== ((((SSSSVVVV****))))NNNNUUUULLLLLLLL;;;;
  1770.  
  1771.            ssssttttaaaattttiiiicccc vvvvooooiiiidddd
  1772.            ccccbbbb1111(((())))
  1773.            {{{{
  1774.                ddddSSSSPPPP ;;;;
  1775.  
  1776.                PPPPUUUUSSSSHHHHMMMMAAAARRRRKKKK((((sssspppp)))) ;;;;
  1777.  
  1778.  
  1779.  
  1780. 29/Jan/96                perl 5.002 with                       27
  1781.  
  1782.  
  1783.  
  1784.  
  1785.  
  1786. PERLCALL(1)    User Contributed Perl Documentation    PERLCALL(1)
  1787.  
  1788.  
  1789.                ////**** CCCCaaaallllllll tttthhhheeee PPPPeeeerrrrllll ssssuuuubbbb ttttoooo pppprrrroooocccceeeessssssss tttthhhheeee ccccaaaallllllllbbbbaaaacccckkkk ****////
  1790.                ppppeeeerrrrllll____ccccaaaallllllll____ssssvvvv((((ccccaaaallllllllbbbbaaaacccckkkk,,,, GGGG____DDDDIIIISSSSCCCCAAAARRRRDDDD)))) ;;;;
  1791.            }}}}
  1792.  
  1793.            vvvvooooiiiidddd
  1794.            rrrreeeeggggiiiisssstttteeeerrrr____ffffaaaattttaaaallll((((ffffnnnn))))
  1795.                SSSSVVVV ****    ffffnnnn
  1796.                CCCCOOOODDDDEEEE::::
  1797.                ////**** RRRReeeemmmmeeeemmmmbbbbeeeerrrr tttthhhheeee PPPPeeeerrrrllll ssssuuuubbbb ****////
  1798.                iiiiffff ((((ccccaaaallllllllbbbbaaaacccckkkk ======== ((((SSSSVVVV****))))NNNNUUUULLLLLLLL))))
  1799.                    ccccaaaallllllllbbbbaaaacccckkkk ==== nnnneeeewwwwSSSSVVVVssssvvvv((((ffffnnnn)))) ;;;;
  1800.                eeeellllsssseeee
  1801.                    SSSSvvvvSSSSeeeettttSSSSVVVV((((ccccaaaallllllllbbbbaaaacccckkkk,,,, ffffnnnn)))) ;;;;
  1802.  
  1803.                ////**** rrrreeeeggggiiiisssstttteeeerrrr tttthhhheeee ccccaaaallllllllbbbbaaaacccckkkk wwwwiiiitttthhhh tttthhhheeee eeeexxxxtttteeeerrrrnnnnaaaallll lllliiiibbbbrrrraaaarrrryyyy ****////
  1804.                rrrreeeeggggiiiisssstttteeeerrrr____ffffaaaattttaaaallll((((ccccbbbb1111)))) ;;;;
  1805.  
  1806.        where the Perl equivalent of rrrreeeeggggiiiisssstttteeeerrrr____ffffaaaattttaaaallll and the
  1807.        callback it registers, ppppccccbbbb1111, might look like this
  1808.  
  1809.            #### RRRReeeeggggiiiisssstttteeeerrrr tttthhhheeee ssssuuuubbbb ppppccccbbbb1111
  1810.            rrrreeeeggggiiiisssstttteeeerrrr____ffffaaaattttaaaallll((((\\\\&&&&ppppccccbbbb1111)))) ;;;;
  1811.  
  1812.            ssssuuuubbbb ppppccccbbbb1111
  1813.            {{{{
  1814.                ddddiiiieeee """"IIII''''mmmm ddddyyyyiiiinnnngggg............\\\\nnnn"""" ;;;;
  1815.            }}}}
  1816.  
  1817.        The mapping between the C callback and the Perl equivalent
  1818.        is stored in the global variable ccccaaaallllllllbbbbaaaacccckkkk.
  1819.  
  1820.        This will be adequate if you ever need to have only 1
  1821.        callback registered at any time. An example could be an
  1822.        error handler like the code sketched out above. Remember
  1823.        though, repeated calls to rrrreeeeggggiiiisssstttteeeerrrr____ffffaaaattttaaaallll will replace the
  1824.        previously registered callback function with the new one.
  1825.  
  1826.        Say for example you want to interface to a library which
  1827.        allows asynchronous file i/o.  In this case you may be
  1828.        able to register a callback whenever a read operation has
  1829.        completed. To be of any use we want to be able to call
  1830.        separate Perl subroutines for each file that is opened.
  1831.        As it stands, the error handler example above would not be
  1832.        adequate as it allows only a single callback to be defined
  1833.        at any time. What we require is a means of storing the
  1834.        mapping between the opened file and the Perl subroutine we
  1835.        want to be called for that file.
  1836.  
  1837.        Say the i/o library has a function aaaassssyyyynnnncccchhhh____rrrreeeeaaaadddd which
  1838.        associates a C function PPPPrrrroooocccceeeessssssssRRRReeeeaaaadddd with a file handle ffffhhhh
  1839.        - this assumes that it has also provided some routine to
  1840.        open the file and so obtain the file handle.
  1841.  
  1842.            aaaassssyyyynnnncccchhhh____rrrreeeeaaaadddd((((ffffhhhh,,,, PPPPrrrroooocccceeeessssssssRRRReeeeaaaadddd))))
  1843.  
  1844.  
  1845.  
  1846. 29/Jan/96                perl 5.002 with                       28
  1847.  
  1848.  
  1849.  
  1850.  
  1851.  
  1852. PERLCALL(1)    User Contributed Perl Documentation    PERLCALL(1)
  1853.  
  1854.  
  1855.        This may expect the C _P_r_o_c_e_s_s_R_e_a_d function of this form
  1856.  
  1857.            vvvvooooiiiidddd
  1858.            PPPPrrrroooocccceeeessssssssRRRReeeeaaaadddd((((ffffhhhh,,,, bbbbuuuuffffffffeeeerrrr))))
  1859.            iiiinnnntttt ffffhhhh ;;;;
  1860.            cccchhhhaaaarrrr ****      bbbbuuuuffffffffeeeerrrr ;;;;
  1861.            {{{{
  1862.                 ............
  1863.            }}}}
  1864.  
  1865.        To provide a Perl interface to this library we need to be
  1866.        able to map between the ffffhhhh parameter and the Perl
  1867.        subroutine we want called.  A hash is a convenient
  1868.        mechanism for storing this mapping.  The code below shows
  1869.        a possible implementation
  1870.  
  1871.            ssssttttaaaattttiiiicccc HHHHVVVV **** MMMMaaaappppppppiiiinnnngggg ==== ((((HHHHVVVV****))))NNNNUUUULLLLLLLL ;;;;
  1872.  
  1873.            vvvvooooiiiidddd
  1874.            aaaassssyyyynnnncccchhhh____rrrreeeeaaaadddd((((ffffhhhh,,,, ccccaaaallllllllbbbbaaaacccckkkk))))
  1875.                iiiinnnntttt     ffffhhhh
  1876.                SSSSVVVV ****    ccccaaaallllllllbbbbaaaacccckkkk
  1877.                CCCCOOOODDDDEEEE::::
  1878.                ////**** IIIIffff tttthhhheeee hhhhaaaasssshhhh ddddooooeeeessssnnnn''''tttt aaaallllrrrreeeeaaaaddddyyyy eeeexxxxiiiisssstttt,,,, ccccrrrreeeeaaaatttteeee iiiitttt ****////
  1879.                iiiiffff ((((MMMMaaaappppppppiiiinnnngggg ======== ((((HHHHVVVV****))))NNNNUUUULLLLLLLL))))
  1880.                    MMMMaaaappppppppiiiinnnngggg ==== nnnneeeewwwwHHHHVVVV(((()))) ;;;;
  1881.  
  1882.                ////**** SSSSaaaavvvveeee tttthhhheeee ffffhhhh ---->>>> ccccaaaallllllllbbbbaaaacccckkkk mmmmaaaappppppppiiiinnnngggg ****////
  1883.                hhhhvvvv____ssssttttoooorrrreeee((((MMMMaaaappppppppiiiinnnngggg,,,, ((((cccchhhhaaaarrrr****))))&&&&ffffhhhh,,,, ssssiiiizzzzeeeeooooffff((((ffffhhhh)))),,,, nnnneeeewwwwSSSSVVVVssssvvvv((((ccccaaaallllllllbbbbaaaacccckkkk)))),,,, 0000)))) ;;;;
  1884.  
  1885.                ////**** RRRReeeeggggiiiisssstttteeeerrrr wwwwiiiitttthhhh tttthhhheeee CCCC LLLLiiiibbbbrrrraaaarrrryyyy ****////
  1886.                aaaassssyyyynnnncccchhhh____rrrreeeeaaaadddd((((ffffhhhh,,,, aaaassssyyyynnnncccchhhh____rrrreeeeaaaadddd____iiiiffff)))) ;;;;
  1887.  
  1888.        and aaaassssyyyynnnncccchhhh____rrrreeeeaaaadddd____iiiiffff could look like this
  1889.  
  1890.            ssssttttaaaattttiiiicccc vvvvooooiiiidddd
  1891.            aaaassssyyyynnnncccchhhh____rrrreeeeaaaadddd____iiiiffff((((ffffhhhh,,,, bbbbuuuuffffffffeeeerrrr))))
  1892.            iiiinnnntttt ffffhhhh ;;;;
  1893.            cccchhhhaaaarrrr ****      bbbbuuuuffffffffeeeerrrr ;;;;
  1894.            {{{{
  1895.                ddddSSSSPPPP ;;;;
  1896.                SSSSVVVV ******** ssssvvvv ;;;;
  1897.  
  1898.                ////**** GGGGeeeetttt tttthhhheeee ccccaaaallllllllbbbbaaaacccckkkk aaaassssssssoooocccciiiiaaaatttteeeedddd wwwwiiiitttthhhh ffffhhhh ****////
  1899.                ssssvvvv ====  hhhhvvvv____ffffeeeettttcccchhhh((((MMMMaaaappppppppiiiinnnngggg,,,, ((((cccchhhhaaaarrrr****))))&&&&ffffhhhh ,,,, ssssiiiizzzzeeeeooooffff((((ffffhhhh)))),,,, FFFFAAAALLLLSSSSEEEE)))) ;;;;
  1900.                iiiiffff ((((ssssvvvv ======== ((((SSSSVVVV********))))NNNNUUUULLLLLLLL))))
  1901.                    ccccrrrrooooaaaakkkk((((""""IIIInnnntttteeeerrrrnnnnaaaallll eeeerrrrrrrroooorrrr............\\\\nnnn"""")))) ;;;;
  1902.  
  1903.                PPPPUUUUSSSSHHHHMMMMAAAARRRRKKKK((((sssspppp)))) ;;;;
  1904.                XXXXPPPPUUUUSSSSHHHHssss((((ssssvvvv____2222mmmmoooorrrrttttaaaallll((((nnnneeeewwwwSSSSVVVViiiivvvv((((ffffhhhh)))))))))))) ;;;;
  1905.                XXXXPPPPUUUUSSSSHHHHssss((((ssssvvvv____2222mmmmoooorrrrttttaaaallll((((nnnneeeewwwwSSSSVVVVppppvvvv((((bbbbuuuuffffffffeeeerrrr,,,, 0000)))))))))))) ;;;;
  1906.                PPPPUUUUTTTTBBBBAAAACCCCKKKK ;;;;
  1907.  
  1908.  
  1909.  
  1910.  
  1911.  
  1912. 29/Jan/96                perl 5.002 with                       29
  1913.  
  1914.  
  1915.  
  1916.  
  1917.  
  1918. PERLCALL(1)    User Contributed Perl Documentation    PERLCALL(1)
  1919.  
  1920.  
  1921.                ////**** CCCCaaaallllllll tttthhhheeee PPPPeeeerrrrllll ssssuuuubbbb ****////
  1922.                ppppeeeerrrrllll____ccccaaaallllllll____ssssvvvv((((****ssssvvvv,,,, GGGG____DDDDIIIISSSSCCCCAAAARRRRDDDD)))) ;;;;
  1923.            }}}}
  1924.  
  1925.        For completeness, here is aaaassssyyyynnnncccchhhh____cccclllloooosssseeee.  This shows how to
  1926.        remove the entry from the hash MMMMaaaappppppppiiiinnnngggg.
  1927.  
  1928.            vvvvooooiiiidddd
  1929.            aaaassssyyyynnnncccchhhh____cccclllloooosssseeee((((ffffhhhh))))
  1930.                iiiinnnntttt     ffffhhhh
  1931.                CCCCOOOODDDDEEEE::::
  1932.                ////**** RRRReeeemmmmoooovvvveeee tttthhhheeee eeeennnnttttrrrryyyy ffffrrrroooommmm tttthhhheeee hhhhaaaasssshhhh ****////
  1933.                ((((vvvvooooiiiidddd)))) hhhhvvvv____ddddeeeelllleeeetttteeee((((MMMMaaaappppppppiiiinnnngggg,,,, ((((cccchhhhaaaarrrr****))))&&&&ffffhhhh,,,, ssssiiiizzzzeeeeooooffff((((ffffhhhh)))),,,, GGGG____DDDDIIIISSSSCCCCAAAARRRRDDDD)))) ;;;;
  1934.  
  1935.                ////**** NNNNoooowwww ccccaaaallllllll tttthhhheeee rrrreeeeaaaallll aaaassssyyyynnnncccchhhh____cccclllloooosssseeee ****////
  1936.                aaaassssyyyynnnncccchhhh____cccclllloooosssseeee((((ffffhhhh)))) ;;;;
  1937.  
  1938.        So the Perl interface would look like this
  1939.  
  1940.            ssssuuuubbbb ccccaaaallllllllbbbbaaaacccckkkk1111
  1941.            {{{{
  1942.                mmmmyyyy(((($$$$hhhhaaaannnnddddlllleeee,,,, $$$$bbbbuuuuffffffffeeeerrrr)))) ==== @@@@____ ;;;;
  1943.            }}}}
  1944.  
  1945.            #### RRRReeeeggggiiiisssstttteeeerrrr tttthhhheeee PPPPeeeerrrrllll ccccaaaallllllllbbbbaaaacccckkkk
  1946.            aaaassssyyyynnnncccchhhh____rrrreeeeaaaadddd(((($$$$ffffhhhh,,,, \\\\&&&&ccccaaaallllllllbbbbaaaacccckkkk1111)))) ;;;;
  1947.  
  1948.            aaaassssyyyynnnncccchhhh____cccclllloooosssseeee(((($$$$ffffhhhh)))) ;;;;
  1949.  
  1950.        The mapping between the C callback and Perl is stored in
  1951.        the global hash MMMMaaaappppppppiiiinnnngggg this time. Using a hash has the
  1952.        distinct advantage that it allows an unlimited number of
  1953.        callbacks to be registered.
  1954.  
  1955.        What if the interface provided by the C callback doesn't
  1956.        contain a parameter which allows the file handle to Perl
  1957.        subroutine mapping?  Say in the asynchronous i/o package,
  1958.        the callback function gets passed only the bbbbuuuuffffffffeeeerrrr
  1959.        parameter like this
  1960.  
  1961.            vvvvooooiiiidddd
  1962.            PPPPrrrroooocccceeeessssssssRRRReeeeaaaadddd((((bbbbuuuuffffffffeeeerrrr))))
  1963.            cccchhhhaaaarrrr ****      bbbbuuuuffffffffeeeerrrr ;;;;
  1964.            {{{{
  1965.                ............
  1966.            }}}}
  1967.  
  1968.        Without the file handle there is no straightforward way to
  1969.        map from the C callback to the Perl subroutine.
  1970.  
  1971.        In this case a possible way around this problem is to pre-
  1972.        define a series of C functions to act as the interface to
  1973.        Perl, thus
  1974.  
  1975.  
  1976.  
  1977.  
  1978. 29/Jan/96                perl 5.002 with                       30
  1979.  
  1980.  
  1981.  
  1982.  
  1983.  
  1984. PERLCALL(1)    User Contributed Perl Documentation    PERLCALL(1)
  1985.  
  1986.  
  1987.            ####ddddeeeeffffiiiinnnneeee MMMMAAAAXXXX____CCCCBBBB              3333
  1988.            ####ddddeeeeffffiiiinnnneeee NNNNUUUULLLLLLLL____HHHHAAAANNNNDDDDLLLLEEEE ----1111
  1989.            ttttyyyyppppeeeeddddeeeeffff vvvvooooiiiidddd ((((****FFFFnnnnMMMMaaaapppp))))(((()))) ;;;;
  1990.  
  1991.            ssssttttrrrruuuucccctttt MMMMaaaappppSSSSttttrrrruuuucccctttt {{{{
  1992.                FFFFnnnnMMMMaaaapppp    FFFFuuuunnnnccccttttiiiioooonnnn ;;;;
  1993.                SSSSVVVV ****     PPPPeeeerrrrllllSSSSuuuubbbb ;;;;
  1994.                iiiinnnntttt      HHHHaaaannnnddddlllleeee ;;;;
  1995.              }}}} ;;;;
  1996.  
  1997.            ssssttttaaaattttiiiicccc vvvvooooiiiidddd  ffffnnnn1111(((()))) ;;;;
  1998.            ssssttttaaaattttiiiicccc vvvvooooiiiidddd  ffffnnnn2222(((()))) ;;;;
  1999.            ssssttttaaaattttiiiicccc vvvvooooiiiidddd  ffffnnnn3333(((()))) ;;;;
  2000.  
  2001.            ssssttttaaaattttiiiicccc ssssttttrrrruuuucccctttt MMMMaaaappppSSSSttttrrrruuuucccctttt MMMMaaaapppp [[[[MMMMAAAAXXXX____CCCCBBBB]]]] ====
  2002.                {{{{
  2003.                    {{{{ ffffnnnn1111,,,, NNNNUUUULLLLLLLL,,,, NNNNUUUULLLLLLLL____HHHHAAAANNNNDDDDLLLLEEEE }}}},,,,
  2004.                    {{{{ ffffnnnn2222,,,, NNNNUUUULLLLLLLL,,,, NNNNUUUULLLLLLLL____HHHHAAAANNNNDDDDLLLLEEEE }}}},,,,
  2005.                    {{{{ ffffnnnn3333,,,, NNNNUUUULLLLLLLL,,,, NNNNUUUULLLLLLLL____HHHHAAAANNNNDDDDLLLLEEEE }}}}
  2006.                }}}} ;;;;
  2007.  
  2008.            ssssttttaaaattttiiiicccc vvvvooooiiiidddd
  2009.            PPPPccccbbbb((((iiiinnnnddddeeeexxxx,,,, bbbbuuuuffffffffeeeerrrr))))
  2010.            iiiinnnntttt iiiinnnnddddeeeexxxx ;;;;
  2011.            cccchhhhaaaarrrr **** bbbbuuuuffffffffeeeerrrr ;;;;
  2012.            {{{{
  2013.                ddddSSSSPPPP ;;;;
  2014.  
  2015.                PPPPUUUUSSSSHHHHMMMMAAAARRRRKKKK((((sssspppp)))) ;;;;
  2016.                XXXXPPPPUUUUSSSSHHHHssss((((ssssvvvv____2222mmmmoooorrrrttttaaaallll((((nnnneeeewwwwSSSSVVVVppppvvvv((((bbbbuuuuffffffffeeeerrrr,,,, 0000)))))))))))) ;;;;
  2017.                PPPPUUUUTTTTBBBBAAAACCCCKKKK ;;;;
  2018.  
  2019.                ////**** CCCCaaaallllllll tttthhhheeee PPPPeeeerrrrllll ssssuuuubbbb ****////
  2020.                ppppeeeerrrrllll____ccccaaaallllllll____ssssvvvv((((MMMMaaaapppp[[[[iiiinnnnddddeeeexxxx]]]]....PPPPeeeerrrrllllSSSSuuuubbbb,,,, GGGG____DDDDIIIISSSSCCCCAAAARRRRDDDD)))) ;;;;
  2021.            }}}}
  2022.  
  2023.            ssssttttaaaattttiiiicccc vvvvooooiiiidddd
  2024.            ffffnnnn1111((((bbbbuuuuffffffffeeeerrrr))))
  2025.            cccchhhhaaaarrrr **** bbbbuuuuffffffffeeeerrrr ;;;;
  2026.            {{{{
  2027.                PPPPccccbbbb((((0000,,,, bbbbuuuuffffffffeeeerrrr)))) ;;;;
  2028.            }}}}
  2029.  
  2030.            ssssttttaaaattttiiiicccc vvvvooooiiiidddd
  2031.            ffffnnnn2222((((bbbbuuuuffffffffeeeerrrr))))
  2032.            cccchhhhaaaarrrr **** bbbbuuuuffffffffeeeerrrr ;;;;
  2033.            {{{{
  2034.                PPPPccccbbbb((((1111,,,, bbbbuuuuffffffffeeeerrrr)))) ;;;;
  2035.            }}}}
  2036.  
  2037.  
  2038.  
  2039.  
  2040.  
  2041.  
  2042.  
  2043.  
  2044. 29/Jan/96                perl 5.002 with                       31
  2045.  
  2046.  
  2047.  
  2048.  
  2049.  
  2050. PERLCALL(1)    User Contributed Perl Documentation    PERLCALL(1)
  2051.  
  2052.  
  2053.            ssssttttaaaattttiiiicccc vvvvooooiiiidddd
  2054.            ffffnnnn3333((((bbbbuuuuffffffffeeeerrrr))))
  2055.            cccchhhhaaaarrrr **** bbbbuuuuffffffffeeeerrrr ;;;;
  2056.            {{{{
  2057.                PPPPccccbbbb((((2222,,,, bbbbuuuuffffffffeeeerrrr)))) ;;;;
  2058.            }}}}
  2059.  
  2060.            vvvvooooiiiidddd
  2061.            aaaarrrrrrrraaaayyyy____aaaassssyyyynnnncccchhhh____rrrreeeeaaaadddd((((ffffhhhh,,,, ccccaaaallllllllbbbbaaaacccckkkk))))
  2062.                iiiinnnntttt             ffffhhhh
  2063.                SSSSVVVV ****    ccccaaaallllllllbbbbaaaacccckkkk
  2064.                CCCCOOOODDDDEEEE::::
  2065.                iiiinnnntttt iiiinnnnddddeeeexxxx ;;;;
  2066.                iiiinnnntttt nnnnuuuullllllll____iiiinnnnddddeeeexxxx ==== MMMMAAAAXXXX____CCCCBBBB ;;;;
  2067.  
  2068.                ////**** FFFFiiiinnnndddd tttthhhheeee ssssaaaammmmeeee hhhhaaaannnnddddlllleeee oooorrrr aaaannnn eeeemmmmppppttttyyyy eeeennnnttttrrrryyyy ****////
  2069.                ffffoooorrrr ((((iiiinnnnddddeeeexxxx ==== 0000 ;;;; iiiinnnnddddeeeexxxx <<<< MMMMAAAAXXXX____CCCCBBBB ;;;; ++++++++iiiinnnnddddeeeexxxx))))
  2070.                {{{{
  2071.                    iiiiffff ((((MMMMaaaapppp[[[[iiiinnnnddddeeeexxxx]]]]....HHHHaaaannnnddddlllleeee ======== ffffhhhh))))
  2072.                        bbbbrrrreeeeaaaakkkk ;;;;
  2073.  
  2074.                    iiiiffff ((((MMMMaaaapppp[[[[iiiinnnnddddeeeexxxx]]]]....HHHHaaaannnnddddlllleeee ======== NNNNUUUULLLLLLLL____HHHHAAAANNNNDDDDLLLLEEEE))))
  2075.                        nnnnuuuullllllll____iiiinnnnddddeeeexxxx ==== iiiinnnnddddeeeexxxx ;;;;
  2076.                }}}}
  2077.  
  2078.                iiiiffff ((((iiiinnnnddddeeeexxxx ======== MMMMAAAAXXXX____CCCCBBBB &&&&&&&& nnnnuuuullllllll____iiiinnnnddddeeeexxxx ======== MMMMAAAAXXXX____CCCCBBBB))))
  2079.                    ccccrrrrooooaaaakkkk ((((""""TTTToooooooo mmmmaaaannnnyyyy ccccaaaallllllllbbbbaaaacccckkkk ffffuuuunnnnccccttttiiiioooonnnnssss rrrreeeeggggiiiisssstttteeeerrrreeeedddd\\\\nnnn"""")))) ;;;;
  2080.  
  2081.                iiiiffff ((((iiiinnnnddddeeeexxxx ======== MMMMAAAAXXXX____CCCCBBBB))))
  2082.                    iiiinnnnddddeeeexxxx ==== nnnnuuuullllllll____iiiinnnnddddeeeexxxx ;;;;
  2083.  
  2084.                ////**** SSSSaaaavvvveeee tttthhhheeee ffffiiiilllleeee hhhhaaaannnnddddlllleeee ****////
  2085.                MMMMaaaapppp[[[[iiiinnnnddddeeeexxxx]]]]....HHHHaaaannnnddddlllleeee ==== ffffhhhh ;;;;
  2086.  
  2087.                ////**** RRRReeeemmmmeeeemmmmbbbbeeeerrrr tttthhhheeee PPPPeeeerrrrllll ssssuuuubbbb ****////
  2088.                iiiiffff ((((MMMMaaaapppp[[[[iiiinnnnddddeeeexxxx]]]]....PPPPeeeerrrrllllSSSSuuuubbbb ======== ((((SSSSVVVV****))))NNNNUUUULLLLLLLL))))
  2089.                    MMMMaaaapppp[[[[iiiinnnnddddeeeexxxx]]]]....PPPPeeeerrrrllllSSSSuuuubbbb ==== nnnneeeewwwwSSSSVVVVssssvvvv((((ccccaaaallllllllbbbbaaaacccckkkk)))) ;;;;
  2090.                eeeellllsssseeee
  2091.                    SSSSvvvvSSSSeeeettttSSSSVVVV((((MMMMaaaapppp[[[[iiiinnnnddddeeeexxxx]]]]....PPPPeeeerrrrllllSSSSuuuubbbb,,,, ccccaaaallllllllbbbbaaaacccckkkk)))) ;;;;
  2092.  
  2093.                aaaassssyyyynnnncccchhhh____rrrreeeeaaaadddd((((ffffhhhh,,,, MMMMaaaapppp[[[[iiiinnnnddddeeeexxxx]]]]....FFFFuuuunnnnccccttttiiiioooonnnn)))) ;;;;
  2094.  
  2095.            vvvvooooiiiidddd
  2096.            aaaarrrrrrrraaaayyyy____aaaassssyyyynnnncccchhhh____cccclllloooosssseeee((((ffffhhhh))))
  2097.                iiiinnnntttt     ffffhhhh
  2098.                CCCCOOOODDDDEEEE::::
  2099.                iiiinnnntttt iiiinnnnddddeeeexxxx ;;;;
  2100.  
  2101.                ////**** FFFFiiiinnnndddd tttthhhheeee ffffiiiilllleeee hhhhaaaannnnddddlllleeee ****////
  2102.                ffffoooorrrr ((((iiiinnnnddddeeeexxxx ==== 0000;;;; iiiinnnnddddeeeexxxx <<<< MMMMAAAAXXXX____CCCCBBBB ;;;; ++++++++ iiiinnnnddddeeeexxxx))))
  2103.                    iiiiffff ((((MMMMaaaapppp[[[[iiiinnnnddddeeeexxxx]]]]....HHHHaaaannnnddddlllleeee ======== ffffhhhh))))
  2104.                        bbbbrrrreeeeaaaakkkk ;;;;
  2105.  
  2106.  
  2107.  
  2108.  
  2109.  
  2110. 29/Jan/96                perl 5.002 with                       32
  2111.  
  2112.  
  2113.  
  2114.  
  2115.  
  2116. PERLCALL(1)    User Contributed Perl Documentation    PERLCALL(1)
  2117.  
  2118.  
  2119.                iiiiffff ((((iiiinnnnddddeeeexxxx ======== MMMMAAAAXXXX____CCCCBBBB))))
  2120.                    ccccrrrrooooaaaakkkk ((((""""ccccoooouuuulllldddd nnnnooootttt cccclllloooosssseeee ffffhhhh %%%%dddd\\\\nnnn"""",,,, ffffhhhh)))) ;;;;
  2121.  
  2122.                MMMMaaaapppp[[[[iiiinnnnddddeeeexxxx]]]]....HHHHaaaannnnddddlllleeee ==== NNNNUUUULLLLLLLL____HHHHAAAANNNNDDDDLLLLEEEE ;;;;
  2123.                SSSSvvvvRRRREEEEFFFFCCCCNNNNTTTT____ddddeeeecccc((((MMMMaaaapppp[[[[iiiinnnnddddeeeexxxx]]]]....PPPPeeeerrrrllllSSSSuuuubbbb)))) ;;;;
  2124.                MMMMaaaapppp[[[[iiiinnnnddddeeeexxxx]]]]....PPPPeeeerrrrllllSSSSuuuubbbb ==== ((((SSSSVVVV****))))NNNNUUUULLLLLLLL ;;;;
  2125.  
  2126.                aaaassssyyyynnnncccchhhh____cccclllloooosssseeee((((ffffhhhh)))) ;;;;
  2127.  
  2128.        In this case the functions ffffnnnn1111, ffffnnnn2222 and ffffnnnn3333 are used to
  2129.        remember the Perl subroutine to be called. Each of the
  2130.        functions holds a separate hard-wired index which is used
  2131.        in the function PPPPccccbbbb to access the MMMMaaaapppp array and actually
  2132.        call the Perl subroutine.
  2133.  
  2134.        There are some obvious disadvantages with this technique.
  2135.  
  2136.        Firstly, the code is considerably more complex than with
  2137.        the previous example.
  2138.  
  2139.        Secondly, there is a hard-wired limit (in this case 3) to
  2140.        the number of callbacks that can exist simultaneously. The
  2141.        only way to increase the limit is by modifying the code to
  2142.        add more functions and then re-compiling.  None the less,
  2143.        as long as the number of functions is chosen with some
  2144.        care, it is still a workable solution and in some cases is
  2145.        the only one available.
  2146.  
  2147.        To summarize, here are a number of possible methods for
  2148.        you to consider for storing the mapping between C and the
  2149.        Perl callback
  2150.  
  2151.        1. Ignore the problem - Allow only 1 callback
  2152.             For a lot of situations, like interfacing to an error
  2153.             handler, this may be a perfectly adequate solution.
  2154.  
  2155.        2. Create a sequence of callbacks - hard wired limit
  2156.             If it is impossible to tell from the parameters
  2157.             passed back from the C callback what the context is,
  2158.             then you may need to create a sequence of C callback
  2159.             interface functions, and store pointers to each in an
  2160.             array.
  2161.  
  2162.        3. Use a parameter to map to the Perl callback
  2163.             A hash is an ideal mechanism to store the mapping
  2164.             between C and Perl.
  2165.  
  2166.        AAAAlllltttteeeerrrrnnnnaaaatttteeee SSSSttttaaaacccckkkk MMMMaaaannnniiiippppuuuullllaaaattttiiiioooonnnn
  2167.  
  2168.        Although I have made use of only the PPPPOOOOPPPP**** macros to access
  2169.        values returned from Perl subroutines, it is also possible
  2170.        to bypass these macros and read the stack using the SSSSTTTT
  2171.        macro (See the _p_e_r_l_x_s manpage for a full description of
  2172.        the SSSSTTTT macro).
  2173.  
  2174.  
  2175.  
  2176. 29/Jan/96                perl 5.002 with                       33
  2177.  
  2178.  
  2179.  
  2180.  
  2181.  
  2182. PERLCALL(1)    User Contributed Perl Documentation    PERLCALL(1)
  2183.  
  2184.  
  2185.        Most of the time the PPPPOOOOPPPP**** macros should be adequate, the
  2186.        main problem with them is that they force you to process
  2187.        the returned values in sequence. This may not be the most
  2188.        suitable way to process the values in some cases. What we
  2189.        want is to be able to access the stack in a random order.
  2190.        The SSSSTTTT macro as used when coding an XSUB is ideal for this
  2191.        purpose.
  2192.  
  2193.        The code below is the example given in the section
  2194.        _R_e_t_u_r_n_i_n_g _a _l_i_s_t _o_f _v_a_l_u_e_s recoded to use SSSSTTTT instead of
  2195.        PPPPOOOOPPPP****.
  2196.  
  2197.            ssssttttaaaattttiiiicccc vvvvooooiiiidddd
  2198.            ccccaaaallllllll____AAAAddddddddSSSSuuuubbbbttttrrrraaaacccctttt2222((((aaaa,,,, bbbb))))
  2199.            iiiinnnntttt aaaa ;;;;
  2200.            iiiinnnntttt bbbb ;;;;
  2201.            {{{{
  2202.                ddddSSSSPPPP ;;;;
  2203.                IIII33332222 aaaaxxxx ;;;;
  2204.                iiiinnnntttt ccccoooouuuunnnntttt ;;;;
  2205.  
  2206.                EEEENNNNTTTTEEEERRRR ;;;;
  2207.                SSSSAAAAVVVVEEEETTTTMMMMPPPPSSSS;;;;
  2208.  
  2209.                PPPPUUUUSSSSHHHHMMMMAAAARRRRKKKK((((sssspppp)))) ;;;;
  2210.                XXXXPPPPUUUUSSSSHHHHssss((((ssssvvvv____2222mmmmoooorrrrttttaaaallll((((nnnneeeewwwwSSSSVVVViiiivvvv((((aaaa))))))))))));;;;
  2211.                XXXXPPPPUUUUSSSSHHHHssss((((ssssvvvv____2222mmmmoooorrrrttttaaaallll((((nnnneeeewwwwSSSSVVVViiiivvvv((((bbbb))))))))))));;;;
  2212.                PPPPUUUUTTTTBBBBAAAACCCCKKKK ;;;;
  2213.  
  2214.                ccccoooouuuunnnntttt ==== ppppeeeerrrrllll____ccccaaaallllllll____ppppvvvv((((""""AAAAddddddddSSSSuuuubbbbttttrrrraaaacccctttt"""",,,, GGGG____AAAARRRRRRRRAAAAYYYY))));;;;
  2215.  
  2216.                SSSSPPPPAAAAGGGGAAAAIIIINNNN ;;;;
  2217.                sssspppp ----==== ccccoooouuuunnnntttt ;;;;
  2218.                aaaaxxxx ==== ((((sssspppp ---- ssssttttaaaacccckkkk____bbbbaaaasssseeee)))) ++++ 1111 ;;;;
  2219.  
  2220.                iiiiffff ((((ccccoooouuuunnnntttt !!!!==== 2222))))
  2221.                    ccccrrrrooooaaaakkkk((((""""BBBBiiiigggg ttttrrrroooouuuubbbblllleeee\\\\nnnn"""")))) ;;;;
  2222.  
  2223.                pppprrrriiiinnnnttttffff ((((""""%%%%dddd ++++ %%%%dddd ==== %%%%dddd\\\\nnnn"""",,,, aaaa,,,, bbbb,,,, SSSSvvvvIIIIVVVV((((SSSSTTTT((((0000)))))))))))) ;;;;
  2224.                pppprrrriiiinnnnttttffff ((((""""%%%%dddd ---- %%%%dddd ==== %%%%dddd\\\\nnnn"""",,,, aaaa,,,, bbbb,,,, SSSSvvvvIIIIVVVV((((SSSSTTTT((((1111)))))))))))) ;;;;
  2225.  
  2226.                PPPPUUUUTTTTBBBBAAAACCCCKKKK ;;;;
  2227.                FFFFRRRREEEEEEEETTTTMMMMPPPPSSSS ;;;;
  2228.                LLLLEEEEAAAAVVVVEEEE ;;;;
  2229.            }}}}
  2230.  
  2231.        Notes
  2232.  
  2233.        1.   Notice that it was necessary to define the variable
  2234.             aaaaxxxx.  This is because the SSSSTTTT macro expects it to
  2235.             exist.  If we were in an XSUB it would not be
  2236.             necessary to define aaaaxxxx as it is already defined for
  2237.             you.
  2238.  
  2239.  
  2240.  
  2241.  
  2242. 29/Jan/96                perl 5.002 with                       34
  2243.  
  2244.  
  2245.  
  2246.  
  2247.  
  2248. PERLCALL(1)    User Contributed Perl Documentation    PERLCALL(1)
  2249.  
  2250.  
  2251.        2.   The code
  2252.  
  2253.                     SSSSPPPPAAAAGGGGAAAAIIIINNNN ;;;;
  2254.                     sssspppp ----==== ccccoooouuuunnnntttt ;;;;
  2255.                     aaaaxxxx ==== ((((sssspppp ---- ssssttttaaaacccckkkk____bbbbaaaasssseeee)))) ++++ 1111 ;;;;
  2256.  
  2257.             sets the stack up so that we can use the SSSSTTTT macro.
  2258.  
  2259.        3.   Unlike the original coding of this example, the
  2260.             returned values are not accessed in reverse order.
  2261.             So SSSSTTTT((((0000)))) refers to the first value returned by the
  2262.             Perl subroutine and SSSSTTTT((((ccccoooouuuunnnntttt----1111)))) refers to the last.
  2263.  
  2264. SSSSEEEEEEEE AAAALLLLSSSSOOOO
  2265.        the _p_e_r_l_x_s manpage, the _p_e_r_l_g_u_t_s manpage, the _p_e_r_l_e_m_b_e_d
  2266.        manpage
  2267.  
  2268. AAAAUUUUTTTTHHHHOOOORRRR
  2269.        Paul Marquess <pmarquess@bfsec.bt.co.uk>
  2270.  
  2271.        Special thanks to the following people who assisted in the
  2272.        creation of the document.
  2273.  
  2274.        Jeff Okamoto, Tim Bunce, Nick Gianniotis, Steve Kelem,
  2275.        Gurusamy Sarathy and Larry Wall.
  2276.  
  2277. DDDDAAAATTTTEEEE
  2278.        Version 1.2, 16th Jan 1996
  2279.  
  2280.  
  2281.  
  2282.  
  2283.  
  2284.  
  2285.  
  2286.  
  2287.  
  2288.  
  2289.  
  2290.  
  2291.  
  2292.  
  2293.  
  2294.  
  2295.  
  2296.  
  2297.  
  2298.  
  2299.  
  2300.  
  2301.  
  2302.  
  2303.  
  2304.  
  2305.  
  2306.  
  2307.  
  2308. 29/Jan/96                perl 5.002 with                       35
  2309.  
  2310.  
  2311.