@@ -1675,8 +1675,8 @@ it will be treated as the start of a new XSUB.
16751675=back
16761676
16771677The following file-scoped keywords are supported. Note that the
1678- L<SCOPE|/The SCOPE: Keyword> can technically be a file-scoped keyword too,
1679- but is described further down as an XSUB keyword.
1678+ L<SCOPE|/The SCOPE: Keyword and typemap entry > can technically be a
1679+ file-scoped keyword too, but is described further down as an XSUB keyword.
16801680
16811681=head3 The REQUIRE: Keyword
16821682
@@ -2528,7 +2528,7 @@ L<PREINIT|/The PREINIT: Keyword> and L<INPUT|/The INPUT: Keyword>. The
25282528first allows you to inject extra variable declaration lines, while the
25292529latter used to be needed to specify the type of each parameter, but is now
25302530mainly of historical interest. This is also the place for the rarely-used
2531- L<SCOPE|/The SCOPE: Keyword> keyword.
2531+ L<SCOPE|/The SCOPE: Keyword and typemap entry > keyword.
25322532
25332533Note that the keywords described in L<XSUB Generic Keywords> and L<Sharing
25342534XSUB bodies> may also appear in this part, plus the L<C_ARGS|/The C_ARGS:
@@ -2734,43 +2734,61 @@ C<INIT> blocks.
27342734
27352735=back
27362736
2737- =head3 The SCOPE: Keyword
2738-
2739- The SCOPE: keyword allows scoping to be enabled for a particular XSUB.
2740- Its effect is to wrap the main body of the XSUB (i.e. the C<CODE> or
2741- C<PPCODE> or implicit) with an C<ENTER> and C<LEAVE> pair. This has the
2742- effect of clearing any accumulated savestack entries at the end of the
2743- code body. It is disabled by default.
2744-
2745- The SCOPE keyword may appear either within the XSUB body (anywhere before
2746- a C<CODE:> could appear), or just before the XSUB declaration, but part of
2747- the same paragraph (i.e. no intervening blank lines). For example:
2737+ =head3 The SCOPE: Keyword and typemap entry
27482738
2739+ # XSUB-scoped
27492740 void
2750- foo()
2751- INPUT:
2752- ...
2753- PREINIT:
2754- ...
2741+ foo(int i)
27552742 SCOPE: ENABLE
27562743 CODE:
27572744 ...
27582745
2746+ # file-scoped
27592747 SCOPE: ENABLE
27602748 void
2761- bar()
2762- ...
2749+ bar(int i)
2750+ CODE:
2751+ ...
27632752
2764- The first form (within the XSUB body) has been available since perl-5.004,
2765- but was broken by perl-5.12.0 (xsubpp v2.21) and fixed in perl-5.44.0
2766- (xsubpp v3.58). The second form has been available since perl-5.12.0 .
2753+ # typemap entry
2754+ TYPEMAP: <<EOF
2755+ INPUT
2756+ T_MYINT
2757+ $var = my_int($arg); /* SCOPE */
2758+ EOF
27672759
2768- Note that to support potentially complex type mappings, if a typemap entry
2769- used by an XSUB contains a comment like C</* SCOPE */>, then scoping will
2770- be automatically enabled for any XSUB which uses that typemap entry for an
2771- C<INPUT> parameter. This currently only works for parameters whose type
2772- is specified in a separate C<INPUT> line rather than any ANSI-style
2773- declaration (C<foo(int i)>).
2760+ The C<SCOPE> keyword can be used to enable scoping for a particular XSUB
2761+ (disabled by default). Its effect is to wrap the main body of the XSUB
2762+ (including most parameter and return value processing) within an C<{
2763+ ENTER;> and C<LEAVE; }> pair. This has the effect of clearing any
2764+ accumulated savestack entries at the end of the code body. If disabled,
2765+ then the savestack will usually be cleared by the caller anyway, so this
2766+ is a rarely-used keyword.
2767+
2768+ The SCOPE keyword may be either XSUB-scoped or file-scoped (this refers to
2769+ the scope of the keyword within the XS file, not to the scope generated by
2770+ the keyword). For the first, it may appear anywhere in the input part or
2771+ the XSUB. For the latter, it may appear anywhere in file scope, but due to
2772+ a long-standing parser bug, the keyword's state is reset at the start of
2773+ each XSUB, so it will only have any effect if appears just before a XSUB
2774+ declaration and as part of the same paragraph (i.e. with no intervening
2775+ blank lines), such as in the example above. It will only affect the single
2776+ following XSUB.
2777+
2778+ The XSUB-scoped form has been available since perl-5.004, but was broken
2779+ by perl-5.12.0 (F<xsubpp> v2.21) and fixed in perl-5.44.0 (F<xsubpp>
2780+ v3.58). The file-scoped form has been available since perl-5.12.0 .
2781+
2782+ To support potentially complex type mappings, if an C<INPUT> typemap entry
2783+ contains a code comment like C</* SCOPE */>, then scoping will be
2784+ automatically enabled for any XSUB which uses that typemap entry. This
2785+ currently only works for parameters whose type is specified using
2786+ old-style C<INPUT> lines rather than an ANSI-style declaration, i.e. not
2787+ for C<foo(int i)>. In fact, the XS parser, when looking for a SCOPE
2788+ comment in a typemap, is currently very lax: it's actually a
2789+ case-insensitive match of any code comment which contains the text "scope"
2790+ plus anything else. But you shouldn't rely on this; always use the form
2791+ shown here. Even better, just don't use it at all.
27742792
27752793=head2 The XSUB Init Part
27762794
0 commit comments