3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 * 2005, 2006, 2007, 2008 by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
12 * '"The roots of those mountains must be roots indeed; there must be
13 * great secrets buried there which have not been discovered since the
14 * beginning."' --Gandalf, relating Gollum's history
16 * [p.54 of _The Lord of the Rings_, I/ii: "The Shadow of the Past"]
19 /* This file contains the code that implements the functions in Perl's
20 * UNIVERSAL package, such as UNIVERSAL->can().
22 * It is also used to store XS functions that need to be present in
23 * miniperl for a lack of a better place to put them. It might be
24 * clever to move them to seperate XS files which would then be pulled
25 * in by some to-be-written build process.
29 #define PERL_IN_UNIVERSAL_C
33 #include "perliol.h" /* For the PERLIO_F_XXX */
37 S_get_isa_hash(pTHX_ HV *const stash)
40 struct mro_meta *const meta = HvMROMETA(stash);
42 PERL_ARGS_ASSERT_GET_ISA_HASH;
45 AV *const isa = mro_get_linear_isa(stash);
47 HV *const isa_hash = newHV();
48 /* Linearisation didn't build it for us, so do it here. */
49 SV *const *svp = AvARRAY(isa);
50 SV *const *const svp_end = svp + AvFILLp(isa) + 1;
51 const HEK *const canon_name = HvNAME_HEK(stash);
53 while (svp < svp_end) {
54 (void) hv_store_ent(isa_hash, *svp++, &PL_sv_undef, 0);
57 (void) hv_common(isa_hash, NULL, HEK_KEY(canon_name),
58 HEK_LEN(canon_name), HEK_FLAGS(canon_name),
59 HV_FETCH_ISSTORE, &PL_sv_undef,
60 HEK_HASH(canon_name));
61 (void) hv_store(isa_hash, "UNIVERSAL", 9, &PL_sv_undef, 0);
63 SvREADONLY_on(isa_hash);
72 * Contributed by Graham Barr <Graham.Barr@tiuk.ti.com>
73 * The main guts of traverse_isa was actually copied from gv_fetchmeth
77 S_isa_lookup(pTHX_ HV *stash, const char * const name)
80 const struct mro_meta *const meta = HvMROMETA(stash);
81 HV *const isa = meta->isa ? meta->isa : S_get_isa_hash(aTHX_ stash);
82 STRLEN len = strlen(name);
85 PERL_ARGS_ASSERT_ISA_LOOKUP;
87 if (hv_common(isa, NULL, name, len, 0 /* No "UTF-8" flag possible with only
89 HV_FETCH_ISEXISTS, NULL, 0)) {
90 /* Direct name lookup worked. */
94 /* A stash/class can go by many names (ie. User == main::User), so
95 we use the name in the stash itself, which is canonical. */
96 our_stash = gv_stashpvn(name, len, 0);
99 HEK *const canon_name = HvNAME_HEK(our_stash);
101 if (hv_common(isa, NULL, HEK_KEY(canon_name), HEK_LEN(canon_name),
102 HEK_FLAGS(canon_name),
103 HV_FETCH_ISEXISTS, NULL, HEK_HASH(canon_name))) {
112 =head1 SV Manipulation Functions
114 =for apidoc sv_derived_from
116 Returns a boolean indicating whether the SV is derived from the specified class
117 I<at the C level>. To check derivation at the Perl level, call C<isa()> as a
124 Perl_sv_derived_from(pTHX_ SV *sv, const char *const name)
129 PERL_ARGS_ASSERT_SV_DERIVED_FROM;
136 type = sv_reftype(sv,0);
137 if (type && strEQ(type,name))
139 stash = SvOBJECT(sv) ? SvSTASH(sv) : NULL;
142 stash = gv_stashsv(sv, 0);
145 return stash ? isa_lookup(stash, name) : FALSE;
151 Returns a boolean indicating whether the SV performs a specific, named role.
152 The SV can be a Perl object or the name of a Perl class.
160 Perl_sv_does(pTHX_ SV *sv, const char *const name)
162 const char *classname;
167 PERL_ARGS_ASSERT_SV_DOES;
174 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
175 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv)))) {
180 if (sv_isobject(sv)) {
181 classname = sv_reftype(SvRV(sv),TRUE);
183 classname = SvPV_nolen(sv);
186 if (strEQ(name,classname)) {
193 mXPUSHs(newSVpv(name, 0));
196 methodname = newSVpvs_flags("isa", SVs_TEMP);
197 /* ugly hack: use the SvSCREAM flag so S_method_common
198 * can figure out we're calling DOES() and not isa(),
199 * and report eventual errors correctly. --rgs */
200 SvSCREAM_on(methodname);
201 call_sv(methodname, G_SCALAR | G_METHOD);
204 does_it = SvTRUE( TOPs );
211 PERL_XS_EXPORT_C void XS_UNIVERSAL_isa(pTHX_ CV *cv);
212 PERL_XS_EXPORT_C void XS_UNIVERSAL_can(pTHX_ CV *cv);
213 PERL_XS_EXPORT_C void XS_UNIVERSAL_DOES(pTHX_ CV *cv);
214 PERL_XS_EXPORT_C void XS_UNIVERSAL_VERSION(pTHX_ CV *cv);
216 XS(XS_version_stringify);
217 XS(XS_version_numify);
218 XS(XS_version_normal);
220 XS(XS_version_boolean);
221 #ifdef HASATTRIBUTE_NORETURN
222 XS(XS_version_noop) __attribute__noreturn__;
226 XS(XS_version_is_alpha);
228 XS(XS_version_is_qv);
234 XS(XS_utf8_downgrade);
235 XS(XS_utf8_unicode_to_native);
236 XS(XS_utf8_native_to_unicode);
237 XS(XS_Internals_SvREADONLY);
238 XS(XS_Internals_SvREFCNT);
239 XS(XS_Internals_hv_clear_placehold);
240 XS(XS_PerlIO_get_layers);
241 XS(XS_Internals_hash_seed);
242 XS(XS_Internals_rehash_seed);
243 XS(XS_Internals_HvREHASH);
247 XS(XS_re_regnames_count);
248 XS(XS_re_regexp_pattern);
249 XS(XS_Tie_Hash_NamedCapture_FETCH);
250 XS(XS_Tie_Hash_NamedCapture_STORE);
251 XS(XS_Tie_Hash_NamedCapture_DELETE);
252 XS(XS_Tie_Hash_NamedCapture_CLEAR);
253 XS(XS_Tie_Hash_NamedCapture_EXISTS);
254 XS(XS_Tie_Hash_NamedCapture_FIRSTK);
255 XS(XS_Tie_Hash_NamedCapture_NEXTK);
256 XS(XS_Tie_Hash_NamedCapture_SCALAR);
257 XS(XS_Tie_Hash_NamedCapture_flags);
260 Perl_boot_core_UNIVERSAL(pTHX)
263 static const char file[] = __FILE__;
265 newXS("UNIVERSAL::isa", XS_UNIVERSAL_isa, file);
266 newXS("UNIVERSAL::can", XS_UNIVERSAL_can, file);
267 newXS("UNIVERSAL::DOES", XS_UNIVERSAL_DOES, file);
268 newXS("UNIVERSAL::VERSION", XS_UNIVERSAL_VERSION, file);
270 /* register the overloading (type 'A') magic */
271 PL_amagic_generation++;
272 /* Make it findable via fetchmethod */
273 newXS("version::()", XS_version_noop, file);
274 newXS("version::new", XS_version_new, file);
275 newXS("version::parse", XS_version_new, file);
276 newXS("version::(\"\"", XS_version_stringify, file);
277 newXS("version::stringify", XS_version_stringify, file);
278 newXS("version::(0+", XS_version_numify, file);
279 newXS("version::numify", XS_version_numify, file);
280 newXS("version::normal", XS_version_normal, file);
281 newXS("version::(cmp", XS_version_vcmp, file);
282 newXS("version::(<=>", XS_version_vcmp, file);
283 newXS("version::vcmp", XS_version_vcmp, file);
284 newXS("version::(bool", XS_version_boolean, file);
285 newXS("version::boolean", XS_version_boolean, file);
286 newXS("version::(nomethod", XS_version_noop, file);
287 newXS("version::noop", XS_version_noop, file);
288 newXS("version::is_alpha", XS_version_is_alpha, file);
289 newXS("version::qv", XS_version_qv, file);
290 newXS("version::declare", XS_version_qv, file);
291 newXS("version::is_qv", XS_version_is_qv, file);
293 newXS("utf8::is_utf8", XS_utf8_is_utf8, file);
294 newXS("utf8::valid", XS_utf8_valid, file);
295 newXS("utf8::encode", XS_utf8_encode, file);
296 newXS("utf8::decode", XS_utf8_decode, file);
297 newXS("utf8::upgrade", XS_utf8_upgrade, file);
298 newXS("utf8::downgrade", XS_utf8_downgrade, file);
299 newXS("utf8::native_to_unicode", XS_utf8_native_to_unicode, file);
300 newXS("utf8::unicode_to_native", XS_utf8_unicode_to_native, file);
301 newXSproto("Internals::SvREADONLY",XS_Internals_SvREADONLY, file, "\\[$%@];$");
302 newXSproto("Internals::SvREFCNT",XS_Internals_SvREFCNT, file, "\\[$%@];$");
303 newXSproto("Internals::hv_clear_placeholders",
304 XS_Internals_hv_clear_placehold, file, "\\%");
305 newXSproto("PerlIO::get_layers",
306 XS_PerlIO_get_layers, file, "*;@");
307 /* Providing a Regexp::DESTROY fixes #21347. See test in t/op/ref.t */
308 CvFILE(newCONSTSUB(get_hv("Regexp::", GV_ADD), "DESTROY", NULL))
310 newXSproto("Internals::hash_seed",XS_Internals_hash_seed, file, "");
311 newXSproto("Internals::rehash_seed",XS_Internals_rehash_seed, file, "");
312 newXSproto("Internals::HvREHASH", XS_Internals_HvREHASH, file, "\\%");
313 newXSproto("re::is_regexp", XS_re_is_regexp, file, "$");
314 newXSproto("re::regname", XS_re_regname, file, ";$$");
315 newXSproto("re::regnames", XS_re_regnames, file, ";$");
316 newXSproto("re::regnames_count", XS_re_regnames_count, file, "");
317 newXSproto("re::regexp_pattern", XS_re_regexp_pattern, file, "$");
318 newXS("Tie::Hash::NamedCapture::FETCH", XS_Tie_Hash_NamedCapture_FETCH, file);
319 newXS("Tie::Hash::NamedCapture::STORE", XS_Tie_Hash_NamedCapture_STORE, file);
320 newXS("Tie::Hash::NamedCapture::DELETE", XS_Tie_Hash_NamedCapture_DELETE, file);
321 newXS("Tie::Hash::NamedCapture::CLEAR", XS_Tie_Hash_NamedCapture_CLEAR, file);
322 newXS("Tie::Hash::NamedCapture::EXISTS", XS_Tie_Hash_NamedCapture_EXISTS, file);
323 newXS("Tie::Hash::NamedCapture::FIRSTKEY", XS_Tie_Hash_NamedCapture_FIRSTK, file);
324 newXS("Tie::Hash::NamedCapture::NEXTKEY", XS_Tie_Hash_NamedCapture_NEXTK, file);
325 newXS("Tie::Hash::NamedCapture::SCALAR", XS_Tie_Hash_NamedCapture_SCALAR, file);
326 newXS("Tie::Hash::NamedCapture::flags", XS_Tie_Hash_NamedCapture_flags, file);
330 =for apidoc croak_xs_usage
332 A specialised variant of C<croak()> for emitting the usage message for xsubs
334 croak_xs_usage(cv, "eee_yow");
336 works out the package name and subroutine name from C<cv>, and then calls
337 C<croak()>. Hence if C<cv> is C<&ouch::awk>, it would call C<croak> as:
339 Perl_croak(aTHX_ "Usage %s::%s(%s)", "ouch" "awk", "eee_yow");
345 Perl_croak_xs_usage(pTHX_ const CV *const cv, const char *const params)
347 const GV *const gv = CvGV(cv);
349 PERL_ARGS_ASSERT_CROAK_XS_USAGE;
352 const char *const gvname = GvNAME(gv);
353 const HV *const stash = GvSTASH(gv);
354 const char *const hvname = stash ? HvNAME_get(stash) : NULL;
357 Perl_croak(aTHX_ "Usage: %s::%s(%s)", hvname, gvname, params);
359 Perl_croak(aTHX_ "Usage: %s(%s)", gvname, params);
361 /* Pants. I don't think that it should be possible to get here. */
362 Perl_croak(aTHX_ "Usage: CODE(0x%"UVxf")(%s)", PTR2UV(cv), params);
372 croak_xs_usage(cv, "reference, kind");
374 SV * const sv = ST(0);
379 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
380 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
383 name = SvPV_nolen_const(ST(1));
385 ST(0) = boolSV(sv_derived_from(sv, name));
400 croak_xs_usage(cv, "object-ref, method");
406 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
407 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
410 name = SvPV_nolen_const(ST(1));
414 sv = MUTABLE_SV(SvRV(sv));
419 pkg = gv_stashsv(sv, 0);
423 GV * const gv = gv_fetchmethod_autoload(pkg, name, FALSE);
425 rv = sv_2mortal(newRV(MUTABLE_SV(GvCV(gv))));
432 XS(XS_UNIVERSAL_DOES)
439 Perl_croak(aTHX_ "Usage: invocant->DOES(kind)");
441 SV * const sv = ST(0);
444 name = SvPV_nolen_const(ST(1));
445 if (sv_does( sv, name ))
452 XS(XS_UNIVERSAL_VERSION)
464 sv = MUTABLE_SV(SvRV(ST(0)));
466 Perl_croak(aTHX_ "Cannot find version of an unblessed reference");
470 pkg = gv_stashsv(ST(0), 0);
473 gvp = pkg ? (GV**)hv_fetchs(pkg, "VERSION", FALSE) : NULL;
475 if (gvp && isGV(gv = *gvp) && (sv = GvSV(gv)) && SvOK(sv)) {
476 SV * const nsv = sv_newmortal();
479 if ( !sv_derived_from(sv, "version"))
480 upg_version(sv, FALSE);
493 const char * const name = HvNAME_get(pkg);
495 "%s does not define $%s::VERSION--version check failed",
499 "%s defines neither package nor VERSION--version check failed",
500 SvPVx_nolen_const(ST(0)) );
504 if ( !sv_derived_from(req, "version")) {
505 /* req may very well be R/O, so create a new object */
506 req = sv_2mortal( new_version(req) );
509 if ( vcmp( req, sv ) > 0 ) {
510 if ( hv_exists(MUTABLE_HV(SvRV(req)), "qv", 2 ) ) {
511 Perl_croak(aTHX_ "%s version %"SVf" required--"
512 "this is only version %"SVf"", HvNAME_get(pkg),
513 SVfARG(vnormal(req)),
514 SVfARG(vnormal(sv)));
516 Perl_croak(aTHX_ "%s version %"SVf" required--"
517 "this is only version %"SVf"", HvNAME_get(pkg),
518 SVfARG(vstringify(req)),
519 SVfARG(vstringify(sv)));
525 if ( SvOK(sv) && sv_derived_from(sv, "version") ) {
526 ST(0) = vstringify(sv);
539 croak_xs_usage(cv, "class, version");
544 const char * const classname =
545 sv_isobject(ST(0)) /* get the class if called as an object method */
546 ? HvNAME(SvSTASH(SvRV(ST(0))))
547 : (char *)SvPV_nolen(ST(0));
549 if ( items == 1 || ! SvOK(vs) ) { /* no param or explicit undef */
550 /* create empty object */
554 else if ( items == 3 ) {
556 Perl_sv_setpvf(aTHX_ vs,"v%s",SvPV_nolen_const(ST(2)));
559 rv = new_version(vs);
560 if ( strcmp(classname,"version") != 0 ) /* inherited new() */
561 sv_bless(rv, gv_stashpv(classname, GV_ADD));
569 XS(XS_version_stringify)
574 croak_xs_usage(cv, "lobj, ...");
579 if (sv_derived_from(ST(0), "version")) {
583 Perl_croak(aTHX_ "lobj is not of type version");
585 mPUSHs(vstringify(lobj));
592 XS(XS_version_numify)
597 croak_xs_usage(cv, "lobj, ...");
602 if (sv_derived_from(ST(0), "version")) {
606 Perl_croak(aTHX_ "lobj is not of type version");
608 mPUSHs(vnumify(lobj));
615 XS(XS_version_normal)
620 croak_xs_usage(cv, "lobj, ...");
625 if (sv_derived_from(ST(0), "version")) {
629 Perl_croak(aTHX_ "lobj is not of type version");
631 mPUSHs(vnormal(lobj));
643 croak_xs_usage(cv, "lobj, ...");
648 if (sv_derived_from(ST(0), "version")) {
652 Perl_croak(aTHX_ "lobj is not of type version");
658 const IV swap = (IV)SvIV(ST(2));
660 if ( ! sv_derived_from(robj, "version") )
662 robj = new_version(SvOK(robj) ? robj : newSVpvs("0"));
668 rs = newSViv(vcmp(rvs,lobj));
672 rs = newSViv(vcmp(lobj,rvs));
683 XS(XS_version_boolean)
688 croak_xs_usage(cv, "lobj, ...");
690 if (sv_derived_from(ST(0), "version")) {
691 SV * const lobj = SvRV(ST(0));
692 SV * const rs = newSViv( vcmp(lobj,new_version(newSVpvs("0"))) );
698 Perl_croak(aTHX_ "lobj is not of type version");
706 croak_xs_usage(cv, "lobj, ...");
707 if (sv_derived_from(ST(0), "version"))
708 Perl_croak(aTHX_ "operation not supported with version object");
710 Perl_croak(aTHX_ "lobj is not of type version");
711 #ifndef HASATTRIBUTE_NORETURN
716 XS(XS_version_is_alpha)
721 croak_xs_usage(cv, "lobj");
723 if (sv_derived_from(ST(0), "version")) {
724 SV * const lobj = ST(0);
725 if ( hv_exists(MUTABLE_HV(SvRV(lobj)), "alpha", 5 ) )
733 Perl_croak(aTHX_ "lobj is not of type version");
745 const char * classname = "";
746 if ( items == 2 && SvOK(ST(1)) ) {
747 /* getting called as object or class method */
750 sv_isobject(ST(0)) /* class called as an object method */
751 ? HvNAME_get(SvSTASH(SvRV(ST(0))))
752 : (char *)SvPV_nolen(ST(0));
754 if ( !SvVOK(ver) ) { /* not already a v-string */
756 sv_setsv(rv,ver); /* make a duplicate */
757 upg_version(rv, TRUE);
759 rv = sv_2mortal(new_version(ver));
761 if ( items == 2 && strcmp(classname,"version") ) { /* inherited new() */
762 sv_bless(rv, gv_stashpv(classname, GV_ADD));
775 croak_xs_usage(cv, "lobj");
777 if (sv_derived_from(ST(0), "version")) {
778 SV * const lobj = ST(0);
779 if ( hv_exists(MUTABLE_HV(SvRV(lobj)), "qv", 2 ) )
787 Perl_croak(aTHX_ "lobj is not of type version");
795 croak_xs_usage(cv, "sv");
797 SV * const sv = ST(0);
812 croak_xs_usage(cv, "sv");
814 SV * const sv = ST(0);
816 const char * const s = SvPV_const(sv,len);
817 if (!SvUTF8(sv) || is_utf8_string((const U8*)s,len))
830 croak_xs_usage(cv, "sv");
831 sv_utf8_encode(ST(0));
840 croak_xs_usage(cv, "sv");
842 SV * const sv = ST(0);
843 const bool RETVAL = sv_utf8_decode(sv);
844 ST(0) = boolSV(RETVAL);
855 croak_xs_usage(cv, "sv");
857 SV * const sv = ST(0);
861 RETVAL = sv_utf8_upgrade(sv);
862 XSprePUSH; PUSHi((IV)RETVAL);
867 XS(XS_utf8_downgrade)
871 if (items < 1 || items > 2)
872 croak_xs_usage(cv, "sv, failok=0");
874 SV * const sv = ST(0);
875 const bool failok = (items < 2) ? 0 : (int)SvIV(ST(1));
876 const bool RETVAL = sv_utf8_downgrade(sv, failok);
878 ST(0) = boolSV(RETVAL);
884 XS(XS_utf8_native_to_unicode)
888 const UV uv = SvUV(ST(0));
891 croak_xs_usage(cv, "sv");
893 ST(0) = sv_2mortal(newSViv(NATIVE_TO_UNI(uv)));
897 XS(XS_utf8_unicode_to_native)
901 const UV uv = SvUV(ST(0));
904 croak_xs_usage(cv, "sv");
906 ST(0) = sv_2mortal(newSViv(UNI_TO_NATIVE(uv)));
910 XS(XS_Internals_SvREADONLY) /* This is dangerous stuff. */
914 SV * const sv = SvRV(ST(0));
923 else if (items == 2) {
929 /* I hope you really know what you are doing. */
934 XSRETURN_UNDEF; /* Can't happen. */
937 XS(XS_Internals_SvREFCNT) /* This is dangerous stuff. */
941 SV * const sv = SvRV(ST(0));
945 XSRETURN_IV(SvREFCNT(sv) - 1); /* Minus the ref created for us. */
946 else if (items == 2) {
947 /* I hope you really know what you are doing. */
948 SvREFCNT(sv) = SvIV(ST(1));
949 XSRETURN_IV(SvREFCNT(sv));
951 XSRETURN_UNDEF; /* Can't happen. */
954 XS(XS_Internals_hv_clear_placehold)
960 croak_xs_usage(cv, "hv");
962 HV * const hv = MUTABLE_HV(SvRV(ST(0)));
963 hv_clear_placeholders(hv);
968 XS(XS_PerlIO_get_layers)
972 if (items < 1 || items % 2 == 0)
973 croak_xs_usage(cv, "filehandle[,args]");
980 bool details = FALSE;
984 for (svp = MARK + 2; svp <= SP; svp += 2) {
985 SV * const * const varp = svp;
986 SV * const * const valp = svp + 1;
988 const char * const key = SvPV_const(*varp, klen);
992 if (klen == 5 && memEQ(key, "input", 5)) {
993 input = SvTRUE(*valp);
998 if (klen == 6 && memEQ(key, "output", 6)) {
999 input = !SvTRUE(*valp);
1004 if (klen == 7 && memEQ(key, "details", 7)) {
1005 details = SvTRUE(*valp);
1012 "get_layers: unknown argument '%s'",
1021 gv = MUTABLE_GV(sv);
1024 if (SvROK(sv) && isGV(SvRV(sv)))
1025 gv = MUTABLE_GV(SvRV(sv));
1026 else if (SvPOKp(sv))
1027 gv = gv_fetchsv(sv, 0, SVt_PVIO);
1030 if (gv && (io = GvIO(gv))) {
1031 AV* const av = PerlIO_get_layers(aTHX_ input ?
1032 IoIFP(io) : IoOFP(io));
1034 const I32 last = av_len(av);
1037 for (i = last; i >= 0; i -= 3) {
1038 SV * const * const namsvp = av_fetch(av, i - 2, FALSE);
1039 SV * const * const argsvp = av_fetch(av, i - 1, FALSE);
1040 SV * const * const flgsvp = av_fetch(av, i, FALSE);
1042 const bool namok = namsvp && *namsvp && SvPOK(*namsvp);
1043 const bool argok = argsvp && *argsvp && SvPOK(*argsvp);
1044 const bool flgok = flgsvp && *flgsvp && SvIOK(*flgsvp);
1047 /* Indents of 5? Yuck. */
1048 /* We know that PerlIO_get_layers creates a new SV for
1049 the name and flags, so we can just take a reference
1050 and "steal" it when we free the AV below. */
1052 ? sv_2mortal(SvREFCNT_inc_simple_NN(*namsvp))
1055 ? newSVpvn_flags(SvPVX_const(*argsvp),
1057 (SvUTF8(*argsvp) ? SVf_UTF8 : 0)
1061 ? sv_2mortal(SvREFCNT_inc_simple_NN(*flgsvp))
1067 XPUSHs(sv_2mortal(Perl_newSVpvf(aTHX_ "%"SVf"(%"SVf")",
1071 XPUSHs(sv_2mortal(SvREFCNT_inc_simple_NN(*namsvp)));
1073 XPUSHs(&PL_sv_undef);
1076 const IV flags = SvIVX(*flgsvp);
1078 if (flags & PERLIO_F_UTF8) {
1079 XPUSHs(newSVpvs_flags("utf8", SVs_TEMP));
1096 XS(XS_Internals_hash_seed)
1099 /* Using dXSARGS would also have dITEM and dSP,
1100 * which define 2 unused local variables. */
1102 PERL_UNUSED_ARG(cv);
1103 PERL_UNUSED_VAR(mark);
1104 XSRETURN_UV(PERL_HASH_SEED);
1107 XS(XS_Internals_rehash_seed)
1110 /* Using dXSARGS would also have dITEM and dSP,
1111 * which define 2 unused local variables. */
1113 PERL_UNUSED_ARG(cv);
1114 PERL_UNUSED_VAR(mark);
1115 XSRETURN_UV(PL_rehash_seed);
1118 XS(XS_Internals_HvREHASH) /* Subject to change */
1122 PERL_UNUSED_ARG(cv);
1124 const HV * const hv = (const HV *) SvRV(ST(0));
1125 if (items == 1 && SvTYPE(hv) == SVt_PVHV) {
1132 Perl_croak(aTHX_ "Internals::HvREHASH $hashref");
1139 PERL_UNUSED_VAR(cv);
1142 croak_xs_usage(cv, "sv");
1146 if (SvRXOK(ST(0))) {
1153 XS(XS_re_regnames_count)
1155 REGEXP *rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1161 croak_xs_usage(cv, "");
1168 ret = CALLREG_NAMED_BUFF_COUNT(rx);
1189 if (items < 1 || items > 2)
1190 croak_xs_usage(cv, "name[, all ]");
1194 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1199 if (items == 2 && SvTRUE(ST(1))) {
1204 ret = CALLREG_NAMED_BUFF_FETCH(rx, ST(0), (flags | RXapif_REGNAME));
1227 croak_xs_usage(cv, "[all]");
1229 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1234 if (items == 1 && SvTRUE(ST(0))) {
1242 ret = CALLREG_NAMED_BUFF_ALL(rx, (flags | RXapif_REGNAMES));
1251 av = MUTABLE_AV(SvRV(ret));
1252 length = av_len(av);
1254 for (i = 0; i <= length; i++) {
1255 entry = av_fetch(av, i, FALSE);
1258 Perl_croak(aTHX_ "NULL array element in re::regnames()");
1260 mXPUSHs(SvREFCNT_inc_simple_NN(*entry));
1269 XS(XS_re_regexp_pattern)
1276 croak_xs_usage(cv, "sv");
1281 Checks if a reference is a regex or not. If the parameter is
1282 not a ref, or is not the result of a qr// then returns false
1283 in scalar context and an empty list in list context.
1284 Otherwise in list context it returns the pattern and the
1285 modifiers, in scalar context it returns the pattern just as it
1286 would if the qr// was stringified normally, regardless as
1287 to the class of the variable and any strigification overloads
1291 if ((re = SvRX(ST(0)))) /* assign deliberate */
1293 /* Housten, we have a regex! */
1298 if ( GIMME_V == G_ARRAY ) {
1300 we are in list context so stringify
1301 the modifiers that apply. We ignore "negative
1302 modifiers" in this scenario.
1305 const char *fptr = INT_PAT_MODS;
1307 U16 match_flags = (U16)((RX_EXTFLAGS(re) & PMf_COMPILETIME)
1308 >> RXf_PMf_STD_PMMOD_SHIFT);
1310 while((ch = *fptr++)) {
1311 if(match_flags & 1) {
1312 reflags[left++] = ch;
1317 pattern = newSVpvn_flags(RX_PRECOMP(re),RX_PRELEN(re),
1318 (RX_UTF8(re) ? SVf_UTF8 : 0) | SVs_TEMP);
1320 /* return the pattern and the modifiers */
1322 XPUSHs(newSVpvn_flags(reflags, left, SVs_TEMP));
1325 /* Scalar, so use the string that Perl would return */
1326 /* return the pattern in (?msix:..) format */
1327 #if PERL_VERSION >= 11
1328 pattern = sv_2mortal(newSVsv(MUTABLE_SV(re)));
1330 pattern = newSVpvn_flags(RX_WRAPPED(re), RX_WRAPLEN(re),
1331 (RX_UTF8(re) ? SVf_UTF8 : 0) | SVs_TEMP);
1337 /* It ain't a regexp folks */
1338 if ( GIMME_V == G_ARRAY ) {
1339 /* return the empty list */
1342 /* Because of the (?:..) wrapping involved in a
1343 stringified pattern it is impossible to get a
1344 result for a real regexp that would evaluate to
1345 false. Therefore we can return PL_sv_no to signify
1346 that the object is not a regex, this means that one
1349 if (regex($might_be_a_regex) eq '(?:foo)') { }
1351 and not worry about undefined values.
1359 XS(XS_Tie_Hash_NamedCapture_FETCH)
1368 croak_xs_usage(cv, "$key, $flags");
1370 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1372 if (!rx || !SvROK(ST(0)))
1377 flags = (U32)INT2PTR(IV,SvIV(SvRV(MUTABLE_SV(ST(0)))));
1378 ret = CALLREG_NAMED_BUFF_FETCH(rx, ST(1), flags);
1390 XS(XS_Tie_Hash_NamedCapture_STORE)
1398 croak_xs_usage(cv, "$key, $value, $flags");
1400 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1402 if (!rx || !SvROK(ST(0))) {
1404 Perl_croak(aTHX_ "%s", PL_no_modify);
1411 flags = (U32)INT2PTR(IV,SvIV(SvRV(MUTABLE_SV(ST(0)))));
1412 CALLREG_NAMED_BUFF_STORE(rx,ST(1), ST(2), flags);
1415 XS(XS_Tie_Hash_NamedCapture_DELETE)
1419 REGEXP * rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1423 croak_xs_usage(cv, "$key, $flags");
1425 if (!rx || !SvROK(ST(0)))
1426 Perl_croak(aTHX_ "%s", PL_no_modify);
1430 flags = (U32)INT2PTR(IV,SvIV(SvRV(MUTABLE_SV(ST(0)))));
1431 CALLREG_NAMED_BUFF_DELETE(rx, ST(1), flags);
1434 XS(XS_Tie_Hash_NamedCapture_CLEAR)
1442 croak_xs_usage(cv, "$flags");
1444 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1446 if (!rx || !SvROK(ST(0)))
1447 Perl_croak(aTHX_ "%s", PL_no_modify);
1451 flags = (U32)INT2PTR(IV,SvIV(SvRV(MUTABLE_SV(ST(0)))));
1452 CALLREG_NAMED_BUFF_CLEAR(rx, flags);
1455 XS(XS_Tie_Hash_NamedCapture_EXISTS)
1464 croak_xs_usage(cv, "$key, $flags");
1466 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1468 if (!rx || !SvROK(ST(0)))
1473 flags = (U32)INT2PTR(IV,SvIV(SvRV(MUTABLE_SV(ST(0)))));
1474 ret = CALLREG_NAMED_BUFF_EXISTS(rx, ST(1), flags);
1483 XS(XS_Tie_Hash_NamedCapture_FIRSTK)
1492 croak_xs_usage(cv, "");
1494 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1496 if (!rx || !SvROK(ST(0)))
1501 flags = (U32)INT2PTR(IV,SvIV(SvRV(MUTABLE_SV(ST(0)))));
1502 ret = CALLREG_NAMED_BUFF_FIRSTKEY(rx, flags);
1515 XS(XS_Tie_Hash_NamedCapture_NEXTK)
1524 croak_xs_usage(cv, "$lastkey");
1526 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1528 if (!rx || !SvROK(ST(0)))
1533 flags = (U32)INT2PTR(IV,SvIV(SvRV(MUTABLE_SV(ST(0)))));
1534 ret = CALLREG_NAMED_BUFF_NEXTKEY(rx, ST(1), flags);
1546 XS(XS_Tie_Hash_NamedCapture_SCALAR)
1555 croak_xs_usage(cv, "");
1557 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1559 if (!rx || !SvROK(ST(0)))
1564 flags = (U32)INT2PTR(IV,SvIV(SvRV(MUTABLE_SV(ST(0)))));
1565 ret = CALLREG_NAMED_BUFF_SCALAR(rx, flags);
1578 XS(XS_Tie_Hash_NamedCapture_flags)
1584 croak_xs_usage(cv, "");
1586 mXPUSHu(RXapif_ONE);
1587 mXPUSHu(RXapif_ALL);
1595 * c-indentation-style: bsd
1597 * indent-tabs-mode: t
1600 * ex: set ts=8 sts=4 sw=4 noet: