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))))
178 if (sv_isobject(sv)) {
179 classname = sv_reftype(SvRV(sv),TRUE);
181 classname = SvPV_nolen(sv);
184 if (strEQ(name,classname))
189 mXPUSHs(newSVpv(name, 0));
192 methodname = newSVpvs_flags("isa", SVs_TEMP);
193 /* ugly hack: use the SvSCREAM flag so S_method_common
194 * can figure out we're calling DOES() and not isa(),
195 * and report eventual errors correctly. --rgs */
196 SvSCREAM_on(methodname);
197 call_sv(methodname, G_SCALAR | G_METHOD);
200 does_it = SvTRUE( TOPs );
207 PERL_XS_EXPORT_C void XS_UNIVERSAL_isa(pTHX_ CV *cv);
208 PERL_XS_EXPORT_C void XS_UNIVERSAL_can(pTHX_ CV *cv);
209 PERL_XS_EXPORT_C void XS_UNIVERSAL_DOES(pTHX_ CV *cv);
210 PERL_XS_EXPORT_C void XS_UNIVERSAL_VERSION(pTHX_ CV *cv);
212 XS(XS_version_stringify);
213 XS(XS_version_numify);
214 XS(XS_version_normal);
216 XS(XS_version_boolean);
217 #ifdef HASATTRIBUTE_NORETURN
218 XS(XS_version_noop) __attribute__noreturn__;
222 XS(XS_version_is_alpha);
224 XS(XS_version_is_qv);
230 XS(XS_utf8_downgrade);
231 XS(XS_utf8_unicode_to_native);
232 XS(XS_utf8_native_to_unicode);
233 XS(XS_Internals_SvREADONLY);
234 XS(XS_Internals_SvREFCNT);
235 XS(XS_Internals_hv_clear_placehold);
236 XS(XS_PerlIO_get_layers);
237 XS(XS_Internals_hash_seed);
238 XS(XS_Internals_rehash_seed);
239 XS(XS_Internals_HvREHASH);
243 XS(XS_re_regnames_count);
244 XS(XS_re_regexp_pattern);
245 XS(XS_Tie_Hash_NamedCapture_FETCH);
246 XS(XS_Tie_Hash_NamedCapture_STORE);
247 XS(XS_Tie_Hash_NamedCapture_DELETE);
248 XS(XS_Tie_Hash_NamedCapture_CLEAR);
249 XS(XS_Tie_Hash_NamedCapture_EXISTS);
250 XS(XS_Tie_Hash_NamedCapture_FIRSTK);
251 XS(XS_Tie_Hash_NamedCapture_NEXTK);
252 XS(XS_Tie_Hash_NamedCapture_SCALAR);
253 XS(XS_Tie_Hash_NamedCapture_flags);
256 Perl_boot_core_UNIVERSAL(pTHX)
259 static const char file[] = __FILE__;
261 newXS("UNIVERSAL::isa", XS_UNIVERSAL_isa, file);
262 newXS("UNIVERSAL::can", XS_UNIVERSAL_can, file);
263 newXS("UNIVERSAL::DOES", XS_UNIVERSAL_DOES, file);
264 newXS("UNIVERSAL::VERSION", XS_UNIVERSAL_VERSION, file);
266 /* register the overloading (type 'A') magic */
267 PL_amagic_generation++;
268 /* Make it findable via fetchmethod */
269 newXS("version::()", XS_version_noop, file);
270 newXS("version::new", XS_version_new, file);
271 newXS("version::parse", XS_version_new, file);
272 newXS("version::(\"\"", XS_version_stringify, file);
273 newXS("version::stringify", XS_version_stringify, file);
274 newXS("version::(0+", XS_version_numify, file);
275 newXS("version::numify", XS_version_numify, file);
276 newXS("version::normal", XS_version_normal, file);
277 newXS("version::(cmp", XS_version_vcmp, file);
278 newXS("version::(<=>", XS_version_vcmp, file);
279 newXS("version::vcmp", XS_version_vcmp, file);
280 newXS("version::(bool", XS_version_boolean, file);
281 newXS("version::boolean", XS_version_boolean, file);
282 newXS("version::(nomethod", XS_version_noop, file);
283 newXS("version::noop", XS_version_noop, file);
284 newXS("version::is_alpha", XS_version_is_alpha, file);
285 newXS("version::qv", XS_version_qv, file);
286 newXS("version::declare", XS_version_qv, file);
287 newXS("version::is_qv", XS_version_is_qv, file);
289 newXS("utf8::is_utf8", XS_utf8_is_utf8, file);
290 newXS("utf8::valid", XS_utf8_valid, file);
291 newXS("utf8::encode", XS_utf8_encode, file);
292 newXS("utf8::decode", XS_utf8_decode, file);
293 newXS("utf8::upgrade", XS_utf8_upgrade, file);
294 newXS("utf8::downgrade", XS_utf8_downgrade, file);
295 newXS("utf8::native_to_unicode", XS_utf8_native_to_unicode, file);
296 newXS("utf8::unicode_to_native", XS_utf8_unicode_to_native, file);
297 newXSproto("Internals::SvREADONLY",XS_Internals_SvREADONLY, file, "\\[$%@];$");
298 newXSproto("Internals::SvREFCNT",XS_Internals_SvREFCNT, file, "\\[$%@];$");
299 newXSproto("Internals::hv_clear_placeholders",
300 XS_Internals_hv_clear_placehold, file, "\\%");
301 newXSproto("PerlIO::get_layers",
302 XS_PerlIO_get_layers, file, "*;@");
303 /* Providing a Regexp::DESTROY fixes #21347. See test in t/op/ref.t */
304 CvFILE(newCONSTSUB(get_hv("Regexp::", GV_ADD), "DESTROY", NULL))
306 newXSproto("Internals::hash_seed",XS_Internals_hash_seed, file, "");
307 newXSproto("Internals::rehash_seed",XS_Internals_rehash_seed, file, "");
308 newXSproto("Internals::HvREHASH", XS_Internals_HvREHASH, file, "\\%");
309 newXSproto("re::is_regexp", XS_re_is_regexp, file, "$");
310 newXSproto("re::regname", XS_re_regname, file, ";$$");
311 newXSproto("re::regnames", XS_re_regnames, file, ";$");
312 newXSproto("re::regnames_count", XS_re_regnames_count, file, "");
313 newXSproto("re::regexp_pattern", XS_re_regexp_pattern, file, "$");
314 newXS("Tie::Hash::NamedCapture::FETCH", XS_Tie_Hash_NamedCapture_FETCH, file);
315 newXS("Tie::Hash::NamedCapture::STORE", XS_Tie_Hash_NamedCapture_STORE, file);
316 newXS("Tie::Hash::NamedCapture::DELETE", XS_Tie_Hash_NamedCapture_DELETE, file);
317 newXS("Tie::Hash::NamedCapture::CLEAR", XS_Tie_Hash_NamedCapture_CLEAR, file);
318 newXS("Tie::Hash::NamedCapture::EXISTS", XS_Tie_Hash_NamedCapture_EXISTS, file);
319 newXS("Tie::Hash::NamedCapture::FIRSTKEY", XS_Tie_Hash_NamedCapture_FIRSTK, file);
320 newXS("Tie::Hash::NamedCapture::NEXTKEY", XS_Tie_Hash_NamedCapture_NEXTK, file);
321 newXS("Tie::Hash::NamedCapture::SCALAR", XS_Tie_Hash_NamedCapture_SCALAR, file);
322 newXS("Tie::Hash::NamedCapture::flags", XS_Tie_Hash_NamedCapture_flags, file);
326 =for apidoc croak_xs_usage
328 A specialised variant of C<croak()> for emitting the usage message for xsubs
330 croak_xs_usage(cv, "eee_yow");
332 works out the package name and subroutine name from C<cv>, and then calls
333 C<croak()>. Hence if C<cv> is C<&ouch::awk>, it would call C<croak> as:
335 Perl_croak(aTHX_ "Usage %s::%s(%s)", "ouch" "awk", "eee_yow");
341 Perl_croak_xs_usage(pTHX_ const CV *const cv, const char *const params)
343 const GV *const gv = CvGV(cv);
345 PERL_ARGS_ASSERT_CROAK_XS_USAGE;
348 const char *const gvname = GvNAME(gv);
349 const HV *const stash = GvSTASH(gv);
350 const char *const hvname = stash ? HvNAME_get(stash) : NULL;
353 Perl_croak(aTHX_ "Usage: %s::%s(%s)", hvname, gvname, params);
355 Perl_croak(aTHX_ "Usage: %s(%s)", gvname, params);
357 /* Pants. I don't think that it should be possible to get here. */
358 Perl_croak(aTHX_ "Usage: CODE(0x%"UVxf")(%s)", PTR2UV(cv), params);
368 croak_xs_usage(cv, "reference, kind");
370 SV * const sv = ST(0);
375 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
376 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
379 name = SvPV_nolen_const(ST(1));
381 ST(0) = boolSV(sv_derived_from(sv, name));
396 croak_xs_usage(cv, "object-ref, method");
402 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
403 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
406 name = SvPV_nolen_const(ST(1));
410 sv = MUTABLE_SV(SvRV(sv));
415 pkg = gv_stashsv(sv, 0);
419 GV * const gv = gv_fetchmethod_autoload(pkg, name, FALSE);
421 rv = sv_2mortal(newRV(MUTABLE_SV(GvCV(gv))));
428 XS(XS_UNIVERSAL_DOES)
435 Perl_croak(aTHX_ "Usage: invocant->DOES(kind)");
437 SV * const sv = ST(0);
440 name = SvPV_nolen_const(ST(1));
441 if (sv_does( sv, name ))
448 XS(XS_UNIVERSAL_VERSION)
460 sv = MUTABLE_SV(SvRV(ST(0)));
462 Perl_croak(aTHX_ "Cannot find version of an unblessed reference");
466 pkg = gv_stashsv(ST(0), 0);
469 gvp = pkg ? (GV**)hv_fetchs(pkg, "VERSION", FALSE) : NULL;
471 if (gvp && isGV(gv = *gvp) && (sv = GvSV(gv)) && SvOK(sv)) {
472 SV * const nsv = sv_newmortal();
475 if ( !sv_derived_from(sv, "version"))
476 upg_version(sv, FALSE);
489 const char * const name = HvNAME_get(pkg);
491 "%s does not define $%s::VERSION--version check failed",
495 "%s defines neither package nor VERSION--version check failed",
496 SvPVx_nolen_const(ST(0)) );
500 if ( !sv_derived_from(req, "version")) {
501 /* req may very well be R/O, so create a new object */
502 req = sv_2mortal( new_version(req) );
505 if ( vcmp( req, sv ) > 0 ) {
506 if ( hv_exists(MUTABLE_HV(SvRV(req)), "qv", 2 ) ) {
507 Perl_croak(aTHX_ "%s version %"SVf" required--"
508 "this is only version %"SVf"", HvNAME_get(pkg),
509 SVfARG(vnormal(req)),
510 SVfARG(vnormal(sv)));
512 Perl_croak(aTHX_ "%s version %"SVf" required--"
513 "this is only version %"SVf"", HvNAME_get(pkg),
514 SVfARG(vstringify(req)),
515 SVfARG(vstringify(sv)));
521 if ( SvOK(sv) && sv_derived_from(sv, "version") ) {
522 ST(0) = vstringify(sv);
535 croak_xs_usage(cv, "class, version");
540 const char * const classname =
541 sv_isobject(ST(0)) /* get the class if called as an object method */
542 ? HvNAME(SvSTASH(SvRV(ST(0))))
543 : (char *)SvPV_nolen(ST(0));
545 if ( items == 1 || vs == &PL_sv_undef ) { /* no param or explicit undef */
546 /* create empty object */
550 else if ( items == 3 ) {
552 Perl_sv_setpvf(aTHX_ vs,"v%s",SvPV_nolen_const(ST(2)));
555 rv = new_version(vs);
556 if ( strcmp(classname,"version") != 0 ) /* inherited new() */
557 sv_bless(rv, gv_stashpv(classname, GV_ADD));
565 XS(XS_version_stringify)
570 croak_xs_usage(cv, "lobj, ...");
575 if (sv_derived_from(ST(0), "version")) {
579 Perl_croak(aTHX_ "lobj is not of type version");
581 mPUSHs(vstringify(lobj));
588 XS(XS_version_numify)
593 croak_xs_usage(cv, "lobj, ...");
598 if (sv_derived_from(ST(0), "version")) {
602 Perl_croak(aTHX_ "lobj is not of type version");
604 mPUSHs(vnumify(lobj));
611 XS(XS_version_normal)
616 croak_xs_usage(cv, "lobj, ...");
621 if (sv_derived_from(ST(0), "version")) {
625 Perl_croak(aTHX_ "lobj is not of type version");
627 mPUSHs(vnormal(lobj));
639 croak_xs_usage(cv, "lobj, ...");
644 if (sv_derived_from(ST(0), "version")) {
648 Perl_croak(aTHX_ "lobj is not of type version");
654 const IV swap = (IV)SvIV(ST(2));
656 if ( ! sv_derived_from(robj, "version") )
658 robj = new_version(robj);
664 rs = newSViv(vcmp(rvs,lobj));
668 rs = newSViv(vcmp(lobj,rvs));
679 XS(XS_version_boolean)
684 croak_xs_usage(cv, "lobj, ...");
686 if (sv_derived_from(ST(0), "version")) {
687 SV * const lobj = SvRV(ST(0));
688 SV * const rs = newSViv( vcmp(lobj,new_version(newSVpvs("0"))) );
694 Perl_croak(aTHX_ "lobj is not of type version");
702 croak_xs_usage(cv, "lobj, ...");
703 if (sv_derived_from(ST(0), "version"))
704 Perl_croak(aTHX_ "operation not supported with version object");
706 Perl_croak(aTHX_ "lobj is not of type version");
707 #ifndef HASATTRIBUTE_NORETURN
712 XS(XS_version_is_alpha)
717 croak_xs_usage(cv, "lobj");
719 if (sv_derived_from(ST(0), "version")) {
720 SV * const lobj = ST(0);
721 if ( hv_exists(MUTABLE_HV(SvRV(lobj)), "alpha", 5 ) )
729 Perl_croak(aTHX_ "lobj is not of type version");
741 const char * classname = "";
742 if ( items == 2 && (ST(1)) != &PL_sv_undef ) {
743 /* getting called as object or class method */
746 sv_isobject(ST(0)) /* class called as an object method */
747 ? HvNAME_get(SvSTASH(SvRV(ST(0))))
748 : (char *)SvPV_nolen(ST(0));
750 if ( !SvVOK(ver) ) { /* not already a v-string */
752 sv_setsv(rv,ver); /* make a duplicate */
753 upg_version(rv, TRUE);
755 rv = sv_2mortal(new_version(ver));
757 if ( items == 2 && strcmp(classname,"version") ) { /* inherited new() */
758 sv_bless(rv, gv_stashpv(classname, GV_ADD));
771 croak_xs_usage(cv, "lobj");
773 if (sv_derived_from(ST(0), "version")) {
774 SV * const lobj = ST(0);
775 if ( hv_exists(MUTABLE_HV(SvRV(lobj)), "qv", 2 ) )
783 Perl_croak(aTHX_ "lobj is not of type version");
791 croak_xs_usage(cv, "sv");
793 const SV * const sv = ST(0);
807 croak_xs_usage(cv, "sv");
809 SV * const sv = ST(0);
811 const char * const s = SvPV_const(sv,len);
812 if (!SvUTF8(sv) || is_utf8_string((const U8*)s,len))
825 croak_xs_usage(cv, "sv");
826 sv_utf8_encode(ST(0));
835 croak_xs_usage(cv, "sv");
837 SV * const sv = ST(0);
838 const bool RETVAL = sv_utf8_decode(sv);
839 ST(0) = boolSV(RETVAL);
850 croak_xs_usage(cv, "sv");
852 SV * const sv = ST(0);
856 RETVAL = sv_utf8_upgrade(sv);
857 XSprePUSH; PUSHi((IV)RETVAL);
862 XS(XS_utf8_downgrade)
866 if (items < 1 || items > 2)
867 croak_xs_usage(cv, "sv, failok=0");
869 SV * const sv = ST(0);
870 const bool failok = (items < 2) ? 0 : (int)SvIV(ST(1));
871 const bool RETVAL = sv_utf8_downgrade(sv, failok);
873 ST(0) = boolSV(RETVAL);
879 XS(XS_utf8_native_to_unicode)
883 const UV uv = SvUV(ST(0));
886 croak_xs_usage(cv, "sv");
888 ST(0) = sv_2mortal(newSViv(NATIVE_TO_UNI(uv)));
892 XS(XS_utf8_unicode_to_native)
896 const UV uv = SvUV(ST(0));
899 croak_xs_usage(cv, "sv");
901 ST(0) = sv_2mortal(newSViv(UNI_TO_NATIVE(uv)));
905 XS(XS_Internals_SvREADONLY) /* This is dangerous stuff. */
909 SV * const sv = SvRV(ST(0));
918 else if (items == 2) {
924 /* I hope you really know what you are doing. */
929 XSRETURN_UNDEF; /* Can't happen. */
932 XS(XS_Internals_SvREFCNT) /* This is dangerous stuff. */
936 SV * const sv = SvRV(ST(0));
940 XSRETURN_IV(SvREFCNT(sv) - 1); /* Minus the ref created for us. */
941 else if (items == 2) {
942 /* I hope you really know what you are doing. */
943 SvREFCNT(sv) = SvIV(ST(1));
944 XSRETURN_IV(SvREFCNT(sv));
946 XSRETURN_UNDEF; /* Can't happen. */
949 XS(XS_Internals_hv_clear_placehold)
955 croak_xs_usage(cv, "hv");
957 HV * const hv = MUTABLE_HV(SvRV(ST(0)));
958 hv_clear_placeholders(hv);
963 XS(XS_PerlIO_get_layers)
967 if (items < 1 || items % 2 == 0)
968 croak_xs_usage(cv, "filehandle[,args]");
975 bool details = FALSE;
979 for (svp = MARK + 2; svp <= SP; svp += 2) {
980 SV * const * const varp = svp;
981 SV * const * const valp = svp + 1;
983 const char * const key = SvPV_const(*varp, klen);
987 if (klen == 5 && memEQ(key, "input", 5)) {
988 input = SvTRUE(*valp);
993 if (klen == 6 && memEQ(key, "output", 6)) {
994 input = !SvTRUE(*valp);
999 if (klen == 7 && memEQ(key, "details", 7)) {
1000 details = SvTRUE(*valp);
1007 "get_layers: unknown argument '%s'",
1016 gv = MUTABLE_GV(sv);
1019 if (SvROK(sv) && isGV(SvRV(sv)))
1020 gv = MUTABLE_GV(SvRV(sv));
1021 else if (SvPOKp(sv))
1022 gv = gv_fetchsv(sv, 0, SVt_PVIO);
1025 if (gv && (io = GvIO(gv))) {
1026 AV* const av = PerlIO_get_layers(aTHX_ input ?
1027 IoIFP(io) : IoOFP(io));
1029 const I32 last = av_len(av);
1032 for (i = last; i >= 0; i -= 3) {
1033 SV * const * const namsvp = av_fetch(av, i - 2, FALSE);
1034 SV * const * const argsvp = av_fetch(av, i - 1, FALSE);
1035 SV * const * const flgsvp = av_fetch(av, i, FALSE);
1037 const bool namok = namsvp && *namsvp && SvPOK(*namsvp);
1038 const bool argok = argsvp && *argsvp && SvPOK(*argsvp);
1039 const bool flgok = flgsvp && *flgsvp && SvIOK(*flgsvp);
1042 /* Indents of 5? Yuck. */
1043 /* We know that PerlIO_get_layers creates a new SV for
1044 the name and flags, so we can just take a reference
1045 and "steal" it when we free the AV below. */
1047 ? sv_2mortal(SvREFCNT_inc_simple_NN(*namsvp))
1050 ? newSVpvn_flags(SvPVX_const(*argsvp),
1052 (SvUTF8(*argsvp) ? SVf_UTF8 : 0)
1056 ? sv_2mortal(SvREFCNT_inc_simple_NN(*flgsvp))
1062 XPUSHs(sv_2mortal(Perl_newSVpvf(aTHX_ "%"SVf"(%"SVf")",
1066 XPUSHs(sv_2mortal(SvREFCNT_inc_simple_NN(*namsvp)));
1068 XPUSHs(&PL_sv_undef);
1071 const IV flags = SvIVX(*flgsvp);
1073 if (flags & PERLIO_F_UTF8) {
1074 XPUSHs(newSVpvs_flags("utf8", SVs_TEMP));
1091 XS(XS_Internals_hash_seed)
1094 /* Using dXSARGS would also have dITEM and dSP,
1095 * which define 2 unused local variables. */
1097 PERL_UNUSED_ARG(cv);
1098 PERL_UNUSED_VAR(mark);
1099 XSRETURN_UV(PERL_HASH_SEED);
1102 XS(XS_Internals_rehash_seed)
1105 /* Using dXSARGS would also have dITEM and dSP,
1106 * which define 2 unused local variables. */
1108 PERL_UNUSED_ARG(cv);
1109 PERL_UNUSED_VAR(mark);
1110 XSRETURN_UV(PL_rehash_seed);
1113 XS(XS_Internals_HvREHASH) /* Subject to change */
1117 PERL_UNUSED_ARG(cv);
1119 const HV * const hv = (const HV *) SvRV(ST(0));
1120 if (items == 1 && SvTYPE(hv) == SVt_PVHV) {
1127 Perl_croak(aTHX_ "Internals::HvREHASH $hashref");
1134 PERL_UNUSED_VAR(cv);
1137 croak_xs_usage(cv, "sv");
1141 if (SvRXOK(ST(0))) {
1148 XS(XS_re_regnames_count)
1150 REGEXP *rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1156 croak_xs_usage(cv, "");
1163 ret = CALLREG_NAMED_BUFF_COUNT(rx);
1184 if (items < 1 || items > 2)
1185 croak_xs_usage(cv, "name[, all ]");
1189 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1194 if (items == 2 && SvTRUE(ST(1))) {
1199 ret = CALLREG_NAMED_BUFF_FETCH(rx, ST(0), (flags | RXapif_REGNAME));
1222 croak_xs_usage(cv, "[all]");
1224 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1229 if (items == 1 && SvTRUE(ST(0))) {
1237 ret = CALLREG_NAMED_BUFF_ALL(rx, (flags | RXapif_REGNAMES));
1246 av = MUTABLE_AV(SvRV(ret));
1247 length = av_len(av);
1249 for (i = 0; i <= length; i++) {
1250 entry = av_fetch(av, i, FALSE);
1253 Perl_croak(aTHX_ "NULL array element in re::regnames()");
1255 mXPUSHs(SvREFCNT_inc_simple_NN(*entry));
1264 XS(XS_re_regexp_pattern)
1271 croak_xs_usage(cv, "sv");
1276 Checks if a reference is a regex or not. If the parameter is
1277 not a ref, or is not the result of a qr// then returns false
1278 in scalar context and an empty list in list context.
1279 Otherwise in list context it returns the pattern and the
1280 modifiers, in scalar context it returns the pattern just as it
1281 would if the qr// was stringified normally, regardless as
1282 to the class of the variable and any strigification overloads
1286 if ((re = SvRX(ST(0)))) /* assign deliberate */
1288 /* Housten, we have a regex! */
1293 if ( GIMME_V == G_ARRAY ) {
1295 we are in list context so stringify
1296 the modifiers that apply. We ignore "negative
1297 modifiers" in this scenario.
1300 const char *fptr = INT_PAT_MODS;
1302 U16 match_flags = (U16)((RX_EXTFLAGS(re) & PMf_COMPILETIME)
1303 >> RXf_PMf_STD_PMMOD_SHIFT);
1305 while((ch = *fptr++)) {
1306 if(match_flags & 1) {
1307 reflags[left++] = ch;
1312 pattern = newSVpvn_flags(RX_PRECOMP(re),RX_PRELEN(re),
1313 (RX_UTF8(re) ? SVf_UTF8 : 0) | SVs_TEMP);
1315 /* return the pattern and the modifiers */
1317 XPUSHs(newSVpvn_flags(reflags, left, SVs_TEMP));
1320 /* Scalar, so use the string that Perl would return */
1321 /* return the pattern in (?msix:..) format */
1322 #if PERL_VERSION >= 11
1323 pattern = sv_2mortal(newSVsv(MUTABLE_SV(re)));
1325 pattern = newSVpvn_flags(RX_WRAPPED(re), RX_WRAPLEN(re),
1326 (RX_UTF8(re) ? SVf_UTF8 : 0) | SVs_TEMP);
1332 /* It ain't a regexp folks */
1333 if ( GIMME_V == G_ARRAY ) {
1334 /* return the empty list */
1337 /* Because of the (?:..) wrapping involved in a
1338 stringified pattern it is impossible to get a
1339 result for a real regexp that would evaluate to
1340 false. Therefore we can return PL_sv_no to signify
1341 that the object is not a regex, this means that one
1344 if (regex($might_be_a_regex) eq '(?:foo)') { }
1346 and not worry about undefined values.
1354 XS(XS_Tie_Hash_NamedCapture_FETCH)
1363 croak_xs_usage(cv, "$key, $flags");
1365 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1372 flags = (U32)INT2PTR(IV,SvIV(SvRV(MUTABLE_SV(ST(0)))));
1373 ret = CALLREG_NAMED_BUFF_FETCH(rx, ST(1), flags);
1385 XS(XS_Tie_Hash_NamedCapture_STORE)
1393 croak_xs_usage(cv, "$key, $value, $flags");
1395 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1399 Perl_croak(aTHX_ "%s", PL_no_modify);
1406 flags = (U32)INT2PTR(IV,SvIV(SvRV(MUTABLE_SV(ST(0)))));
1407 CALLREG_NAMED_BUFF_STORE(rx,ST(1), ST(2), flags);
1410 XS(XS_Tie_Hash_NamedCapture_DELETE)
1414 REGEXP * rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1418 croak_xs_usage(cv, "$key, $flags");
1421 Perl_croak(aTHX_ "%s", PL_no_modify);
1425 flags = (U32)INT2PTR(IV,SvIV(SvRV(MUTABLE_SV(ST(0)))));
1426 CALLREG_NAMED_BUFF_DELETE(rx, ST(1), flags);
1429 XS(XS_Tie_Hash_NamedCapture_CLEAR)
1437 croak_xs_usage(cv, "$flags");
1439 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1442 Perl_croak(aTHX_ "%s", PL_no_modify);
1446 flags = (U32)INT2PTR(IV,SvIV(SvRV(MUTABLE_SV(ST(0)))));
1447 CALLREG_NAMED_BUFF_CLEAR(rx, flags);
1450 XS(XS_Tie_Hash_NamedCapture_EXISTS)
1459 croak_xs_usage(cv, "$key, $flags");
1461 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1468 flags = (U32)INT2PTR(IV,SvIV(SvRV(MUTABLE_SV(ST(0)))));
1469 ret = CALLREG_NAMED_BUFF_EXISTS(rx, ST(1), flags);
1478 XS(XS_Tie_Hash_NamedCapture_FIRSTK)
1487 croak_xs_usage(cv, "");
1489 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1496 flags = (U32)INT2PTR(IV,SvIV(SvRV(MUTABLE_SV(ST(0)))));
1497 ret = CALLREG_NAMED_BUFF_FIRSTKEY(rx, flags);
1510 XS(XS_Tie_Hash_NamedCapture_NEXTK)
1519 croak_xs_usage(cv, "$lastkey");
1521 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1528 flags = (U32)INT2PTR(IV,SvIV(SvRV(MUTABLE_SV(ST(0)))));
1529 ret = CALLREG_NAMED_BUFF_NEXTKEY(rx, ST(1), flags);
1541 XS(XS_Tie_Hash_NamedCapture_SCALAR)
1550 croak_xs_usage(cv, "");
1552 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1559 flags = (U32)INT2PTR(IV,SvIV(SvRV(MUTABLE_SV(ST(0)))));
1560 ret = CALLREG_NAMED_BUFF_SCALAR(rx, flags);
1573 XS(XS_Tie_Hash_NamedCapture_flags)
1579 croak_xs_usage(cv, "");
1581 mXPUSHu(RXapif_ONE);
1582 mXPUSHu(RXapif_ALL);
1590 * c-indentation-style: bsd
1592 * indent-tabs-mode: t
1595 * ex: set ts=8 sts=4 sw=4 noet: