Skip to content

Commit 1b7e058

Browse files
committed
perlapi: Combine all forms of gv_fetchmeth()
1 parent b786e5e commit 1b7e058

File tree

1 file changed

+74
-66
lines changed

1 file changed

+74
-66
lines changed

gv.c

Lines changed: 74 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -723,55 +723,101 @@ S_maybe_add_coresub(pTHX_ HV * const stash, GV *gv,
723723

724724
/*
725725
=for apidoc gv_fetchmeth
726+
=for apidoc_item gv_fetchmeth_autoload
726727
=for apidoc_item gv_fetchmeth_pv
728+
=for apidoc_item gv_fetchmeth_pv_autoload
727729
=for apidoc_item gv_fetchmeth_pvn
730+
=for apidoc_item gv_fetchmeth_pvn_autoload
728731
=for apidoc_item gv_fetchmeth_sv
732+
=for apidoc_item gv_fetchmeth_sv_autoload
729733
730734
These each look for a glob with name C<name>, containing a defined subroutine,
731735
returning the GV of that glob if found, or C<NULL> if not.
732736
733-
C<stash> is always searched (first), unless it is C<NULL>.
737+
You probably want to use the C<L</gv_fetchmethod>> family of functions
738+
instead.
734739
735-
If C<stash> is NULL, or was searched but nothing was found in it, and the
736-
C<GV_SUPER> bit is set in C<flags>, stashes accessible via C<@ISA> are searched
737-
next. Searching is conducted according to L<C<MRO> order|perlmroapi>.
740+
Searching is always done in the following order, with some steps skipped
741+
depending on various criteria. The first match found is used, ending the
742+
search. C<gv_fetchmeth_pv> and C<gv_fetchmeth_pv_autoload> lack a flags
743+
parameter, so in the following, consider C<flags> to be zero for those two
744+
functions.
738745
739-
Finally, if no matches were found so far, and the C<GV_NOUNIVERSAL> flag in
740-
C<flags> is not set, C<UNIVERSAL::> is searched.
746+
=over
747+
748+
=item 1
749+
750+
C<stash> is searched first, unless C<stash> either is NULL or C<GV_SUPER> is
751+
set in C<flags>.
752+
753+
=item 2
754+
755+
Stashes accessible via C<@ISA> are searched next.
756+
757+
Searching is conducted according to L<C<MRO> order|perlmroapi>.
758+
759+
=item 3
760+
761+
C<UNIVERSAL::> is searched unless C<GV_NOUNIVERSAL> is set.
762+
763+
=item 4
764+
765+
Autoloaded subroutines are then looked for, but only for the forms whose names
766+
end in C<_autoload>, and when C<stash> is not NULL and C<GV_SUPER> is not set.
767+
768+
=back
741769
742-
The argument C<level> should be either 0 or -1. If -1, the function will
743-
return without any side effects or caching. If 0, the function makes sure
744-
there is a glob named C<name> in C<stash>, creating one if necessary.
745-
The subroutine slot in the glob will be set to any subroutine found in the
746-
C<stash> and C<SUPER::> search, hence caching any C<SUPER::> result. Note that
747-
subroutines found in C<UNIVERSAL::> are not cached.
770+
The argument C<level> should be either 0 or -1.
771+
772+
=over
773+
774+
=item If -1
775+
776+
No method caching is done.
777+
778+
=item If 0
779+
780+
If C<GV_SUPER> is not set in C<flags>, the method found is cached in C<stash>.
781+
782+
If C<GV_SUPER> is set in C<flags>, the method is cached in the super
783+
cache for C<stash>.
784+
785+
If the method is not found a negative cache entry is added.
786+
787+
Note that subroutines found in C<UNIVERSAL::> are not cached,
788+
though this may change.
789+
790+
=back
748791
749792
The GV returned from these may be a method cache entry, which is not visible to
750-
Perl code. So when calling C<call_sv>, you should not use the GV directly;
793+
Perl code. So when calling C<L</call_sv>>, you should not use the GV directly;
751794
instead, you should use the method's CV, which can be obtained from the GV with
752-
the C<GvCV> macro.
795+
the C<GvCV> macro. For an autoloaded subroutine without a stub, C<GvCV()> of
796+
the result may be zero.
753797
754798
The only other significant value for C<flags> is C<SVf_UTF8>, indicating that
755-
C<name> is to be treated as being encoded in UTF-8.
799+
C<name> is to be treated as being encoded in UTF-8. Since plain
800+
C<gv_fetchmeth> and C<gv_fetchmeth_autoload> lack a C<flags> parameter, C<name>
801+
is never UTF-8.
756802
757-
Plain C<gv_fetchmeth> lacks a C<flags> parameter, hence always searches in
758-
C<stash>, then C<UNIVERSAL::>, and C<name> is never UTF-8. Otherwise it is
759-
exactly like C<gv_fetchmeth_pvn>.
803+
Otherwise, the functions behave identically, except as noted below.
760804
761-
The other forms do have a C<flags> parameter, and differ only in how the glob
762-
name is specified.
805+
In C<gv_fetchmeth_pv> and C<gv_fetchmeth_pv_autoload>, C<name> is a C language
806+
NUL-terminated string.
763807
764-
In C<gv_fetchmeth_pv>, C<name> is a C language NUL-terminated string.
808+
In C<gv_fetchmeth>, C<gv_fetchmeth_pvn>, C<gv_fetchmeth_autoload>, and
809+
C<gv_fetchmeth_pvn_autoload>, C<name> points to the first byte of the name, and
810+
an additional parameter, C<len>, specifies its length in bytes. Hence, the
811+
name may contain embedded-NUL characters.
765812
766-
In C<gv_fetchmeth_pvn>, C<name> points to the first byte of the name, and an
767-
additional parameter, C<len>, specifies its length in bytes. Hence, the name
768-
may contain embedded-NUL characters.
769-
770-
In C<gv_fetchmeth_sv>, C<*name> is an SV, and the name is the PV extracted from
771-
that, using L</C<SvPV>>. If the SV is marked as being in UTF-8, the extracted
772-
PV will also be.
813+
In C<gv_fetchmeth_sv> and C<gv_fetchmeth_sv_autoload>, C<*name> is an SV, and
814+
the name is the PV extracted from that, using C<L</SvPV>>. If the SV is marked
815+
as being in UTF-8, the extracted PV will also be. Including C<SVf_UTF8> in
816+
C<flags> will force the name to be considered to be UTF-8 even if the SV is
817+
not so marked.
773818
774819
=for apidoc Amnh||GV_SUPER
820+
=for apidoc Amnh||GV_NOUNIVERSAL
775821
776822
=cut
777823
*/
@@ -996,20 +1042,6 @@ Perl_gv_fetchmeth_pvn(pTHX_ HV *stash, const char *name, STRLEN len, I32 level,
9961042
return gv_fetchmeth_internal(stash, NULL, name, len, level, flags);
9971043
}
9981044

999-
/*
1000-
=for apidoc gv_fetchmeth_autoload
1001-
1002-
This is the old form of L</gv_fetchmeth_pvn_autoload>, which has no flags
1003-
parameter.
1004-
1005-
=for apidoc gv_fetchmeth_sv_autoload
1006-
1007-
Exactly like L</gv_fetchmeth_pvn_autoload>, but takes the name string in the form
1008-
of an SV instead of a string/length pair.
1009-
1010-
=cut
1011-
*/
1012-
10131045
GV *
10141046
Perl_gv_fetchmeth_sv_autoload(pTHX_ HV *stash, SV *namesv, I32 level, U32 flags)
10151047
{
@@ -1022,37 +1054,13 @@ Perl_gv_fetchmeth_sv_autoload(pTHX_ HV *stash, SV *namesv, I32 level, U32 flags)
10221054
return gv_fetchmeth_pvn_autoload(stash, namepv, namelen, level, flags);
10231055
}
10241056

1025-
/*
1026-
=for apidoc gv_fetchmeth_pv_autoload
1027-
1028-
Exactly like L</gv_fetchmeth_pvn_autoload>, but takes a nul-terminated string
1029-
instead of a string/length pair.
1030-
1031-
=cut
1032-
*/
1033-
10341057
GV *
10351058
Perl_gv_fetchmeth_pv_autoload(pTHX_ HV *stash, const char *name, I32 level, U32 flags)
10361059
{
10371060
PERL_ARGS_ASSERT_GV_FETCHMETH_PV_AUTOLOAD;
10381061
return gv_fetchmeth_pvn_autoload(stash, name, strlen(name), level, flags);
10391062
}
10401063

1041-
/*
1042-
=for apidoc gv_fetchmeth_pvn_autoload
1043-
1044-
Same as C<gv_fetchmeth_pvn()>, but looks for autoloaded subroutines too.
1045-
Returns a glob for the subroutine.
1046-
1047-
For an autoloaded subroutine without a GV, will create a GV even
1048-
if C<level < 0>. For an autoloaded subroutine without a stub, C<GvCV()>
1049-
of the result may be zero.
1050-
1051-
Currently, the only significant value for C<flags> is C<SVf_UTF8>.
1052-
1053-
=cut
1054-
*/
1055-
10561064
GV *
10571065
Perl_gv_fetchmeth_pvn_autoload(pTHX_ HV *stash, const char *name, STRLEN len, I32 level, U32 flags)
10581066
{

0 commit comments

Comments
 (0)