From: Ilya Zakharevich Date: Fri, 6 Jun 1997 02:58:12 +0000 (+1200) Subject: Updates to perlguts (repost) X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d1c897a19f90ad973e09b0d40809b915aba9d563;p=p5sagit%2Fp5-mst-13.2.git Updates to perlguts (repost) Enjoy, p5p-msgid: 199707152223.SAA00776@monk.mps.ohio-state.edu --- diff --git a/pod/perlguts.pod b/pod/perlguts.pod index 08ba339..ecf8610 100644 --- a/pod/perlguts.pod +++ b/pod/perlguts.pod @@ -904,6 +904,150 @@ This overhead will be comparatively small if the TIE methods are themselves substantial, but if they are only a few statements long, the overhead will not be insignificant. +=head2 Localizing changes + +Perl has a very handy construction + + { + local $var = 2; + ... + } + +This construction is I equivalent to + + { + my $oldvar = $var; + $var = 2; + ... + $var = $oldvar; + } + +The biggest difference is that the first construction would +reinstate the initial value of $var, irrespective of how control exits +the block: C, C, C/C etc. It is a little bit +more efficient as well. + +There is a way to achieve a similar task from C via Perl API: create a +I, and arrange for some changes to be automatically +undone at the end of it, either explicit, or via a non-local exit (via +die()). A I-like construct is created by a pair of +C/C macros (see L). Such a construct may be created specially for some +important localized task, or an existing one (like boundaries of +enclosing Perl subroutine/block, or an existing pair for freeing TMPs) +may be used. (In the second case the overhead of additional +localization must be almost negligible.) Note that any XSUB is +automatically enclosed in an C/C pair. + +Inside such a I the following service is available: + +=over + +=item C + +=item C + +=item C + +=item C + +These macros arrange things to restore the value of integer variable +C at the end of enclosing I. + +=item C + +=item C + +These macros arrange things to restore the value of pointers C and +C

. C must be a pointer of a type which survives conversion to +C and back, C

should be able to survive conversion to C +and back. + +=item C + +The refcount of C would be decremented at the end of +I. This is similar to C, which should (?) be +used instead. + +=item C + +The C is op_free()ed at the end of I. + +=item C + +The chunk of memory which is pointed to by C

is Safefree()ed at the +end of I. + +=item C + +Clears a slot in the current scratchpad which corresponds to C at +the end of I. + +=item C + +The key C of C is deleted at the end of I. The +string pointed to by C is Safefree()ed. If one has a I in +short-lived storage, the corresponding string may be reallocated like +this: + + SAVEDELETE(defstash, savepv(tmpbuf), strlen(tmpbuf)); + +=item C + +At the end of I the function C is called with the +only argument (of type C) C

. + +=item C + +The current offset on the Perl internal stack (cf. C) is restored +at the end of I. + +=back + +The following API list contains functions, thus one needs to +provide pointers to the modifiable data explicitly (either C pointers, +or Perlish Cs). Where the above macros take C, a similar +function takes C. + +=over + +=item C + +Equivalent to Perl code C. + +=item C + +=item C + +Similar to C, but localize C<@gv> and C<%gv>. + +=item C + +Duplicates the current value of C, on the exit from the current +C/C I will restore the value of C +using the stored value. + +=item C + +A variant of C which takes multiple arguments via an array +C of C of length C. + +=item C + +Similar to C, but will reinstate a C. + +=item C + +=item C + +Similar to C, but localize C and C. + +=back + +The C module implements localization of the basic types within the +I. People who are interested in how to localize things in +the containing scope should take a look there too. + =head1 Subroutines =head2 XSUBs and the Argument Stack