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");
740 const char * classname = "";
741 if ( items == 2 && (ST(1)) != &PL_sv_undef ) {
742 /* getting called as object or class method */
745 sv_isobject(ST(0)) /* class called as an object method */
746 ? HvNAME_get(SvSTASH(SvRV(ST(0))))
747 : (char *)SvPV_nolen(ST(0));
749 if ( !SvVOK(ver) ) { /* not already a v-string */
751 sv_setsv(rv,ver); /* make a duplicate */
752 upg_version(rv, TRUE);
754 rv = sv_2mortal(new_version(ver));
756 if ( items == 2 && strcmp(classname,"version") ) { /* inherited new() */
757 sv_bless(rv, gv_stashpv(classname, GV_ADD));
770 croak_xs_usage(cv, "lobj");
772 if (sv_derived_from(ST(0), "version")) {
773 SV * const lobj = ST(0);
774 if ( hv_exists(MUTABLE_HV(SvRV(lobj)), "qv", 2 ) )
782 Perl_croak(aTHX_ "lobj is not of type version");
790 croak_xs_usage(cv, "sv");
792 const SV * const sv = ST(0);
806 croak_xs_usage(cv, "sv");
808 SV * const sv = ST(0);
810 const char * const s = SvPV_const(sv,len);
811 if (!SvUTF8(sv) || is_utf8_string((const U8*)s,len))
824 croak_xs_usage(cv, "sv");
825 sv_utf8_encode(ST(0));
834 croak_xs_usage(cv, "sv");
836 SV * const sv = ST(0);
837 const bool RETVAL = sv_utf8_decode(sv);
838 ST(0) = boolSV(RETVAL);
849 croak_xs_usage(cv, "sv");
851 SV * const sv = ST(0);
855 RETVAL = sv_utf8_upgrade(sv);
856 XSprePUSH; PUSHi((IV)RETVAL);
861 XS(XS_utf8_downgrade)
865 if (items < 1 || items > 2)
866 croak_xs_usage(cv, "sv, failok=0");
868 SV * const sv = ST(0);
869 const bool failok = (items < 2) ? 0 : (int)SvIV(ST(1));
870 const bool RETVAL = sv_utf8_downgrade(sv, failok);
872 ST(0) = boolSV(RETVAL);
878 XS(XS_utf8_native_to_unicode)
882 const UV uv = SvUV(ST(0));
885 croak_xs_usage(cv, "sv");
887 ST(0) = sv_2mortal(newSViv(NATIVE_TO_UNI(uv)));
891 XS(XS_utf8_unicode_to_native)
895 const UV uv = SvUV(ST(0));
898 croak_xs_usage(cv, "sv");
900 ST(0) = sv_2mortal(newSViv(UNI_TO_NATIVE(uv)));
904 XS(XS_Internals_SvREADONLY) /* This is dangerous stuff. */
908 SV * const sv = SvRV(ST(0));
917 else if (items == 2) {
923 /* I hope you really know what you are doing. */
928 XSRETURN_UNDEF; /* Can't happen. */
931 XS(XS_Internals_SvREFCNT) /* This is dangerous stuff. */
935 SV * const sv = SvRV(ST(0));
939 XSRETURN_IV(SvREFCNT(sv) - 1); /* Minus the ref created for us. */
940 else if (items == 2) {
941 /* I hope you really know what you are doing. */
942 SvREFCNT(sv) = SvIV(ST(1));
943 XSRETURN_IV(SvREFCNT(sv));
945 XSRETURN_UNDEF; /* Can't happen. */
948 XS(XS_Internals_hv_clear_placehold)
954 croak_xs_usage(cv, "hv");
956 HV * const hv = MUTABLE_HV(SvRV(ST(0)));
957 hv_clear_placeholders(hv);
962 XS(XS_PerlIO_get_layers)
966 if (items < 1 || items % 2 == 0)
967 croak_xs_usage(cv, "filehandle[,args]");
974 bool details = FALSE;
978 for (svp = MARK + 2; svp <= SP; svp += 2) {
979 SV * const * const varp = svp;
980 SV * const * const valp = svp + 1;
982 const char * const key = SvPV_const(*varp, klen);
986 if (klen == 5 && memEQ(key, "input", 5)) {
987 input = SvTRUE(*valp);
992 if (klen == 6 && memEQ(key, "output", 6)) {
993 input = !SvTRUE(*valp);
998 if (klen == 7 && memEQ(key, "details", 7)) {
999 details = SvTRUE(*valp);
1006 "get_layers: unknown argument '%s'",
1015 gv = MUTABLE_GV(sv);
1018 if (SvROK(sv) && isGV(SvRV(sv)))
1019 gv = MUTABLE_GV(SvRV(sv));
1020 else if (SvPOKp(sv))
1021 gv = gv_fetchsv(sv, 0, SVt_PVIO);
1024 if (gv && (io = GvIO(gv))) {
1025 AV* const av = PerlIO_get_layers(aTHX_ input ?
1026 IoIFP(io) : IoOFP(io));
1028 const I32 last = av_len(av);
1031 for (i = last; i >= 0; i -= 3) {
1032 SV * const * const namsvp = av_fetch(av, i - 2, FALSE);
1033 SV * const * const argsvp = av_fetch(av, i - 1, FALSE);
1034 SV * const * const flgsvp = av_fetch(av, i, FALSE);
1036 const bool namok = namsvp && *namsvp && SvPOK(*namsvp);
1037 const bool argok = argsvp && *argsvp && SvPOK(*argsvp);
1038 const bool flgok = flgsvp && *flgsvp && SvIOK(*flgsvp);
1041 /* Indents of 5? Yuck. */
1042 /* We know that PerlIO_get_layers creates a new SV for
1043 the name and flags, so we can just take a reference
1044 and "steal" it when we free the AV below. */
1046 ? sv_2mortal(SvREFCNT_inc_simple_NN(*namsvp))
1049 ? newSVpvn_flags(SvPVX_const(*argsvp),
1051 (SvUTF8(*argsvp) ? SVf_UTF8 : 0)
1055 ? sv_2mortal(SvREFCNT_inc_simple_NN(*flgsvp))
1061 XPUSHs(sv_2mortal(Perl_newSVpvf(aTHX_ "%"SVf"(%"SVf")",
1065 XPUSHs(sv_2mortal(SvREFCNT_inc_simple_NN(*namsvp)));
1067 XPUSHs(&PL_sv_undef);
1070 const IV flags = SvIVX(*flgsvp);
1072 if (flags & PERLIO_F_UTF8) {
1073 XPUSHs(newSVpvs_flags("utf8", SVs_TEMP));
1090 XS(XS_Internals_hash_seed)
1093 /* Using dXSARGS would also have dITEM and dSP,
1094 * which define 2 unused local variables. */
1096 PERL_UNUSED_ARG(cv);
1097 PERL_UNUSED_VAR(mark);
1098 XSRETURN_UV(PERL_HASH_SEED);
1101 XS(XS_Internals_rehash_seed)
1104 /* Using dXSARGS would also have dITEM and dSP,
1105 * which define 2 unused local variables. */
1107 PERL_UNUSED_ARG(cv);
1108 PERL_UNUSED_VAR(mark);
1109 XSRETURN_UV(PL_rehash_seed);
1112 XS(XS_Internals_HvREHASH) /* Subject to change */
1116 PERL_UNUSED_ARG(cv);
1118 const HV * const hv = (const HV *) SvRV(ST(0));
1119 if (items == 1 && SvTYPE(hv) == SVt_PVHV) {
1126 Perl_croak(aTHX_ "Internals::HvREHASH $hashref");
1133 PERL_UNUSED_VAR(cv);
1136 croak_xs_usage(cv, "sv");
1140 if (SvRXOK(ST(0))) {
1147 XS(XS_re_regnames_count)
1149 REGEXP *rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1155 croak_xs_usage(cv, "");
1162 ret = CALLREG_NAMED_BUFF_COUNT(rx);
1183 if (items < 1 || items > 2)
1184 croak_xs_usage(cv, "name[, all ]");
1188 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1193 if (items == 2 && SvTRUE(ST(1))) {
1198 ret = CALLREG_NAMED_BUFF_FETCH(rx, ST(0), (flags | RXapif_REGNAME));
1221 croak_xs_usage(cv, "[all]");
1223 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1228 if (items == 1 && SvTRUE(ST(0))) {
1236 ret = CALLREG_NAMED_BUFF_ALL(rx, (flags | RXapif_REGNAMES));
1245 av = MUTABLE_AV(SvRV(ret));
1246 length = av_len(av);
1248 for (i = 0; i <= length; i++) {
1249 entry = av_fetch(av, i, FALSE);
1252 Perl_croak(aTHX_ "NULL array element in re::regnames()");
1254 mXPUSHs(SvREFCNT_inc_simple_NN(*entry));
1263 XS(XS_re_regexp_pattern)
1270 croak_xs_usage(cv, "sv");
1275 Checks if a reference is a regex or not. If the parameter is
1276 not a ref, or is not the result of a qr// then returns false
1277 in scalar context and an empty list in list context.
1278 Otherwise in list context it returns the pattern and the
1279 modifiers, in scalar context it returns the pattern just as it
1280 would if the qr// was stringified normally, regardless as
1281 to the class of the variable and any strigification overloads
1285 if ((re = SvRX(ST(0)))) /* assign deliberate */
1287 /* Housten, we have a regex! */
1292 if ( GIMME_V == G_ARRAY ) {
1294 we are in list context so stringify
1295 the modifiers that apply. We ignore "negative
1296 modifiers" in this scenario.
1299 const char *fptr = INT_PAT_MODS;
1301 U16 match_flags = (U16)((RX_EXTFLAGS(re) & PMf_COMPILETIME)
1302 >> RXf_PMf_STD_PMMOD_SHIFT);
1304 while((ch = *fptr++)) {
1305 if(match_flags & 1) {
1306 reflags[left++] = ch;
1311 pattern = newSVpvn_flags(RX_PRECOMP(re),RX_PRELEN(re),
1312 (RX_UTF8(re) ? SVf_UTF8 : 0) | SVs_TEMP);
1314 /* return the pattern and the modifiers */
1316 XPUSHs(newSVpvn_flags(reflags, left, SVs_TEMP));
1319 /* Scalar, so use the string that Perl would return */
1320 /* return the pattern in (?msix:..) format */
1321 #if PERL_VERSION >= 11
1322 pattern = sv_2mortal(newSVsv(MUTABLE_SV(re)));
1324 pattern = newSVpvn_flags(RX_WRAPPED(re), RX_WRAPLEN(re),
1325 (RX_UTF8(re) ? SVf_UTF8 : 0) | SVs_TEMP);
1331 /* It ain't a regexp folks */
1332 if ( GIMME_V == G_ARRAY ) {
1333 /* return the empty list */
1336 /* Because of the (?:..) wrapping involved in a
1337 stringified pattern it is impossible to get a
1338 result for a real regexp that would evaluate to
1339 false. Therefore we can return PL_sv_no to signify
1340 that the object is not a regex, this means that one
1343 if (regex($might_be_a_regex) eq '(?:foo)') { }
1345 and not worry about undefined values.
1353 XS(XS_Tie_Hash_NamedCapture_FETCH)
1362 croak_xs_usage(cv, "$key, $flags");
1364 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1371 flags = (U32)INT2PTR(IV,SvIV(SvRV(MUTABLE_SV(ST(0)))));
1372 ret = CALLREG_NAMED_BUFF_FETCH(rx, ST(1), flags);
1384 XS(XS_Tie_Hash_NamedCapture_STORE)
1392 croak_xs_usage(cv, "$key, $value, $flags");
1394 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1398 Perl_croak(aTHX_ "%s", PL_no_modify);
1405 flags = (U32)INT2PTR(IV,SvIV(SvRV(MUTABLE_SV(ST(0)))));
1406 CALLREG_NAMED_BUFF_STORE(rx,ST(1), ST(2), flags);
1409 XS(XS_Tie_Hash_NamedCapture_DELETE)
1413 REGEXP * rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1417 croak_xs_usage(cv, "$key, $flags");
1420 Perl_croak(aTHX_ "%s", PL_no_modify);
1424 flags = (U32)INT2PTR(IV,SvIV(SvRV(MUTABLE_SV(ST(0)))));
1425 CALLREG_NAMED_BUFF_DELETE(rx, ST(1), flags);
1428 XS(XS_Tie_Hash_NamedCapture_CLEAR)
1436 croak_xs_usage(cv, "$flags");
1438 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1441 Perl_croak(aTHX_ "%s", PL_no_modify);
1445 flags = (U32)INT2PTR(IV,SvIV(SvRV(MUTABLE_SV(ST(0)))));
1446 CALLREG_NAMED_BUFF_CLEAR(rx, flags);
1449 XS(XS_Tie_Hash_NamedCapture_EXISTS)
1458 croak_xs_usage(cv, "$key, $flags");
1460 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1467 flags = (U32)INT2PTR(IV,SvIV(SvRV(MUTABLE_SV(ST(0)))));
1468 ret = CALLREG_NAMED_BUFF_EXISTS(rx, ST(1), flags);
1477 XS(XS_Tie_Hash_NamedCapture_FIRSTK)
1486 croak_xs_usage(cv, "");
1488 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1495 flags = (U32)INT2PTR(IV,SvIV(SvRV(MUTABLE_SV(ST(0)))));
1496 ret = CALLREG_NAMED_BUFF_FIRSTKEY(rx, flags);
1509 XS(XS_Tie_Hash_NamedCapture_NEXTK)
1518 croak_xs_usage(cv, "$lastkey");
1520 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1527 flags = (U32)INT2PTR(IV,SvIV(SvRV(MUTABLE_SV(ST(0)))));
1528 ret = CALLREG_NAMED_BUFF_NEXTKEY(rx, ST(1), flags);
1540 XS(XS_Tie_Hash_NamedCapture_SCALAR)
1549 croak_xs_usage(cv, "");
1551 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1558 flags = (U32)INT2PTR(IV,SvIV(SvRV(MUTABLE_SV(ST(0)))));
1559 ret = CALLREG_NAMED_BUFF_SCALAR(rx, flags);
1572 XS(XS_Tie_Hash_NamedCapture_flags)
1578 croak_xs_usage(cv, "");
1580 mXPUSHu(RXapif_ONE);
1581 mXPUSHu(RXapif_ALL);
1589 * c-indentation-style: bsd
1591 * indent-tabs-mode: t
1594 * ex: set ts=8 sts=4 sw=4 noet: