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 * Contributed by Graham Barr <Graham.Barr@tiuk.ti.com>
38 * The main guts of traverse_isa was actually copied from gv_fetchmeth
42 S_isa_lookup(pTHX_ HV *stash, const char * const name)
45 const struct mro_meta *const meta = HvMROMETA(stash);
46 HV *const isa = meta->isa ? meta->isa : Perl_get_isa_hash(aTHX_ stash);
47 STRLEN len = strlen(name);
50 PERL_ARGS_ASSERT_ISA_LOOKUP;
52 if (hv_common(isa, NULL, name, len, 0 /* No "UTF-8" flag possible with only
54 HV_FETCH_ISEXISTS, NULL, 0)) {
55 /* Direct name lookup worked. */
59 /* A stash/class can go by many names (ie. User == main::User), so
60 we use the name in the stash itself, which is canonical. */
61 our_stash = gv_stashpvn(name, len, 0);
64 HEK *const canon_name = HvNAME_HEK(our_stash);
66 if (hv_common(isa, NULL, HEK_KEY(canon_name), HEK_LEN(canon_name),
67 HEK_FLAGS(canon_name),
68 HV_FETCH_ISEXISTS, NULL, HEK_HASH(canon_name))) {
77 =head1 SV Manipulation Functions
79 =for apidoc sv_derived_from
81 Returns a boolean indicating whether the SV is derived from the specified class
82 I<at the C level>. To check derivation at the Perl level, call C<isa()> as a
89 Perl_sv_derived_from(pTHX_ SV *sv, const char *const name)
94 PERL_ARGS_ASSERT_SV_DERIVED_FROM;
101 type = sv_reftype(sv,0);
102 if (type && strEQ(type,name))
104 stash = SvOBJECT(sv) ? SvSTASH(sv) : NULL;
107 stash = gv_stashsv(sv, 0);
110 return stash ? isa_lookup(stash, name) : FALSE;
116 Returns a boolean indicating whether the SV performs a specific, named role.
117 The SV can be a Perl object or the name of a Perl class.
125 Perl_sv_does(pTHX_ SV *sv, const char *const name)
127 const char *classname;
132 PERL_ARGS_ASSERT_SV_DOES;
139 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
140 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
143 if (sv_isobject(sv)) {
144 classname = sv_reftype(SvRV(sv),TRUE);
146 classname = SvPV_nolen(sv);
149 if (strEQ(name,classname))
154 mXPUSHs(newSVpv(name, 0));
157 methodname = newSVpvs_flags("isa", SVs_TEMP);
158 /* ugly hack: use the SvSCREAM flag so S_method_common
159 * can figure out we're calling DOES() and not isa(),
160 * and report eventual errors correctly. --rgs */
161 SvSCREAM_on(methodname);
162 call_sv(methodname, G_SCALAR | G_METHOD);
165 does_it = SvTRUE( TOPs );
172 PERL_XS_EXPORT_C void XS_UNIVERSAL_isa(pTHX_ CV *cv);
173 PERL_XS_EXPORT_C void XS_UNIVERSAL_can(pTHX_ CV *cv);
174 PERL_XS_EXPORT_C void XS_UNIVERSAL_DOES(pTHX_ CV *cv);
175 PERL_XS_EXPORT_C void XS_UNIVERSAL_VERSION(pTHX_ CV *cv);
177 XS(XS_version_stringify);
178 XS(XS_version_numify);
179 XS(XS_version_normal);
181 XS(XS_version_boolean);
182 #ifdef HASATTRIBUTE_NORETURN
183 XS(XS_version_noop) __attribute__noreturn__;
187 XS(XS_version_is_alpha);
194 XS(XS_utf8_downgrade);
195 XS(XS_utf8_unicode_to_native);
196 XS(XS_utf8_native_to_unicode);
197 XS(XS_Internals_SvREADONLY);
198 XS(XS_Internals_SvREFCNT);
199 XS(XS_Internals_hv_clear_placehold);
200 XS(XS_PerlIO_get_layers);
201 XS(XS_Regexp_DESTROY);
202 XS(XS_Internals_hash_seed);
203 XS(XS_Internals_rehash_seed);
204 XS(XS_Internals_HvREHASH);
205 XS(XS_Internals_inc_sub_generation);
209 XS(XS_re_regnames_count);
210 XS(XS_re_regexp_pattern);
211 XS(XS_Tie_Hash_NamedCapture_FETCH);
212 XS(XS_Tie_Hash_NamedCapture_STORE);
213 XS(XS_Tie_Hash_NamedCapture_DELETE);
214 XS(XS_Tie_Hash_NamedCapture_CLEAR);
215 XS(XS_Tie_Hash_NamedCapture_EXISTS);
216 XS(XS_Tie_Hash_NamedCapture_FIRSTK);
217 XS(XS_Tie_Hash_NamedCapture_NEXTK);
218 XS(XS_Tie_Hash_NamedCapture_SCALAR);
219 XS(XS_Tie_Hash_NamedCapture_flags);
222 Perl_boot_core_UNIVERSAL(pTHX)
225 static const char file[] = __FILE__;
227 newXS("UNIVERSAL::isa", XS_UNIVERSAL_isa, file);
228 newXS("UNIVERSAL::can", XS_UNIVERSAL_can, file);
229 newXS("UNIVERSAL::DOES", XS_UNIVERSAL_DOES, file);
230 newXS("UNIVERSAL::VERSION", XS_UNIVERSAL_VERSION, file);
232 /* register the overloading (type 'A') magic */
233 PL_amagic_generation++;
234 /* Make it findable via fetchmethod */
235 newXS("version::()", XS_version_noop, file);
236 newXS("version::new", XS_version_new, file);
237 newXS("version::(\"\"", XS_version_stringify, file);
238 newXS("version::stringify", XS_version_stringify, file);
239 newXS("version::(0+", XS_version_numify, file);
240 newXS("version::numify", XS_version_numify, file);
241 newXS("version::normal", XS_version_normal, file);
242 newXS("version::(cmp", XS_version_vcmp, file);
243 newXS("version::(<=>", XS_version_vcmp, file);
244 newXS("version::vcmp", XS_version_vcmp, file);
245 newXS("version::(bool", XS_version_boolean, file);
246 newXS("version::boolean", XS_version_boolean, file);
247 newXS("version::(nomethod", XS_version_noop, file);
248 newXS("version::noop", XS_version_noop, file);
249 newXS("version::is_alpha", XS_version_is_alpha, file);
250 newXS("version::qv", XS_version_qv, file);
252 newXS("utf8::is_utf8", XS_utf8_is_utf8, file);
253 newXS("utf8::valid", XS_utf8_valid, file);
254 newXS("utf8::encode", XS_utf8_encode, file);
255 newXS("utf8::decode", XS_utf8_decode, file);
256 newXS("utf8::upgrade", XS_utf8_upgrade, file);
257 newXS("utf8::downgrade", XS_utf8_downgrade, file);
258 newXS("utf8::native_to_unicode", XS_utf8_native_to_unicode, file);
259 newXS("utf8::unicode_to_native", XS_utf8_unicode_to_native, file);
260 newXSproto("Internals::SvREADONLY",XS_Internals_SvREADONLY, file, "\\[$%@];$");
261 newXSproto("Internals::SvREFCNT",XS_Internals_SvREFCNT, file, "\\[$%@];$");
262 newXSproto("Internals::hv_clear_placeholders",
263 XS_Internals_hv_clear_placehold, file, "\\%");
264 newXSproto("PerlIO::get_layers",
265 XS_PerlIO_get_layers, file, "*;@");
266 newXS("Regexp::DESTROY", XS_Regexp_DESTROY, file);
267 newXSproto("Internals::hash_seed",XS_Internals_hash_seed, file, "");
268 newXSproto("Internals::rehash_seed",XS_Internals_rehash_seed, file, "");
269 newXSproto("Internals::HvREHASH", XS_Internals_HvREHASH, file, "\\%");
270 newXSproto("re::is_regexp", XS_re_is_regexp, file, "$");
271 newXSproto("re::regname", XS_re_regname, file, ";$$");
272 newXSproto("re::regnames", XS_re_regnames, file, ";$");
273 newXSproto("re::regnames_count", XS_re_regnames_count, file, "");
274 newXSproto("re::regexp_pattern", XS_re_regexp_pattern, file, "$");
275 newXS("Tie::Hash::NamedCapture::FETCH", XS_Tie_Hash_NamedCapture_FETCH, file);
276 newXS("Tie::Hash::NamedCapture::STORE", XS_Tie_Hash_NamedCapture_STORE, file);
277 newXS("Tie::Hash::NamedCapture::DELETE", XS_Tie_Hash_NamedCapture_DELETE, file);
278 newXS("Tie::Hash::NamedCapture::CLEAR", XS_Tie_Hash_NamedCapture_CLEAR, file);
279 newXS("Tie::Hash::NamedCapture::EXISTS", XS_Tie_Hash_NamedCapture_EXISTS, file);
280 newXS("Tie::Hash::NamedCapture::FIRSTKEY", XS_Tie_Hash_NamedCapture_FIRSTK, file);
281 newXS("Tie::Hash::NamedCapture::NEXTKEY", XS_Tie_Hash_NamedCapture_NEXTK, file);
282 newXS("Tie::Hash::NamedCapture::SCALAR", XS_Tie_Hash_NamedCapture_SCALAR, file);
283 newXS("Tie::Hash::NamedCapture::flags", XS_Tie_Hash_NamedCapture_flags, file);
287 =for apidoc croak_xs_usage
289 A specialised variant of C<croak()> for emitting the usage message for xsubs
291 croak_xs_usage(cv, "eee_yow");
293 works out the package name and subroutine name from C<cv>, and then calls
294 C<croak()>. Hence if C<cv> is C<&ouch::awk>, it would call C<croak> as:
296 Perl_croak(aTHX_ "Usage %s::%s(%s)", "ouch" "awk", "eee_yow");
302 Perl_croak_xs_usage(pTHX_ const CV *const cv, const char *const params)
304 const GV *const gv = CvGV(cv);
306 PERL_ARGS_ASSERT_CROAK_XS_USAGE;
309 const char *const gvname = GvNAME(gv);
310 const HV *const stash = GvSTASH(gv);
311 const char *const hvname = stash ? HvNAME_get(stash) : NULL;
314 Perl_croak(aTHX_ "Usage: %s::%s(%s)", hvname, gvname, params);
316 Perl_croak(aTHX_ "Usage: %s(%s)", gvname, params);
318 /* Pants. I don't think that it should be possible to get here. */
319 Perl_croak(aTHX_ "Usage: CODE(0x%"UVxf")(%s)", PTR2UV(cv), params);
329 croak_xs_usage(cv, "reference, kind");
331 SV * const sv = ST(0);
336 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
337 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
340 name = SvPV_nolen_const(ST(1));
342 ST(0) = boolSV(sv_derived_from(sv, name));
357 croak_xs_usage(cv, "object-ref, method");
363 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
364 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
367 name = SvPV_nolen_const(ST(1));
371 sv = MUTABLE_SV(SvRV(sv));
376 pkg = gv_stashsv(sv, 0);
380 GV * const gv = gv_fetchmethod_autoload(pkg, name, FALSE);
382 rv = sv_2mortal(newRV(MUTABLE_SV(GvCV(gv))));
389 XS(XS_UNIVERSAL_DOES)
396 Perl_croak(aTHX_ "Usage: invocant->DOES(kind)");
398 SV * const sv = ST(0);
401 name = SvPV_nolen_const(ST(1));
402 if (sv_does( sv, name ))
409 XS(XS_UNIVERSAL_VERSION)
421 sv = MUTABLE_SV(SvRV(ST(0)));
423 Perl_croak(aTHX_ "Cannot find version of an unblessed reference");
427 pkg = gv_stashsv(ST(0), 0);
430 gvp = pkg ? (GV**)hv_fetchs(pkg, "VERSION", FALSE) : NULL;
432 if (gvp && isGV(gv = *gvp) && (sv = GvSV(gv)) && SvOK(sv)) {
433 SV * const nsv = sv_newmortal();
436 if ( !sv_derived_from(sv, "version"))
437 upg_version(sv, FALSE);
450 const char * const name = HvNAME_get(pkg);
452 "%s does not define $%s::VERSION--version check failed",
456 "%s defines neither package nor VERSION--version check failed",
457 SvPVx_nolen_const(ST(0)) );
461 if ( !sv_derived_from(req, "version")) {
462 /* req may very well be R/O, so create a new object */
463 req = sv_2mortal( new_version(req) );
466 if ( vcmp( req, sv ) > 0 ) {
467 if ( hv_exists(MUTABLE_HV(SvRV(req)), "qv", 2 ) ) {
468 Perl_croak(aTHX_ "%s version %"SVf" required--"
469 "this is only version %"SVf"", HvNAME_get(pkg),
470 SVfARG(vnormal(req)),
471 SVfARG(vnormal(sv)));
473 Perl_croak(aTHX_ "%s version %"SVf" required--"
474 "this is only version %"SVf"", HvNAME_get(pkg),
475 SVfARG(vstringify(req)),
476 SVfARG(vstringify(sv)));
482 if ( SvOK(sv) && sv_derived_from(sv, "version") ) {
483 ST(0) = vstringify(sv);
496 croak_xs_usage(cv, "class, version");
501 const char * const classname =
502 sv_isobject(ST(0)) /* get the class if called as an object method */
503 ? HvNAME(SvSTASH(SvRV(ST(0))))
504 : (char *)SvPV_nolen(ST(0));
506 if ( items == 1 || vs == &PL_sv_undef ) { /* no param or explicit undef */
507 /* create empty object */
511 else if ( items == 3 ) {
513 Perl_sv_setpvf(aTHX_ vs,"v%s",SvPV_nolen_const(ST(2)));
516 rv = new_version(vs);
517 if ( strcmp(classname,"version") != 0 ) /* inherited new() */
518 sv_bless(rv, gv_stashpv(classname, GV_ADD));
526 XS(XS_version_stringify)
531 croak_xs_usage(cv, "lobj, ...");
536 if (sv_derived_from(ST(0), "version")) {
540 Perl_croak(aTHX_ "lobj is not of type version");
542 mPUSHs(vstringify(lobj));
549 XS(XS_version_numify)
554 croak_xs_usage(cv, "lobj, ...");
559 if (sv_derived_from(ST(0), "version")) {
563 Perl_croak(aTHX_ "lobj is not of type version");
565 mPUSHs(vnumify(lobj));
572 XS(XS_version_normal)
577 croak_xs_usage(cv, "lobj, ...");
582 if (sv_derived_from(ST(0), "version")) {
586 Perl_croak(aTHX_ "lobj is not of type version");
588 mPUSHs(vnormal(lobj));
600 croak_xs_usage(cv, "lobj, ...");
605 if (sv_derived_from(ST(0), "version")) {
609 Perl_croak(aTHX_ "lobj is not of type version");
615 const IV swap = (IV)SvIV(ST(2));
617 if ( ! sv_derived_from(robj, "version") )
619 robj = new_version(robj);
625 rs = newSViv(vcmp(rvs,lobj));
629 rs = newSViv(vcmp(lobj,rvs));
640 XS(XS_version_boolean)
645 croak_xs_usage(cv, "lobj, ...");
647 if (sv_derived_from(ST(0), "version")) {
648 SV * const lobj = SvRV(ST(0));
649 SV * const rs = newSViv( vcmp(lobj,new_version(newSVpvs("0"))) );
655 Perl_croak(aTHX_ "lobj is not of type version");
663 croak_xs_usage(cv, "lobj, ...");
664 if (sv_derived_from(ST(0), "version"))
665 Perl_croak(aTHX_ "operation not supported with version object");
667 Perl_croak(aTHX_ "lobj is not of type version");
668 #ifndef HASATTRIBUTE_NORETURN
673 XS(XS_version_is_alpha)
678 croak_xs_usage(cv, "lobj");
680 if (sv_derived_from(ST(0), "version")) {
681 SV * const lobj = ST(0);
682 if ( hv_exists(MUTABLE_HV(SvRV(lobj)), "alpha", 5 ) )
690 Perl_croak(aTHX_ "lobj is not of type version");
698 croak_xs_usage(cv, "ver");
702 if ( !SvVOK(ver) ) { /* only need to do with if not already v-string */
703 SV * const rv = sv_newmortal();
704 sv_setsv(rv,ver); /* make a duplicate */
705 upg_version(rv, TRUE);
710 mPUSHs(new_version(ver));
723 croak_xs_usage(cv, "sv");
725 const SV * const sv = ST(0);
739 croak_xs_usage(cv, "sv");
741 SV * const sv = ST(0);
743 const char * const s = SvPV_const(sv,len);
744 if (!SvUTF8(sv) || is_utf8_string((const U8*)s,len))
757 croak_xs_usage(cv, "sv");
758 sv_utf8_encode(ST(0));
767 croak_xs_usage(cv, "sv");
769 SV * const sv = ST(0);
770 const bool RETVAL = sv_utf8_decode(sv);
771 ST(0) = boolSV(RETVAL);
782 croak_xs_usage(cv, "sv");
784 SV * const sv = ST(0);
788 RETVAL = sv_utf8_upgrade(sv);
789 XSprePUSH; PUSHi((IV)RETVAL);
794 XS(XS_utf8_downgrade)
798 if (items < 1 || items > 2)
799 croak_xs_usage(cv, "sv, failok=0");
801 SV * const sv = ST(0);
802 const bool failok = (items < 2) ? 0 : (int)SvIV(ST(1));
803 const bool RETVAL = sv_utf8_downgrade(sv, failok);
805 ST(0) = boolSV(RETVAL);
811 XS(XS_utf8_native_to_unicode)
815 const UV uv = SvUV(ST(0));
818 croak_xs_usage(cv, "sv");
820 ST(0) = sv_2mortal(newSViv(NATIVE_TO_UNI(uv)));
824 XS(XS_utf8_unicode_to_native)
828 const UV uv = SvUV(ST(0));
831 croak_xs_usage(cv, "sv");
833 ST(0) = sv_2mortal(newSViv(UNI_TO_NATIVE(uv)));
837 XS(XS_Internals_SvREADONLY) /* This is dangerous stuff. */
841 SV * const sv = SvRV(ST(0));
850 else if (items == 2) {
856 /* I hope you really know what you are doing. */
861 XSRETURN_UNDEF; /* Can't happen. */
864 XS(XS_Internals_SvREFCNT) /* This is dangerous stuff. */
868 SV * const sv = SvRV(ST(0));
872 XSRETURN_IV(SvREFCNT(sv) - 1); /* Minus the ref created for us. */
873 else if (items == 2) {
874 /* I hope you really know what you are doing. */
875 SvREFCNT(sv) = SvIV(ST(1));
876 XSRETURN_IV(SvREFCNT(sv));
878 XSRETURN_UNDEF; /* Can't happen. */
881 XS(XS_Internals_hv_clear_placehold)
887 croak_xs_usage(cv, "hv");
889 HV * const hv = MUTABLE_HV(SvRV(ST(0)));
890 hv_clear_placeholders(hv);
895 XS(XS_Regexp_DESTROY)
901 XS(XS_PerlIO_get_layers)
905 if (items < 1 || items % 2 == 0)
906 croak_xs_usage(cv, "filehandle[,args]");
913 bool details = FALSE;
917 for (svp = MARK + 2; svp <= SP; svp += 2) {
918 SV * const * const varp = svp;
919 SV * const * const valp = svp + 1;
921 const char * const key = SvPV_const(*varp, klen);
925 if (klen == 5 && memEQ(key, "input", 5)) {
926 input = SvTRUE(*valp);
931 if (klen == 6 && memEQ(key, "output", 6)) {
932 input = !SvTRUE(*valp);
937 if (klen == 7 && memEQ(key, "details", 7)) {
938 details = SvTRUE(*valp);
945 "get_layers: unknown argument '%s'",
957 if (SvROK(sv) && isGV(SvRV(sv)))
958 gv = MUTABLE_GV(SvRV(sv));
960 gv = gv_fetchsv(sv, 0, SVt_PVIO);
963 if (gv && (io = GvIO(gv))) {
964 AV* const av = PerlIO_get_layers(aTHX_ input ?
965 IoIFP(io) : IoOFP(io));
967 const I32 last = av_len(av);
970 for (i = last; i >= 0; i -= 3) {
971 SV * const * const namsvp = av_fetch(av, i - 2, FALSE);
972 SV * const * const argsvp = av_fetch(av, i - 1, FALSE);
973 SV * const * const flgsvp = av_fetch(av, i, FALSE);
975 const bool namok = namsvp && *namsvp && SvPOK(*namsvp);
976 const bool argok = argsvp && *argsvp && SvPOK(*argsvp);
977 const bool flgok = flgsvp && *flgsvp && SvIOK(*flgsvp);
980 /* Indents of 5? Yuck. */
981 /* We know that PerlIO_get_layers creates a new SV for
982 the name and flags, so we can just take a reference
983 and "steal" it when we free the AV below. */
985 ? sv_2mortal(SvREFCNT_inc_simple_NN(*namsvp))
988 ? newSVpvn_flags(SvPVX_const(*argsvp),
990 (SvUTF8(*argsvp) ? SVf_UTF8 : 0)
994 ? sv_2mortal(SvREFCNT_inc_simple_NN(*flgsvp))
1000 XPUSHs(sv_2mortal(Perl_newSVpvf(aTHX_ "%"SVf"(%"SVf")",
1004 XPUSHs(sv_2mortal(SvREFCNT_inc_simple_NN(*namsvp)));
1006 XPUSHs(&PL_sv_undef);
1009 const IV flags = SvIVX(*flgsvp);
1011 if (flags & PERLIO_F_UTF8) {
1012 XPUSHs(newSVpvs_flags("utf8", SVs_TEMP));
1029 XS(XS_Internals_hash_seed)
1032 /* Using dXSARGS would also have dITEM and dSP,
1033 * which define 2 unused local variables. */
1035 PERL_UNUSED_ARG(cv);
1036 PERL_UNUSED_VAR(mark);
1037 XSRETURN_UV(PERL_HASH_SEED);
1040 XS(XS_Internals_rehash_seed)
1043 /* Using dXSARGS would also have dITEM and dSP,
1044 * which define 2 unused local variables. */
1046 PERL_UNUSED_ARG(cv);
1047 PERL_UNUSED_VAR(mark);
1048 XSRETURN_UV(PL_rehash_seed);
1051 XS(XS_Internals_HvREHASH) /* Subject to change */
1055 PERL_UNUSED_ARG(cv);
1057 const HV * const hv = (const HV *) SvRV(ST(0));
1058 if (items == 1 && SvTYPE(hv) == SVt_PVHV) {
1065 Perl_croak(aTHX_ "Internals::HvREHASH $hashref");
1072 PERL_UNUSED_VAR(cv);
1075 croak_xs_usage(cv, "sv");
1079 if (SvRXOK(ST(0))) {
1086 XS(XS_re_regnames_count)
1088 REGEXP *rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1094 croak_xs_usage(cv, "");
1101 ret = CALLREG_NAMED_BUFF_COUNT(rx);
1122 if (items < 1 || items > 2)
1123 croak_xs_usage(cv, "name[, all ]");
1127 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1132 if (items == 2 && SvTRUE(ST(1))) {
1137 ret = CALLREG_NAMED_BUFF_FETCH(rx, ST(0), (flags | RXapif_REGNAME));
1160 croak_xs_usage(cv, "[all]");
1162 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1167 if (items == 1 && SvTRUE(ST(0))) {
1175 ret = CALLREG_NAMED_BUFF_ALL(rx, (flags | RXapif_REGNAMES));
1184 av = MUTABLE_AV(SvRV(ret));
1185 length = av_len(av);
1187 for (i = 0; i <= length; i++) {
1188 entry = av_fetch(av, i, FALSE);
1191 Perl_croak(aTHX_ "NULL array element in re::regnames()");
1193 mXPUSHs(SvREFCNT_inc_simple_NN(*entry));
1202 XS(XS_re_regexp_pattern)
1209 croak_xs_usage(cv, "sv");
1214 Checks if a reference is a regex or not. If the parameter is
1215 not a ref, or is not the result of a qr// then returns false
1216 in scalar context and an empty list in list context.
1217 Otherwise in list context it returns the pattern and the
1218 modifiers, in scalar context it returns the pattern just as it
1219 would if the qr// was stringified normally, regardless as
1220 to the class of the variable and any strigification overloads
1224 if ((re = SvRX(ST(0)))) /* assign deliberate */
1226 /* Housten, we have a regex! */
1231 if ( GIMME_V == G_ARRAY ) {
1233 we are in list context so stringify
1234 the modifiers that apply. We ignore "negative
1235 modifiers" in this scenario.
1238 const char *fptr = INT_PAT_MODS;
1240 U16 match_flags = (U16)((RX_EXTFLAGS(re) & PMf_COMPILETIME)
1241 >> RXf_PMf_STD_PMMOD_SHIFT);
1243 while((ch = *fptr++)) {
1244 if(match_flags & 1) {
1245 reflags[left++] = ch;
1250 pattern = newSVpvn_flags(RX_PRECOMP(re),RX_PRELEN(re),
1251 (RX_UTF8(re) ? SVf_UTF8 : 0) | SVs_TEMP);
1253 /* return the pattern and the modifiers */
1255 XPUSHs(newSVpvn_flags(reflags, left, SVs_TEMP));
1258 /* Scalar, so use the string that Perl would return */
1259 /* return the pattern in (?msix:..) format */
1260 #if PERL_VERSION >= 11
1261 pattern = sv_2mortal(newSVsv(MUTABLE_SV(re)));
1263 pattern = newSVpvn_flags(RX_WRAPPED(re), RX_WRAPLEN(re),
1264 (RX_UTF8(re) ? SVf_UTF8 : 0) | SVs_TEMP);
1270 /* It ain't a regexp folks */
1271 if ( GIMME_V == G_ARRAY ) {
1272 /* return the empty list */
1275 /* Because of the (?:..) wrapping involved in a
1276 stringified pattern it is impossible to get a
1277 result for a real regexp that would evaluate to
1278 false. Therefore we can return PL_sv_no to signify
1279 that the object is not a regex, this means that one
1282 if (regex($might_be_a_regex) eq '(?:foo)') { }
1284 and not worry about undefined values.
1292 XS(XS_Tie_Hash_NamedCapture_FETCH)
1301 croak_xs_usage(cv, "$key, $flags");
1303 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1310 flags = (U32)INT2PTR(IV,SvIV(SvRV(MUTABLE_SV(ST(0)))));
1311 ret = CALLREG_NAMED_BUFF_FETCH(rx, ST(1), flags);
1323 XS(XS_Tie_Hash_NamedCapture_STORE)
1331 croak_xs_usage(cv, "$key, $value, $flags");
1333 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1337 Perl_croak(aTHX_ "%s", PL_no_modify);
1344 flags = (U32)INT2PTR(IV,SvIV(SvRV(MUTABLE_SV(ST(0)))));
1345 CALLREG_NAMED_BUFF_STORE(rx,ST(1), ST(2), flags);
1348 XS(XS_Tie_Hash_NamedCapture_DELETE)
1352 REGEXP * rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1356 croak_xs_usage(cv, "$key, $flags");
1359 Perl_croak(aTHX_ "%s", PL_no_modify);
1363 flags = (U32)INT2PTR(IV,SvIV(SvRV(MUTABLE_SV(ST(0)))));
1364 CALLREG_NAMED_BUFF_DELETE(rx, ST(1), flags);
1367 XS(XS_Tie_Hash_NamedCapture_CLEAR)
1375 croak_xs_usage(cv, "$flags");
1377 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1380 Perl_croak(aTHX_ "%s", PL_no_modify);
1384 flags = (U32)INT2PTR(IV,SvIV(SvRV(MUTABLE_SV(ST(0)))));
1385 CALLREG_NAMED_BUFF_CLEAR(rx, flags);
1388 XS(XS_Tie_Hash_NamedCapture_EXISTS)
1397 croak_xs_usage(cv, "$key, $flags");
1399 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1406 flags = (U32)INT2PTR(IV,SvIV(SvRV(MUTABLE_SV(ST(0)))));
1407 ret = CALLREG_NAMED_BUFF_EXISTS(rx, ST(1), flags);
1416 XS(XS_Tie_Hash_NamedCapture_FIRSTK)
1425 croak_xs_usage(cv, "");
1427 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1434 flags = (U32)INT2PTR(IV,SvIV(SvRV(MUTABLE_SV(ST(0)))));
1435 ret = CALLREG_NAMED_BUFF_FIRSTKEY(rx, flags);
1448 XS(XS_Tie_Hash_NamedCapture_NEXTK)
1457 croak_xs_usage(cv, "$lastkey");
1459 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1466 flags = (U32)INT2PTR(IV,SvIV(SvRV(MUTABLE_SV(ST(0)))));
1467 ret = CALLREG_NAMED_BUFF_NEXTKEY(rx, ST(1), flags);
1479 XS(XS_Tie_Hash_NamedCapture_SCALAR)
1488 croak_xs_usage(cv, "");
1490 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1497 flags = (U32)INT2PTR(IV,SvIV(SvRV(MUTABLE_SV(ST(0)))));
1498 ret = CALLREG_NAMED_BUFF_SCALAR(rx, flags);
1511 XS(XS_Tie_Hash_NamedCapture_flags)
1517 croak_xs_usage(cv, "");
1519 mXPUSHu(RXapif_ONE);
1520 mXPUSHu(RXapif_ALL);
1528 * c-indentation-style: bsd
1530 * indent-tabs-mode: t
1533 * ex: set ts=8 sts=4 sw=4 noet: