3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 * 2005, 2006, 2007 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 story
17 /* This file contains the code that implements the functions in Perl's
18 * UNIVERSAL package, such as UNIVERSAL->can().
20 * It is also used to store XS functions that need to be present in
21 * miniperl for a lack of a better place to put them. It might be
22 * clever to move them to seperate XS files which would then be pulled
23 * in by some to-be-written build process.
27 #define PERL_IN_UNIVERSAL_C
31 #include "perliol.h" /* For the PERLIO_F_XXX */
35 * Contributed by Graham Barr <Graham.Barr@tiuk.ti.com>
36 * The main guts of traverse_isa was actually copied from gv_fetchmeth
40 S_isa_lookup(pTHX_ HV *stash, const char * const name, const HV* const name_stash)
48 PERL_ARGS_ASSERT_ISA_LOOKUP;
50 /* A stash/class can go by many names (ie. User == main::User), so
51 we compare the stash itself just in case */
52 if (name_stash && ((const HV *)stash == name_stash))
55 hvname = HvNAME_get(stash);
57 if (strEQ(hvname, name))
60 if (strEQ(name, "UNIVERSAL"))
63 stash_linear_isa = mro_get_linear_isa(stash);
64 svp = AvARRAY(stash_linear_isa) + 1;
65 items = AvFILLp(stash_linear_isa);
67 SV* const basename_sv = *svp++;
68 HV* const basestash = gv_stashsv(basename_sv, 0);
70 /* We have no test coverage for this block, as of 2008/08. */
71 if (ckWARN(WARN_SYNTAX))
72 Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
73 "Can't locate package %"SVf" for the parents of %s",
74 SVfARG(basename_sv), hvname);
77 if(name_stash == basestash || strEQ(name, SvPVX(basename_sv)))
85 =head1 SV Manipulation Functions
87 =for apidoc sv_derived_from
89 Returns a boolean indicating whether the SV is derived from the specified class
90 I<at the C level>. To check derivation at the Perl level, call C<isa()> as a
97 Perl_sv_derived_from(pTHX_ SV *sv, const char *const name)
102 PERL_ARGS_ASSERT_SV_DERIVED_FROM;
109 type = sv_reftype(sv,0);
110 if (type && strEQ(type,name))
112 stash = SvOBJECT(sv) ? SvSTASH(sv) : NULL;
115 stash = gv_stashsv(sv, 0);
119 HV * const name_stash = gv_stashpv(name, 0);
120 return isa_lookup(stash, name, name_stash);
130 Returns a boolean indicating whether the SV performs a specific, named role.
131 The SV can be a Perl object or the name of a Perl class.
139 Perl_sv_does(pTHX_ SV *sv, const char *const name)
141 const char *classname;
146 PERL_ARGS_ASSERT_SV_DOES;
153 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
154 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
157 if (sv_isobject(sv)) {
158 classname = sv_reftype(SvRV(sv),TRUE);
160 classname = SvPV_nolen(sv);
163 if (strEQ(name,classname))
168 mXPUSHs(newSVpv(name, 0));
171 methodname = newSVpvs_flags("isa", SVs_TEMP);
172 /* ugly hack: use the SvSCREAM flag so S_method_common
173 * can figure out we're calling DOES() and not isa(),
174 * and report eventual errors correctly. --rgs */
175 SvSCREAM_on(methodname);
176 call_sv(methodname, G_SCALAR | G_METHOD);
179 does_it = SvTRUE( TOPs );
186 PERL_XS_EXPORT_C void XS_UNIVERSAL_isa(pTHX_ CV *cv);
187 PERL_XS_EXPORT_C void XS_UNIVERSAL_can(pTHX_ CV *cv);
188 PERL_XS_EXPORT_C void XS_UNIVERSAL_DOES(pTHX_ CV *cv);
189 PERL_XS_EXPORT_C void XS_UNIVERSAL_VERSION(pTHX_ CV *cv);
191 XS(XS_version_stringify);
192 XS(XS_version_numify);
193 XS(XS_version_normal);
195 XS(XS_version_boolean);
196 #ifdef HASATTRIBUTE_NORETURN
197 XS(XS_version_noop) __attribute__noreturn__;
201 XS(XS_version_is_alpha);
208 XS(XS_utf8_downgrade);
209 XS(XS_utf8_unicode_to_native);
210 XS(XS_utf8_native_to_unicode);
211 XS(XS_Internals_SvREADONLY);
212 XS(XS_Internals_SvREFCNT);
213 XS(XS_Internals_hv_clear_placehold);
214 XS(XS_PerlIO_get_layers);
215 XS(XS_Regexp_DESTROY);
216 XS(XS_Internals_hash_seed);
217 XS(XS_Internals_rehash_seed);
218 XS(XS_Internals_HvREHASH);
219 XS(XS_Internals_inc_sub_generation);
223 XS(XS_re_regnames_count);
224 XS(XS_re_regexp_pattern);
225 XS(XS_Tie_Hash_NamedCapture_FETCH);
226 XS(XS_Tie_Hash_NamedCapture_STORE);
227 XS(XS_Tie_Hash_NamedCapture_DELETE);
228 XS(XS_Tie_Hash_NamedCapture_CLEAR);
229 XS(XS_Tie_Hash_NamedCapture_EXISTS);
230 XS(XS_Tie_Hash_NamedCapture_FIRSTK);
231 XS(XS_Tie_Hash_NamedCapture_NEXTK);
232 XS(XS_Tie_Hash_NamedCapture_SCALAR);
233 XS(XS_Tie_Hash_NamedCapture_flags);
236 Perl_boot_core_UNIVERSAL(pTHX)
239 static const char file[] = __FILE__;
241 newXS("UNIVERSAL::isa", XS_UNIVERSAL_isa, file);
242 newXS("UNIVERSAL::can", XS_UNIVERSAL_can, file);
243 newXS("UNIVERSAL::DOES", XS_UNIVERSAL_DOES, file);
244 newXS("UNIVERSAL::VERSION", XS_UNIVERSAL_VERSION, file);
246 /* register the overloading (type 'A') magic */
247 PL_amagic_generation++;
248 /* Make it findable via fetchmethod */
249 newXS("version::()", XS_version_noop, file);
250 newXS("version::new", XS_version_new, file);
251 newXS("version::(\"\"", XS_version_stringify, file);
252 newXS("version::stringify", XS_version_stringify, file);
253 newXS("version::(0+", XS_version_numify, file);
254 newXS("version::numify", XS_version_numify, file);
255 newXS("version::normal", XS_version_normal, file);
256 newXS("version::(cmp", XS_version_vcmp, file);
257 newXS("version::(<=>", XS_version_vcmp, file);
258 newXS("version::vcmp", XS_version_vcmp, file);
259 newXS("version::(bool", XS_version_boolean, file);
260 newXS("version::boolean", XS_version_boolean, file);
261 newXS("version::(nomethod", XS_version_noop, file);
262 newXS("version::noop", XS_version_noop, file);
263 newXS("version::is_alpha", XS_version_is_alpha, file);
264 newXS("version::qv", XS_version_qv, file);
266 newXS("utf8::is_utf8", XS_utf8_is_utf8, file);
267 newXS("utf8::valid", XS_utf8_valid, file);
268 newXS("utf8::encode", XS_utf8_encode, file);
269 newXS("utf8::decode", XS_utf8_decode, file);
270 newXS("utf8::upgrade", XS_utf8_upgrade, file);
271 newXS("utf8::downgrade", XS_utf8_downgrade, file);
272 newXS("utf8::native_to_unicode", XS_utf8_native_to_unicode, file);
273 newXS("utf8::unicode_to_native", XS_utf8_unicode_to_native, file);
274 newXSproto("Internals::SvREADONLY",XS_Internals_SvREADONLY, file, "\\[$%@];$");
275 newXSproto("Internals::SvREFCNT",XS_Internals_SvREFCNT, file, "\\[$%@];$");
276 newXSproto("Internals::hv_clear_placeholders",
277 XS_Internals_hv_clear_placehold, file, "\\%");
278 newXSproto("PerlIO::get_layers",
279 XS_PerlIO_get_layers, file, "*;@");
280 newXS("Regexp::DESTROY", XS_Regexp_DESTROY, file);
281 newXSproto("Internals::hash_seed",XS_Internals_hash_seed, file, "");
282 newXSproto("Internals::rehash_seed",XS_Internals_rehash_seed, file, "");
283 newXSproto("Internals::HvREHASH", XS_Internals_HvREHASH, file, "\\%");
284 newXSproto("re::is_regexp", XS_re_is_regexp, file, "$");
285 newXSproto("re::regname", XS_re_regname, file, ";$$");
286 newXSproto("re::regnames", XS_re_regnames, file, ";$");
287 newXSproto("re::regnames_count", XS_re_regnames_count, file, "");
288 newXSproto("re::regexp_pattern", XS_re_regexp_pattern, file, "$");
289 newXS("Tie::Hash::NamedCapture::FETCH", XS_Tie_Hash_NamedCapture_FETCH, file);
290 newXS("Tie::Hash::NamedCapture::STORE", XS_Tie_Hash_NamedCapture_STORE, file);
291 newXS("Tie::Hash::NamedCapture::DELETE", XS_Tie_Hash_NamedCapture_DELETE, file);
292 newXS("Tie::Hash::NamedCapture::CLEAR", XS_Tie_Hash_NamedCapture_CLEAR, file);
293 newXS("Tie::Hash::NamedCapture::EXISTS", XS_Tie_Hash_NamedCapture_EXISTS, file);
294 newXS("Tie::Hash::NamedCapture::FIRSTKEY", XS_Tie_Hash_NamedCapture_FIRSTK, file);
295 newXS("Tie::Hash::NamedCapture::NEXTKEY", XS_Tie_Hash_NamedCapture_NEXTK, file);
296 newXS("Tie::Hash::NamedCapture::SCALAR", XS_Tie_Hash_NamedCapture_SCALAR, file);
297 newXS("Tie::Hash::NamedCapture::flags", XS_Tie_Hash_NamedCapture_flags, file);
301 =for apidoc croak_xs_usage
303 A specialised variant of C<croak()> for emitting the usage message for xsubs
305 croak_xs_usage(cv, "eee_yow");
307 works out the package name and subroutine name from C<cv>, and then calls
308 C<croak()>. Hence if C<cv> is C<&ouch::awk>, it would call C<croak> as:
310 Perl_croak(aTHX_ "Usage %s::%s(%s)", "ouch" "awk", "eee_yow");
316 Perl_croak_xs_usage(pTHX_ const CV *const cv, const char *const params)
318 const GV *const gv = CvGV(cv);
320 PERL_ARGS_ASSERT_CROAK_XS_USAGE;
323 const char *const gvname = GvNAME(gv);
324 const HV *const stash = GvSTASH(gv);
325 const char *const hvname = stash ? HvNAME_get(stash) : NULL;
328 Perl_croak(aTHX_ "Usage: %s::%s(%s)", hvname, gvname, params);
330 Perl_croak(aTHX_ "Usage: %s(%s)", gvname, params);
332 /* Pants. I don't think that it should be possible to get here. */
333 Perl_croak(aTHX_ "Usage: CODE(0x%"UVxf")(%s)", PTR2UV(cv), params);
343 croak_xs_usage(cv, "reference, kind");
345 SV * const sv = ST(0);
350 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
351 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
354 name = SvPV_nolen_const(ST(1));
356 ST(0) = boolSV(sv_derived_from(sv, name));
371 croak_xs_usage(cv, "object-ref, method");
377 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))
378 || (SvGMAGICAL(sv) && SvPOKp(sv) && SvCUR(sv))))
381 name = SvPV_nolen_const(ST(1));
390 pkg = gv_stashsv(sv, 0);
394 GV * const gv = gv_fetchmethod_autoload(pkg, name, FALSE);
396 rv = sv_2mortal(newRV((SV*)GvCV(gv)));
403 XS(XS_UNIVERSAL_DOES)
410 Perl_croak(aTHX_ "Usage: invocant->DOES(kind)");
412 SV * const sv = ST(0);
415 name = SvPV_nolen_const(ST(1));
416 if (sv_does( sv, name ))
423 XS(XS_UNIVERSAL_VERSION)
435 sv = (SV*)SvRV(ST(0));
437 Perl_croak(aTHX_ "Cannot find version of an unblessed reference");
441 pkg = gv_stashsv(ST(0), 0);
444 gvp = pkg ? (GV**)hv_fetchs(pkg, "VERSION", FALSE) : NULL;
446 if (gvp && isGV(gv = *gvp) && (sv = GvSV(gv)) && SvOK(sv)) {
447 SV * const nsv = sv_newmortal();
450 if ( !sv_derived_from(sv, "version"))
451 upg_version(sv, FALSE);
455 sv = (SV*)&PL_sv_undef;
464 const char * const name = HvNAME_get(pkg);
466 "%s does not define $%s::VERSION--version check failed",
470 "%s defines neither package nor VERSION--version check failed",
471 SvPVx_nolen_const(ST(0)) );
475 if ( !sv_derived_from(req, "version")) {
476 /* req may very well be R/O, so create a new object */
477 req = sv_2mortal( new_version(req) );
480 if ( vcmp( req, sv ) > 0 ) {
481 if ( hv_exists((HV*)SvRV(req), "qv", 2 ) ) {
482 Perl_croak(aTHX_ "%s version %"SVf" required--"
483 "this is only version %"SVf"", HvNAME_get(pkg),
484 SVfARG(vnormal(req)),
485 SVfARG(vnormal(sv)));
487 Perl_croak(aTHX_ "%s version %"SVf" required--"
488 "this is only version %"SVf"", HvNAME_get(pkg),
489 SVfARG(vstringify(req)),
490 SVfARG(vstringify(sv)));
496 if ( SvOK(sv) && sv_derived_from(sv, "version") ) {
497 ST(0) = vstringify(sv);
510 croak_xs_usage(cv, "class, version");
515 const char * const classname =
516 sv_isobject(ST(0)) /* get the class if called as an object method */
517 ? HvNAME(SvSTASH(SvRV(ST(0))))
518 : (char *)SvPV_nolen(ST(0));
520 if ( items == 1 || vs == &PL_sv_undef ) { /* no param or explicit undef */
521 /* create empty object */
525 else if ( items == 3 ) {
527 Perl_sv_setpvf(aTHX_ vs,"v%s",SvPV_nolen_const(ST(2)));
530 rv = new_version(vs);
531 if ( strcmp(classname,"version") != 0 ) /* inherited new() */
532 sv_bless(rv, gv_stashpv(classname, GV_ADD));
540 XS(XS_version_stringify)
545 croak_xs_usage(cv, "lobj, ...");
550 if (sv_derived_from(ST(0), "version")) {
554 Perl_croak(aTHX_ "lobj is not of type version");
556 mPUSHs(vstringify(lobj));
563 XS(XS_version_numify)
568 croak_xs_usage(cv, "lobj, ...");
573 if (sv_derived_from(ST(0), "version")) {
577 Perl_croak(aTHX_ "lobj is not of type version");
579 mPUSHs(vnumify(lobj));
586 XS(XS_version_normal)
591 croak_xs_usage(cv, "lobj, ...");
596 if (sv_derived_from(ST(0), "version")) {
600 Perl_croak(aTHX_ "lobj is not of type version");
602 mPUSHs(vnormal(lobj));
614 croak_xs_usage(cv, "lobj, ...");
619 if (sv_derived_from(ST(0), "version")) {
623 Perl_croak(aTHX_ "lobj is not of type version");
629 const IV swap = (IV)SvIV(ST(2));
631 if ( ! sv_derived_from(robj, "version") )
633 robj = new_version(robj);
639 rs = newSViv(vcmp(rvs,lobj));
643 rs = newSViv(vcmp(lobj,rvs));
654 XS(XS_version_boolean)
659 croak_xs_usage(cv, "lobj, ...");
661 if (sv_derived_from(ST(0), "version")) {
662 SV * const lobj = SvRV(ST(0));
663 SV * const rs = newSViv( vcmp(lobj,new_version(newSVpvs("0"))) );
669 Perl_croak(aTHX_ "lobj is not of type version");
677 croak_xs_usage(cv, "lobj, ...");
678 if (sv_derived_from(ST(0), "version"))
679 Perl_croak(aTHX_ "operation not supported with version object");
681 Perl_croak(aTHX_ "lobj is not of type version");
682 #ifndef HASATTRIBUTE_NORETURN
687 XS(XS_version_is_alpha)
692 croak_xs_usage(cv, "lobj");
694 if (sv_derived_from(ST(0), "version")) {
695 SV * const lobj = ST(0);
696 if ( hv_exists((HV*)SvRV(lobj), "alpha", 5 ) )
704 Perl_croak(aTHX_ "lobj is not of type version");
712 croak_xs_usage(cv, "ver");
716 if ( !SvVOK(ver) ) { /* only need to do with if not already v-string */
717 SV * const rv = sv_newmortal();
718 sv_setsv(rv,ver); /* make a duplicate */
719 upg_version(rv, TRUE);
724 mPUSHs(new_version(ver));
737 croak_xs_usage(cv, "sv");
739 const SV * const sv = ST(0);
753 croak_xs_usage(cv, "sv");
755 SV * const sv = ST(0);
757 const char * const s = SvPV_const(sv,len);
758 if (!SvUTF8(sv) || is_utf8_string((const U8*)s,len))
771 croak_xs_usage(cv, "sv");
772 sv_utf8_encode(ST(0));
781 croak_xs_usage(cv, "sv");
783 SV * const sv = ST(0);
784 const bool RETVAL = sv_utf8_decode(sv);
785 ST(0) = boolSV(RETVAL);
796 croak_xs_usage(cv, "sv");
798 SV * const sv = ST(0);
802 RETVAL = sv_utf8_upgrade(sv);
803 XSprePUSH; PUSHi((IV)RETVAL);
808 XS(XS_utf8_downgrade)
812 if (items < 1 || items > 2)
813 croak_xs_usage(cv, "sv, failok=0");
815 SV * const sv = ST(0);
816 const bool failok = (items < 2) ? 0 : (int)SvIV(ST(1));
817 const bool RETVAL = sv_utf8_downgrade(sv, failok);
819 ST(0) = boolSV(RETVAL);
825 XS(XS_utf8_native_to_unicode)
829 const UV uv = SvUV(ST(0));
832 croak_xs_usage(cv, "sv");
834 ST(0) = sv_2mortal(newSViv(NATIVE_TO_UNI(uv)));
838 XS(XS_utf8_unicode_to_native)
842 const UV uv = SvUV(ST(0));
845 croak_xs_usage(cv, "sv");
847 ST(0) = sv_2mortal(newSViv(UNI_TO_NATIVE(uv)));
851 XS(XS_Internals_SvREADONLY) /* This is dangerous stuff. */
855 SV * const sv = SvRV(ST(0));
864 else if (items == 2) {
870 /* I hope you really know what you are doing. */
875 XSRETURN_UNDEF; /* Can't happen. */
878 XS(XS_Internals_SvREFCNT) /* This is dangerous stuff. */
882 SV * const sv = SvRV(ST(0));
886 XSRETURN_IV(SvREFCNT(sv) - 1); /* Minus the ref created for us. */
887 else if (items == 2) {
888 /* I hope you really know what you are doing. */
889 SvREFCNT(sv) = SvIV(ST(1));
890 XSRETURN_IV(SvREFCNT(sv));
892 XSRETURN_UNDEF; /* Can't happen. */
895 XS(XS_Internals_hv_clear_placehold)
901 croak_xs_usage(cv, "hv");
903 HV * const hv = (HV *) SvRV(ST(0));
904 hv_clear_placeholders(hv);
909 XS(XS_Regexp_DESTROY)
915 XS(XS_PerlIO_get_layers)
919 if (items < 1 || items % 2 == 0)
920 croak_xs_usage(cv, "filehandle[,args]");
927 bool details = FALSE;
931 for (svp = MARK + 2; svp <= SP; svp += 2) {
932 SV * const * const varp = svp;
933 SV * const * const valp = svp + 1;
935 const char * const key = SvPV_const(*varp, klen);
939 if (klen == 5 && memEQ(key, "input", 5)) {
940 input = SvTRUE(*valp);
945 if (klen == 6 && memEQ(key, "output", 6)) {
946 input = !SvTRUE(*valp);
951 if (klen == 7 && memEQ(key, "details", 7)) {
952 details = SvTRUE(*valp);
959 "get_layers: unknown argument '%s'",
971 if (SvROK(sv) && isGV(SvRV(sv)))
974 gv = gv_fetchsv(sv, 0, SVt_PVIO);
977 if (gv && (io = GvIO(gv))) {
978 AV* const av = PerlIO_get_layers(aTHX_ input ?
979 IoIFP(io) : IoOFP(io));
981 const I32 last = av_len(av);
984 for (i = last; i >= 0; i -= 3) {
985 SV * const * const namsvp = av_fetch(av, i - 2, FALSE);
986 SV * const * const argsvp = av_fetch(av, i - 1, FALSE);
987 SV * const * const flgsvp = av_fetch(av, i, FALSE);
989 const bool namok = namsvp && *namsvp && SvPOK(*namsvp);
990 const bool argok = argsvp && *argsvp && SvPOK(*argsvp);
991 const bool flgok = flgsvp && *flgsvp && SvIOK(*flgsvp);
994 /* Indents of 5? Yuck. */
995 /* We know that PerlIO_get_layers creates a new SV for
996 the name and flags, so we can just take a reference
997 and "steal" it when we free the AV below. */
999 ? sv_2mortal(SvREFCNT_inc_simple_NN(*namsvp))
1002 ? newSVpvn_flags(SvPVX_const(*argsvp),
1004 (SvUTF8(*argsvp) ? SVf_UTF8 : 0)
1008 ? sv_2mortal(SvREFCNT_inc_simple_NN(*flgsvp))
1014 XPUSHs(sv_2mortal(Perl_newSVpvf(aTHX_ "%"SVf"(%"SVf")",
1018 XPUSHs(sv_2mortal(SvREFCNT_inc_simple_NN(*namsvp)));
1020 XPUSHs(&PL_sv_undef);
1023 const IV flags = SvIVX(*flgsvp);
1025 if (flags & PERLIO_F_UTF8) {
1026 XPUSHs(newSVpvs_flags("utf8", SVs_TEMP));
1043 XS(XS_Internals_hash_seed)
1046 /* Using dXSARGS would also have dITEM and dSP,
1047 * which define 2 unused local variables. */
1049 PERL_UNUSED_ARG(cv);
1050 PERL_UNUSED_VAR(mark);
1051 XSRETURN_UV(PERL_HASH_SEED);
1054 XS(XS_Internals_rehash_seed)
1057 /* Using dXSARGS would also have dITEM and dSP,
1058 * which define 2 unused local variables. */
1060 PERL_UNUSED_ARG(cv);
1061 PERL_UNUSED_VAR(mark);
1062 XSRETURN_UV(PL_rehash_seed);
1065 XS(XS_Internals_HvREHASH) /* Subject to change */
1069 PERL_UNUSED_ARG(cv);
1071 const HV * const hv = (HV *) SvRV(ST(0));
1072 if (items == 1 && SvTYPE(hv) == SVt_PVHV) {
1079 Perl_croak(aTHX_ "Internals::HvREHASH $hashref");
1086 PERL_UNUSED_VAR(cv);
1089 croak_xs_usage(cv, "sv");
1093 if (SvRXOK(ST(0))) {
1100 XS(XS_re_regnames_count)
1102 REGEXP *rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1108 croak_xs_usage(cv, "");
1115 ret = CALLREG_NAMED_BUFF_COUNT(rx);
1136 if (items < 1 || items > 2)
1137 croak_xs_usage(cv, "name[, all ]");
1141 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1146 if (items == 2 && SvTRUE(ST(1))) {
1151 ret = CALLREG_NAMED_BUFF_FETCH(rx, ST(0), (flags | RXapif_REGNAME));
1174 croak_xs_usage(cv, "[all]");
1176 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1181 if (items == 1 && SvTRUE(ST(0))) {
1189 ret = CALLREG_NAMED_BUFF_ALL(rx, (flags | RXapif_REGNAMES));
1198 av = (AV*)SvRV(ret);
1199 length = av_len(av);
1201 for (i = 0; i <= length; i++) {
1202 entry = av_fetch(av, i, FALSE);
1205 Perl_croak(aTHX_ "NULL array element in re::regnames()");
1207 mXPUSHs(SvREFCNT_inc_simple_NN(*entry));
1216 XS(XS_re_regexp_pattern)
1223 croak_xs_usage(cv, "sv");
1228 Checks if a reference is a regex or not. If the parameter is
1229 not a ref, or is not the result of a qr// then returns false
1230 in scalar context and an empty list in list context.
1231 Otherwise in list context it returns the pattern and the
1232 modifiers, in scalar context it returns the pattern just as it
1233 would if the qr// was stringified normally, regardless as
1234 to the class of the variable and any strigification overloads
1238 if ((re = SvRX(ST(0)))) /* assign deliberate */
1240 /* Housten, we have a regex! */
1245 if ( GIMME_V == G_ARRAY ) {
1247 we are in list context so stringify
1248 the modifiers that apply. We ignore "negative
1249 modifiers" in this scenario.
1252 const char *fptr = INT_PAT_MODS;
1254 U16 match_flags = (U16)((RX_EXTFLAGS(re) & PMf_COMPILETIME)
1255 >> RXf_PMf_STD_PMMOD_SHIFT);
1257 while((ch = *fptr++)) {
1258 if(match_flags & 1) {
1259 reflags[left++] = ch;
1264 pattern = newSVpvn_flags(RX_PRECOMP(re),RX_PRELEN(re),
1265 (RX_UTF8(re) ? SVf_UTF8 : 0) | SVs_TEMP);
1267 /* return the pattern and the modifiers */
1269 XPUSHs(newSVpvn_flags(reflags, left, SVs_TEMP));
1272 /* Scalar, so use the string that Perl would return */
1273 /* return the pattern in (?msix:..) format */
1274 #if PERL_VERSION >= 11
1275 pattern = sv_2mortal(newSVsv((SV*)re));
1277 pattern = newSVpvn_flags(RX_WRAPPED(re), RX_WRAPLEN(re),
1278 (RX_UTF8(re) ? SVf_UTF8 : 0) | SVs_TEMP);
1284 /* It ain't a regexp folks */
1285 if ( GIMME_V == G_ARRAY ) {
1286 /* return the empty list */
1289 /* Because of the (?:..) wrapping involved in a
1290 stringified pattern it is impossible to get a
1291 result for a real regexp that would evaluate to
1292 false. Therefore we can return PL_sv_no to signify
1293 that the object is not a regex, this means that one
1296 if (regex($might_be_a_regex) eq '(?:foo)') { }
1298 and not worry about undefined values.
1306 XS(XS_Tie_Hash_NamedCapture_FETCH)
1315 croak_xs_usage(cv, "$key, $flags");
1317 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1324 flags = (U32)INT2PTR(IV,SvIV(SvRV((SV*)ST(0))));
1325 ret = CALLREG_NAMED_BUFF_FETCH(rx, ST(1), flags);
1337 XS(XS_Tie_Hash_NamedCapture_STORE)
1345 croak_xs_usage(cv, "$key, $value, $flags");
1347 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1351 Perl_croak(aTHX_ PL_no_modify);
1358 flags = (U32)INT2PTR(IV,SvIV(SvRV((SV*)ST(0))));
1359 CALLREG_NAMED_BUFF_STORE(rx,ST(1), ST(2), flags);
1362 XS(XS_Tie_Hash_NamedCapture_DELETE)
1366 REGEXP * rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1370 croak_xs_usage(cv, "$key, $flags");
1373 Perl_croak(aTHX_ PL_no_modify);
1377 flags = (U32)INT2PTR(IV,SvIV(SvRV((SV*)ST(0))));
1378 CALLREG_NAMED_BUFF_DELETE(rx, ST(1), flags);
1381 XS(XS_Tie_Hash_NamedCapture_CLEAR)
1389 croak_xs_usage(cv, "$flags");
1391 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1394 Perl_croak(aTHX_ PL_no_modify);
1398 flags = (U32)INT2PTR(IV,SvIV(SvRV((SV*)ST(0))));
1399 CALLREG_NAMED_BUFF_CLEAR(rx, flags);
1402 XS(XS_Tie_Hash_NamedCapture_EXISTS)
1411 croak_xs_usage(cv, "$key, $flags");
1413 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1420 flags = (U32)INT2PTR(IV,SvIV(SvRV((SV*)ST(0))));
1421 ret = CALLREG_NAMED_BUFF_EXISTS(rx, ST(1), flags);
1430 XS(XS_Tie_Hash_NamedCapture_FIRSTK)
1439 croak_xs_usage(cv, "");
1441 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1448 flags = (U32)INT2PTR(IV,SvIV(SvRV((SV*)ST(0))));
1449 ret = CALLREG_NAMED_BUFF_FIRSTKEY(rx, flags);
1462 XS(XS_Tie_Hash_NamedCapture_NEXTK)
1471 croak_xs_usage(cv, "$lastkey");
1473 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1480 flags = (U32)INT2PTR(IV,SvIV(SvRV((SV*)ST(0))));
1481 ret = CALLREG_NAMED_BUFF_NEXTKEY(rx, ST(1), flags);
1493 XS(XS_Tie_Hash_NamedCapture_SCALAR)
1502 croak_xs_usage(cv, "");
1504 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1511 flags = (U32)INT2PTR(IV,SvIV(SvRV((SV*)ST(0))));
1512 ret = CALLREG_NAMED_BUFF_SCALAR(rx, flags);
1525 XS(XS_Tie_Hash_NamedCapture_flags)
1531 croak_xs_usage(cv, "");
1533 mXPUSHu(RXapif_ONE);
1534 mXPUSHu(RXapif_ALL);
1542 * c-indentation-style: bsd
1544 * indent-tabs-mode: t
1547 * ex: set ts=8 sts=4 sw=4 noet: