Skip to content

Commit a59b17f

Browse files
author
Zefram
committed
more fully document newCONSTSUB{,_flags}()
1 parent 68cd13c commit a59b17f

File tree

1 file changed

+65
-12
lines changed

1 file changed

+65
-12
lines changed

op.c

Lines changed: 65 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10185,9 +10185,11 @@ S_process_special_blocks(pTHX_ I32 floor, const char *const fullname,
1018510185
}
1018610186

1018710187
/*
10188-
=for apidoc newCONSTSUB
10188+
=for apidoc Am|CV *|newCONSTSUB|HV *stash|const char *name|SV *sv
1018910189
10190-
See L</newCONSTSUB_flags>.
10190+
Behaves like L</newCONSTSUB_flags>, except that C<name> is nul-terminated
10191+
rather than of counted length, and no flags are set. (This means that
10192+
C<name> is always interpreted as Latin-1.)
1019110193
1019210194
=cut
1019310195
*/
@@ -10199,20 +10201,71 @@ Perl_newCONSTSUB(pTHX_ HV *stash, const char *name, SV *sv)
1019910201
}
1020010202

1020110203
/*
10202-
=for apidoc newCONSTSUB_flags
10204+
=for apidoc Am|CV *|newCONSTSUB_flags|HV *stash|const char *name|STRLEN len|U32 flags|SV *sv
10205+
10206+
Construct a constant subroutine, also performing some surrounding
10207+
jobs. A scalar constant-valued subroutine is eligible for inlining
10208+
at compile-time, and in Perl code can be created by S<C<sub FOO () {
10209+
123 }>>. Other kinds of constant subroutine have other treatment.
10210+
10211+
The subroutine will have an empty prototype and will ignore any arguments
10212+
when called. Its constant behaviour is determined by C<sv>. If C<sv>
10213+
is null, the subroutine will yield an empty list. If C<sv> points to a
10214+
scalar, the subroutine will always yield that scalar. If C<sv> points
10215+
to an array, the subroutine will always yield a list of the elements of
10216+
that array in list context, or the number of elements in the array in
10217+
scalar context. This function takes ownership of one counted reference
10218+
to the scalar or array, and will arrange for the object to live as long
10219+
as the subroutine does. If C<sv> points to a scalar then the inlining
10220+
assumes that the value of the scalar will never change, so the caller
10221+
must ensure that the scalar is not subsequently written to. If C<sv>
10222+
points to an array then no such assumption is made, so it is ostensibly
10223+
safe to mutate the array or its elements, but whether this is really
10224+
supported has not been determined.
10225+
10226+
The subroutine will have C<CvFILE> set according to C<PL_curcop>.
10227+
Other aspects of the subroutine will be left in their default state.
10228+
The caller is free to mutate the subroutine beyond its initial state
10229+
after this function has returned.
1020310230
10204-
Creates a constant sub equivalent to Perl S<C<sub FOO () { 123 }>> which is
10205-
eligible for inlining at compile-time.
10231+
If C<name> is null then the subroutine will be anonymous, with its
10232+
C<CvGV> referring to an C<__ANON__> glob. If C<name> is non-null then the
10233+
subroutine will be named accordingly, referenced by the appropriate glob.
10234+
C<name> is a string of length C<len> bytes giving a sigilless symbol
10235+
name, in UTF-8 if C<flags> has the C<SVf_UTF8> bit set and in Latin-1
10236+
otherwise. The name may be either qualified or unqualified. If the
10237+
name is unqualified then it defaults to being in the stash specified by
10238+
C<stash> if that is non-null, or to C<PL_curstash> if C<stash> is null.
10239+
The symbol is always added to the stash if necessary, with C<GV_ADDMULTI>
10240+
semantics.
1020610241
10207-
Currently, the only useful value for C<flags> is C<SVf_UTF8>.
10242+
C<flags> should not have bits set other than C<SVf_UTF8>.
1020810243
10209-
The newly created subroutine takes ownership of a reference to the passed in
10210-
SV.
10244+
If there is already a subroutine of the specified name, then the new sub
10245+
will replace the existing one in the glob. A warning may be generated
10246+
about the redefinition.
1021110247
10212-
Passing C<NULL> for SV creates a constant sub equivalent to S<C<sub BAR () {}>>,
10213-
which won't be called if used as a destructor, but will suppress the overhead
10214-
of a call to C<AUTOLOAD>. (This form, however, isn't eligible for inlining at
10215-
compile time.)
10248+
If the subroutine has one of a few special names, such as C<BEGIN> or
10249+
C<END>, then it will be claimed by the appropriate queue for automatic
10250+
running of phase-related subroutines. In this case the relevant glob will
10251+
be left not containing any subroutine, even if it did contain one before.
10252+
Execution of the subroutine will likely be a no-op, unless C<sv> was
10253+
a tied array or the caller modified the subroutine in some interesting
10254+
way before it was executed. In the case of C<BEGIN>, the treatment is
10255+
buggy: the sub will be executed when only half built, and may be deleted
10256+
prematurely, possibly causing a crash.
10257+
10258+
The function returns a pointer to the constructed subroutine. If the sub
10259+
is anonymous then ownership of one counted reference to the subroutine
10260+
is transferred to the caller. If the sub is named then the caller does
10261+
not get ownership of a reference. In most such cases, where the sub
10262+
has a non-phase name, the sub will be alive at the point it is returned
10263+
by virtue of being contained in the glob that names it. A phase-named
10264+
subroutine will usually be alive by virtue of the reference owned by
10265+
the phase's automatic run queue. A C<BEGIN> subroutine may have been
10266+
destroyed already by the time this function returns, but currently bugs
10267+
occur in that case before the caller gets control. It is the caller's
10268+
responsibility to ensure that it knows which of these situations applies.
1021610269
1021710270
=cut
1021810271
*/

0 commit comments

Comments
 (0)