This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #38955] exists(): error message on wrong argument type is incorrect (5.8.7...
[perl5.git] / universal.c
index ea901da..d03596c 100644 (file)
 
 /* This file contains the code that implements the functions in Perl's
  * UNIVERSAL package, such as UNIVERSAL->can().
+ *
+ * It is also used to store XS functions that need to be present in
+ * miniperl for a lack of a better place to put them. It might be
+ * clever to move them to seperate XS files which would then be pulled
+ * in by some to-be-written build process.
  */
 
 #include "EXTERN.h"
  */
 
 STATIC bool
-S_isa_lookup(pTHX_ HV *stash, const char *name, const HV* const name_stash,
-             int len, int level)
+S_isa_lookup(pTHX_ HV *stash, const char * const name, const HV* const name_stash)
 {
     dVAR;
     AV* stash_linear_isa;
     SV** svp;
     const char *hvname;
     I32 items;
-    PERL_UNUSED_ARG(len);
-    PERL_UNUSED_ARG(level);
+
+    PERL_ARGS_ASSERT_ISA_LOOKUP;
 
     /* A stash/class can go by many names (ie. User == main::User), so 
        we compare the stash itself just in case */
@@ -61,8 +65,8 @@ S_isa_lookup(pTHX_ HV *stash, const char *name, const HV* const name_stash,
     items = AvFILLp(stash_linear_isa);
     while (items--) {
        SV* const basename_sv = *svp++;
-        HV* basestash = gv_stashsv(basename_sv, 0);
-       if (!basestash || (HvMROMETA(basestash)->fake && !HvFILL(basestash))) {
+        HV* const basestash = gv_stashsv(basename_sv, 0);
+       if (!basestash) {
            if (ckWARN(WARN_SYNTAX))
                Perl_warner(aTHX_ packWARN(WARN_SYNTAX),
                            "Can't locate package %"SVf" for the parents of %s",
@@ -89,11 +93,13 @@ normal Perl method.
 */
 
 bool
-Perl_sv_derived_from(pTHX_ SV *sv, const char *name)
+Perl_sv_derived_from(pTHX_ SV *sv, const char *const name)
 {
     dVAR;
     HV *stash;
 
+    PERL_ARGS_ASSERT_SV_DERIVED_FROM;
+
     SvGETMAGIC(sv);
 
     if (SvROK(sv)) {
@@ -110,7 +116,7 @@ Perl_sv_derived_from(pTHX_ SV *sv, const char *name)
 
     if (stash) {
        HV * const name_stash = gv_stashpv(name, 0);
-       return isa_lookup(stash, name, name_stash, strlen(name), 0);
+       return isa_lookup(stash, name, name_stash);
     }
     else
        return FALSE;
@@ -129,13 +135,15 @@ The SV can be a Perl object or the name of a Perl class.
 #include "XSUB.h"
 
 bool
-Perl_sv_does(pTHX_ SV *sv, const char *name)
+Perl_sv_does(pTHX_ SV *sv, const char *const name)
 {
     const char *classname;
     bool does_it;
     SV *methodname;
-
     dSP;
+
+    PERL_ARGS_ASSERT_SV_DOES;
+
     ENTER;
     SAVETMPS;
 
@@ -156,10 +164,10 @@ Perl_sv_does(pTHX_ SV *sv, const char *name)
 
     PUSHMARK(SP);
     XPUSHs(sv);
-    XPUSHs(sv_2mortal(newSVpv(name, 0)));
+    mXPUSHs(newSVpv(name, 0));
     PUTBACK;
 
-    methodname = sv_2mortal(newSVpv("isa", 0));
+    methodname = newSVpvs_flags("isa", SVs_TEMP);
     /* ugly hack: use the SvSCREAM flag so S_method_common
      * can figure out we're calling DOES() and not isa(),
      * and report eventual errors correctly. --rgs */
@@ -174,26 +182,6 @@ Perl_sv_does(pTHX_ SV *sv, const char *name)
     return does_it;
 }
 
-regexp *
-Perl_get_re_arg( pTHX_ SV *sv, U32 flags, MAGIC **mgp) {
-    MAGIC *mg;
-    if (sv) {
-        if (SvMAGICAL(sv))
-            mg_get(sv);
-        if (SvROK(sv) &&
-            (sv = (SV*)SvRV(sv)) &&     /* assign deliberate */
-            SvTYPE(sv) == SVt_PVMG &&
-            (mg = mg_find(sv, PERL_MAGIC_qr))) /* assign deliberate */
-        {        
-            if (mgp) *mgp = mg;
-            return (regexp *)mg->mg_obj;       
-        }
-    }    
-    if (mgp) *mgp = NULL;
-    return ((flags && PL_curpm) ? PM_GETRE(PL_curpm) : NULL);
-}
-
-
 PERL_XS_EXPORT_C void XS_UNIVERSAL_isa(pTHX_ CV *cv);
 PERL_XS_EXPORT_C void XS_UNIVERSAL_can(pTHX_ CV *cv);
 PERL_XS_EXPORT_C void XS_UNIVERSAL_DOES(pTHX_ CV *cv);
@@ -229,11 +217,19 @@ XS(XS_Internals_rehash_seed);
 XS(XS_Internals_HvREHASH);
 XS(XS_Internals_inc_sub_generation);
 XS(XS_re_is_regexp); 
-XS(XS_re_regname); 
-XS(XS_re_regnames); 
-XS(XS_re_regnames_iterinit);
-XS(XS_re_regnames_iternext);
+XS(XS_re_regname);
+XS(XS_re_regnames);
 XS(XS_re_regnames_count);
+XS(XS_re_regexp_pattern);
+XS(XS_Tie_Hash_NamedCapture_FETCH);
+XS(XS_Tie_Hash_NamedCapture_STORE);
+XS(XS_Tie_Hash_NamedCapture_DELETE);
+XS(XS_Tie_Hash_NamedCapture_CLEAR);
+XS(XS_Tie_Hash_NamedCapture_EXISTS);
+XS(XS_Tie_Hash_NamedCapture_FIRSTK);
+XS(XS_Tie_Hash_NamedCapture_NEXTK);
+XS(XS_Tie_Hash_NamedCapture_SCALAR);
+XS(XS_Tie_Hash_NamedCapture_flags);
 
 void
 Perl_boot_core_UNIVERSAL(pTHX)
@@ -287,9 +283,17 @@ Perl_boot_core_UNIVERSAL(pTHX)
     newXSproto("re::is_regexp", XS_re_is_regexp, file, "$");
     newXSproto("re::regname", XS_re_regname, file, ";$$");
     newXSproto("re::regnames", XS_re_regnames, file, ";$");
-    newXSproto("re::regnames_iterinit", XS_re_regnames_iterinit, file, "");
-    newXSproto("re::regnames_iternext", XS_re_regnames_iternext, file, ";$");
     newXSproto("re::regnames_count", XS_re_regnames_count, file, "");
+    newXSproto("re::regexp_pattern", XS_re_regexp_pattern, file, "$");
+    newXS("Tie::Hash::NamedCapture::FETCH", XS_Tie_Hash_NamedCapture_FETCH, file);
+    newXS("Tie::Hash::NamedCapture::STORE", XS_Tie_Hash_NamedCapture_STORE, file);
+    newXS("Tie::Hash::NamedCapture::DELETE", XS_Tie_Hash_NamedCapture_DELETE, file);
+    newXS("Tie::Hash::NamedCapture::CLEAR", XS_Tie_Hash_NamedCapture_CLEAR, file);
+    newXS("Tie::Hash::NamedCapture::EXISTS", XS_Tie_Hash_NamedCapture_EXISTS, file);
+    newXS("Tie::Hash::NamedCapture::FIRSTKEY", XS_Tie_Hash_NamedCapture_FIRSTK, file);
+    newXS("Tie::Hash::NamedCapture::NEXTKEY", XS_Tie_Hash_NamedCapture_NEXTK, file);
+    newXS("Tie::Hash::NamedCapture::SCALAR", XS_Tie_Hash_NamedCapture_SCALAR, file);
+    newXS("Tie::Hash::NamedCapture::flags", XS_Tie_Hash_NamedCapture_flags, file);
 }
 
 
@@ -493,7 +497,7 @@ XS(XS_version_new)
        if ( strcmp(classname,"version") != 0 ) /* inherited new() */
            sv_bless(rv, gv_stashpv(classname, GV_ADD));
 
-       PUSHs(sv_2mortal(rv));
+       mPUSHs(rv);
        PUTBACK;
        return;
     }
@@ -516,7 +520,7 @@ XS(XS_version_stringify)
          else
               Perl_croak(aTHX_ "lobj is not of type version");
 
-         PUSHs(sv_2mortal(vstringify(lobj)));
+         mPUSHs(vstringify(lobj));
 
          PUTBACK;
          return;
@@ -540,7 +544,7 @@ XS(XS_version_numify)
          else
               Perl_croak(aTHX_ "lobj is not of type version");
 
-         PUSHs(sv_2mortal(vnumify(lobj)));
+         mPUSHs(vnumify(lobj));
 
          PUTBACK;
          return;
@@ -564,7 +568,7 @@ XS(XS_version_normal)
          else
               Perl_croak(aTHX_ "lobj is not of type version");
 
-         PUSHs(sv_2mortal(vnormal(lobj)));
+         mPUSHs(vnormal(lobj));
 
          PUTBACK;
          return;
@@ -609,7 +613,7 @@ XS(XS_version_vcmp)
                    rs = newSViv(vcmp(lobj,rvs));
               }
 
-              PUSHs(sv_2mortal(rs));
+              mPUSHs(rs);
          }
 
          PUTBACK;
@@ -628,7 +632,7 @@ XS(XS_version_boolean)
     if (sv_derived_from(ST(0), "version")) {
        SV * const lobj = SvRV(ST(0));
        SV * const rs = newSViv( vcmp(lobj,new_version(newSVpvs("0"))) );
-       PUSHs(sv_2mortal(rs));
+       mPUSHs(rs);
        PUTBACK;
        return;
     }
@@ -691,7 +695,7 @@ XS(XS_version_qv)
        }
        else
        {
-           PUSHs(sv_2mortal(new_version(ver)));
+           mPUSHs(new_version(ver));
        }
 
        PUTBACK;
@@ -955,7 +959,6 @@ XS(XS_PerlIO_get_layers)
        }
 
        if (gv && (io = GvIO(gv))) {
-            dTARGET;
             AV* const av = PerlIO_get_layers(aTHX_ input ?
                                        IoIFP(io) : IoOFP(io));
             I32 i;
@@ -972,26 +975,31 @@ XS(XS_PerlIO_get_layers)
                  const bool flgok = flgsvp && *flgsvp && SvIOK(*flgsvp);
 
                  if (details) {
+                     /* Indents of 5? Yuck.  */
+                     /* We know that PerlIO_get_layers creates a new SV for
+                        the name and flags, so we can just take a reference
+                        and "steal" it when we free the AV below.  */
                       XPUSHs(namok
-                             ? newSVpvn(SvPVX_const(*namsvp), SvCUR(*namsvp))
+                             ? sv_2mortal(SvREFCNT_inc_simple_NN(*namsvp))
                              : &PL_sv_undef);
                       XPUSHs(argok
-                             ? newSVpvn(SvPVX_const(*argsvp), SvCUR(*argsvp))
+                             ? newSVpvn_flags(SvPVX_const(*argsvp),
+                                              SvCUR(*argsvp),
+                                              (SvUTF8(*argsvp) ? SVf_UTF8 : 0)
+                                              | SVs_TEMP)
+                             : &PL_sv_undef);
+                      XPUSHs(namok
+                             ? sv_2mortal(SvREFCNT_inc_simple_NN(*flgsvp))
                              : &PL_sv_undef);
-                      if (flgok)
-                           XPUSHi(SvIVX(*flgsvp));
-                      else
-                           XPUSHs(&PL_sv_undef);
                       nitem += 3;
                  }
                  else {
                       if (namok && argok)
-                           XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf"(%"SVf")",
+                           XPUSHs(sv_2mortal(Perl_newSVpvf(aTHX_ "%"SVf"(%"SVf")",
                                                 SVfARG(*namsvp),
-                                                SVfARG(*argsvp)));
+                                                SVfARG(*argsvp))));
                       else if (namok)
-                           XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf,
-                                                SVfARG(*namsvp)));
+                          XPUSHs(sv_2mortal(SvREFCNT_inc_simple_NN(*namsvp)));
                       else
                            XPUSHs(&PL_sv_undef);
                       nitem++;
@@ -999,7 +1007,7 @@ XS(XS_PerlIO_get_layers)
                            const IV flags = SvIVX(*flgsvp);
 
                            if (flags & PERLIO_F_UTF8) {
-                                XPUSHs(newSVpvs("utf8"));
+                                XPUSHs(newSVpvs_flags("utf8", SVs_TEMP));
                                 nitem++;
                            }
                       }
@@ -1059,222 +1067,473 @@ XS(XS_re_is_regexp)
 {
     dVAR; 
     dXSARGS;
+    PERL_UNUSED_VAR(cv);
+
     if (items != 1)
        Perl_croak(aTHX_ "Usage: %s(%s)", "re::is_regexp", "sv");
-    PERL_UNUSED_VAR(cv); /* -W */
-    PERL_UNUSED_VAR(ax); /* -Wall */
+
     SP -= items;
-    {
-       SV *    sv = ST(0);
-        if ( Perl_get_re_arg( aTHX_ sv, 0, NULL ) ) 
-        {
-            XSRETURN_YES;
-        } else {
-            XSRETURN_NO;
-        }
-        /* NOTREACHED */        
-       PUTBACK;
-       return;
+
+    if (SvRXOK(ST(0))) {
+        XSRETURN_YES;
+    } else {
+        XSRETURN_NO;
     }
 }
 
-XS(XS_re_regname)
+XS(XS_re_regnames_count)
 {
-
+    REGEXP *rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
+    SV * ret;
     dVAR; 
     dXSARGS;
+    PERL_UNUSED_ARG(cv);
+
+    if (items != 0)
+       Perl_croak(aTHX_ "Usage: %s(%s)", "re::regnames_count", "");
+
+    SP -= items;
+
+    if (!rx)
+        XSRETURN_UNDEF;
+
+    ret = CALLREG_NAMED_BUFF_COUNT(rx);
+
+    SPAGAIN;
+
+    if (ret) {
+        XPUSHs(ret);
+        PUTBACK;
+        return;
+    } else {
+        XSRETURN_UNDEF;
+    }
+}
+
+XS(XS_re_regname)
+{
+    dVAR;
+    dXSARGS;
+    REGEXP * rx;
+    U32 flags;
+    SV * ret;
+    PERL_UNUSED_ARG(cv);
+
     if (items < 1 || items > 2)
-       Perl_croak(aTHX_ "Usage: %s(%s)", "re::regname", "name[, all ]");
-    PERL_UNUSED_VAR(cv); /* -W */
-    PERL_UNUSED_VAR(ax); /* -Wall */
+        Perl_croak(aTHX_ "Usage: %s(%s)", "re::regname", "name[, all ]");
+
     SP -= items;
-    {
-       SV *    sv = ST(0);
-       SV *    all;
-        regexp *re = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
-        SV *bufs = NULL;
 
-       if (items < 2)
-           all = NULL;
-       else {
-           all = ST(1);
-       }
-        {
-            if (SvPOK(sv) && re && re->paren_names) {
-                bufs = CALLREG_NAMEDBUF(re,sv,all && SvTRUE(all));
-                if (bufs) {
-                    if (all && SvTRUE(all))
-                        XPUSHs(newRV(bufs));
-                    else
-                        XPUSHs(SvREFCNT_inc(bufs));
-                    XSRETURN(1);
-                }
-            }
-            XSRETURN_UNDEF;
-        }
-       PUTBACK;
-       return;
+    rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
+
+    if (!rx)
+        XSRETURN_UNDEF;
+
+    if (items == 2 && SvTRUE(ST(1))) {
+        flags = RXapif_ALL;
+    } else {
+        flags = RXapif_ONE;
+    }
+    ret = CALLREG_NAMED_BUFF_FETCH(rx, ST(0), (flags | RXapif_REGNAME));
+
+    if (ret) {
+        if (SvROK(ret))
+            XPUSHs(ret);
+        else
+            XPUSHs(SvREFCNT_inc(ret));
+        XSRETURN(1);
     }
+    XSRETURN_UNDEF;    
 }
 
+
 XS(XS_re_regnames)
 {
-    dVAR; 
+    dVAR;
     dXSARGS;
-    if (items < 0 || items > 1)
-       Perl_croak(aTHX_ "Usage: %s(%s)", "re::regnames", "[all]");
-    PERL_UNUSED_VAR(cv); /* -W */
-    PERL_UNUSED_VAR(ax); /* -Wall */
+    REGEXP * rx;
+    U32 flags;
+    SV *ret;
+    AV *av;
+    I32 length;
+    I32 i;
+    SV **entry;
+    PERL_UNUSED_ARG(cv);
+
+    if (items > 1)
+        Perl_croak(aTHX_ "Usage: %s(%s)", "re::regnames", "[all]");
+
+    rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
+
+    if (!rx)
+        XSRETURN_UNDEF;
+
+    if (items == 1 && SvTRUE(ST(0))) {
+        flags = RXapif_ALL;
+    } else {
+        flags = RXapif_ONE;
+    }
+
     SP -= items;
-    {
-       SV *    all;
-        regexp *re = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
-        IV count = 0;
 
-       if (items < 1)
-           all = NULL;
-       else {
-           all = ST(0);
-       }
-        {
-            if (re && re->paren_names) {
-                HV *hv= re->paren_names;
-                (void)hv_iterinit(hv);
-                while (1) {
-                    HE *temphe = hv_iternext_flags(hv,0);
-                    if (temphe) {
-                        IV i;
-                        IV parno = 0;
-                        SV* sv_dat = HeVAL(temphe);
-                        I32 *nums = (I32*)SvPVX(sv_dat);
-                        for ( i = 0; i < SvIVX(sv_dat); i++ ) {
-                            if ((I32)(re->lastcloseparen) >= nums[i] &&
-                                re->offs[nums[i]].start != -1 &&
-                                re->offs[nums[i]].end != -1)
-                            {
-                                parno = nums[i];
-                                break;
-                            }
-                        }
-                        if (parno || (all && SvTRUE(all))) {
-                            STRLEN len;
-                            char *pv = HePV(temphe, len);
-                            if ( GIMME_V == G_ARRAY ) 
-                                XPUSHs(newSVpvn(pv,len));
-                            count++;
-                        }
-                    } else {
-                        break;
-                    }
-                }
-            }
-            if ( GIMME_V == G_ARRAY ) 
-                XSRETURN(count);
-            else 
-                XSRETURN_UNDEF;
-        }    
-       PUTBACK;
-       return;
+    ret = CALLREG_NAMED_BUFF_ALL(rx, (flags | RXapif_REGNAMES));
+
+    SPAGAIN;
+
+    SP -= items;
+
+    if (!ret)
+        XSRETURN_UNDEF;
+
+    av = (AV*)SvRV(ret);
+    length = av_len(av);
+
+    for (i = 0; i <= length; i++) {
+        entry = av_fetch(av, i, FALSE);
+        
+        if (!entry)
+            Perl_croak(aTHX_ "NULL array element in re::regnames()");
+
+        XPUSHs(*entry);
     }
+    PUTBACK;
+    return;
 }
 
-
-XS(XS_re_regnames_iterinit)
+XS(XS_re_regexp_pattern)
 {
-    dVAR; 
+    dVAR;
     dXSARGS;
-    if (items != 0)
-       Perl_croak(aTHX_ "Usage: re::regnames_iterinit()");
-    PERL_UNUSED_VAR(cv); /* -W */
-    PERL_UNUSED_VAR(ax); /* -Wall */
+    REGEXP *re;
+    PERL_UNUSED_ARG(cv);
+
+    if (items != 1)
+       Perl_croak(aTHX_ "Usage: %s(%s)", "re::regexp_pattern", "sv");
+
     SP -= items;
+
+    /*
+       Checks if a reference is a regex or not. If the parameter is
+       not a ref, or is not the result of a qr// then returns false
+       in scalar context and an empty list in list context.
+       Otherwise in list context it returns the pattern and the
+       modifiers, in scalar context it returns the pattern just as it
+       would if the qr// was stringified normally, regardless as
+       to the class of the variable and any strigification overloads
+       on the object.
+    */
+
+    if ((re = SvRX(ST(0)))) /* assign deliberate */
     {
-        regexp *re = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
-        if (re && re->paren_names) {
-            (void)hv_iterinit(re->paren_names);
-            XPUSHs(newSViv(HvTOTALKEYS(re->paren_names)));
+        /* Housten, we have a regex! */
+        SV *pattern;
+        STRLEN left = 0;
+        char reflags[6];
+
+        if ( GIMME_V == G_ARRAY ) {
+            /*
+               we are in list context so stringify
+               the modifiers that apply. We ignore "negative
+               modifiers" in this scenario.
+            */
+
+            const char *fptr = INT_PAT_MODS;
+            char ch;
+            U16 match_flags = (U16)((RX_EXTFLAGS(re) & PMf_COMPILETIME)
+                                    >> RXf_PMf_STD_PMMOD_SHIFT);
+
+            while((ch = *fptr++)) {
+                if(match_flags & 1) {
+                    reflags[left++] = ch;
+                }
+                match_flags >>= 1;
+            }
+
+            pattern = newSVpvn_flags(RX_PRECOMP(re),RX_PRELEN(re),
+                                    (RX_UTF8(re) ? SVf_UTF8 : 0) | SVs_TEMP);
+
+            /* return the pattern and the modifiers */
+            XPUSHs(pattern);
+            XPUSHs(newSVpvn_flags(reflags, left, SVs_TEMP));
+            XSRETURN(2);
         } else {
+            /* Scalar, so use the string that Perl would return */
+            /* return the pattern in (?msix:..) format */
+#if PERL_VERSION >= 11
+            pattern = sv_2mortal(newSVsv((SV*)re));
+#else
+            pattern = newSVpvn_flags(RX_WRAPPED(re), RX_WRAPLEN(re),
+                                    (RX_UTF8(re) ? SVf_UTF8 : 0) | SVs_TEMP);
+#endif
+            XPUSHs(pattern);
+            XSRETURN(1);
+        }
+    } else {
+        /* It ain't a regexp folks */
+        if ( GIMME_V == G_ARRAY ) {
+            /* return the empty list */
             XSRETURN_UNDEF;
-        }  
-       PUTBACK;
-       return;
+        } else {
+            /* Because of the (?:..) wrapping involved in a
+               stringified pattern it is impossible to get a
+               result for a real regexp that would evaluate to
+               false. Therefore we can return PL_sv_no to signify
+               that the object is not a regex, this means that one
+               can say
+
+                 if (regex($might_be_a_regex) eq '(?:foo)') { }
+
+               and not worry about undefined values.
+            */
+            XSRETURN_NO;
+        }
+    }
+    /* NOT-REACHED */
+}
+
+XS(XS_Tie_Hash_NamedCapture_FETCH)
+{
+    dVAR;
+    dXSARGS;
+    REGEXP * rx;
+    U32 flags;
+    SV * ret;
+    PERL_UNUSED_ARG(cv);
+
+    if (items != 2)
+        Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::STORE($key, $flags)");
+
+    rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
+
+    if (!rx)
+        XSRETURN_UNDEF;
+
+    SP -= items;
+
+    flags = (U32)INT2PTR(IV,SvIV(SvRV((SV*)ST(0))));
+    ret = CALLREG_NAMED_BUFF_FETCH(rx, ST(1), flags);
+
+    SPAGAIN;
+
+    if (ret) {
+        if (SvROK(ret))
+            XPUSHs(ret);
+        else
+            XPUSHs(SvREFCNT_inc(ret));
+        PUTBACK;
+        return;
     }
+    XSRETURN_UNDEF;
 }
 
+XS(XS_Tie_Hash_NamedCapture_STORE)
+{
+    dVAR;
+    dXSARGS;
+    REGEXP * rx;
+    U32 flags;
+    PERL_UNUSED_ARG(cv);
+
+    if (items != 3)
+        Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::STORE($key, $value, $flags)");
+
+    rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
 
-XS(XS_re_regnames_iternext)
+    if (!rx) {
+        if (!PL_localizing)
+            Perl_croak(aTHX_ PL_no_modify);
+        else
+            XSRETURN_UNDEF;
+    }
+
+    SP -= items;
+
+    flags = (U32)INT2PTR(IV,SvIV(SvRV((SV*)ST(0))));
+    CALLREG_NAMED_BUFF_STORE(rx,ST(1), ST(2), flags);
+}
+
+XS(XS_Tie_Hash_NamedCapture_DELETE)
 {
-    dVAR; 
+    dVAR;
     dXSARGS;
-    if (items < 0 || items > 1)
-       Perl_croak(aTHX_ "Usage: %s(%s)", "re::regnames_iternext", "[all]");
-    PERL_UNUSED_VAR(cv); /* -W */
-    PERL_UNUSED_VAR(ax); /* -Wall */
+    REGEXP * rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
+    U32 flags;
+    PERL_UNUSED_ARG(cv);
+
+    if (items != 2)
+        Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::DELETE($key, $flags)");
+
+    if (!rx)
+        Perl_croak(aTHX_ PL_no_modify);
+
     SP -= items;
-    {
-       SV *    all;
-        regexp *re = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
 
-       if (items < 1)
-           all = NULL;
-       else {
-           all = ST(0);
-       }
-        if (re && re->paren_names) {
-            HV *hv= re->paren_names;
-            while (1) {
-                HE *temphe = hv_iternext_flags(hv,0);
-                if (temphe) {
-                    IV i;
-                    IV parno = 0;
-                    SV* sv_dat = HeVAL(temphe);
-                    I32 *nums = (I32*)SvPVX(sv_dat);
-                    for ( i = 0; i < SvIVX(sv_dat); i++ ) {
-                        if ((I32)(re->lastcloseparen) >= nums[i] &&
-                            re->offs[nums[i]].start != -1 &&
-                            re->offs[nums[i]].end != -1)
-                        {
-                            parno = nums[i];
-                            break;
-                        }
-                    }
-                    if (parno || (all && SvTRUE(all))) {
-                        STRLEN len;
-                        char *pv = HePV(temphe, len);
-                        XPUSHs(newSVpvn(pv,len));
-                        XSRETURN(1);    
-                    }
-                } else {
-                    break;
-                }
-            }
-        }
+    flags = (U32)INT2PTR(IV,SvIV(SvRV((SV*)ST(0))));
+    CALLREG_NAMED_BUFF_DELETE(rx, ST(1), flags);
+}
+
+XS(XS_Tie_Hash_NamedCapture_CLEAR)
+{
+    dVAR;
+    dXSARGS;
+    REGEXP * rx;
+    U32 flags;
+    PERL_UNUSED_ARG(cv);
+
+    if (items != 1)
+        Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::CLEAR($flags)");
+
+    rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
+
+    if (!rx)
+        Perl_croak(aTHX_ PL_no_modify);
+
+    SP -= items;
+
+    flags = (U32)INT2PTR(IV,SvIV(SvRV((SV*)ST(0))));
+    CALLREG_NAMED_BUFF_CLEAR(rx, flags);
+}
+
+XS(XS_Tie_Hash_NamedCapture_EXISTS)
+{
+    dVAR;
+    dXSARGS;
+    REGEXP * rx;
+    U32 flags;
+    SV * ret;
+    PERL_UNUSED_ARG(cv);
+
+    if (items != 2)
+        Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::EXISTS($key, $flags)");
+
+    rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
+
+    if (!rx)
         XSRETURN_UNDEF;
+
+    SP -= items;
+
+    flags = (U32)INT2PTR(IV,SvIV(SvRV((SV*)ST(0))));
+    ret = CALLREG_NAMED_BUFF_EXISTS(rx, ST(1), flags);
+
+    SPAGAIN;
+
+       XPUSHs(ret);
        PUTBACK;
        return;
-    }
 }
 
+XS(XS_Tie_Hash_NamedCapture_FIRSTK)
+{
+    dVAR;
+    dXSARGS;
+    REGEXP * rx;
+    U32 flags;
+    SV * ret;
+    PERL_UNUSED_ARG(cv);
+
+    if (items != 1)
+        Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::FIRSTKEY()");
 
-XS(XS_re_regnames_count)
+    rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
+
+    if (!rx)
+        XSRETURN_UNDEF;
+
+    SP -= items;
+
+    flags = (U32)INT2PTR(IV,SvIV(SvRV((SV*)ST(0))));
+    ret = CALLREG_NAMED_BUFF_FIRSTKEY(rx, flags);
+
+    SPAGAIN;
+
+    if (ret) {
+        XPUSHs(SvREFCNT_inc(ret));
+        PUTBACK;
+    } else {
+        XSRETURN_UNDEF;
+    }
+
+}
+
+XS(XS_Tie_Hash_NamedCapture_NEXTK)
 {
-    regexp *re = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
-    dVAR; 
+    dVAR;
     dXSARGS;
+    REGEXP * rx;
+    U32 flags;
+    SV * ret;
+    PERL_UNUSED_ARG(cv);
+
+    if (items != 2)
+        Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::NEXTKEY($lastkey)");
+
+    rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
+
+    if (!rx)
+        XSRETURN_UNDEF;
 
-    if (items != 0)
-       Perl_croak(aTHX_ "Usage: %s(%s)", "re::regnames_count", "");
-    PERL_UNUSED_VAR(cv); /* -W */
-    PERL_UNUSED_VAR(ax); /* -Wall */
     SP -= items;
-    
-    if (re && re->paren_names) {
-        XPUSHs(newSViv(HvTOTALKEYS(re->paren_names)));
+
+    flags = (U32)INT2PTR(IV,SvIV(SvRV((SV*)ST(0))));
+    ret = CALLREG_NAMED_BUFF_NEXTKEY(rx, ST(1), flags);
+
+    SPAGAIN;
+
+    if (ret) {
+        XPUSHs(ret);
     } else {
         XSRETURN_UNDEF;
     }  
     PUTBACK;
-    return;
+}
+
+XS(XS_Tie_Hash_NamedCapture_SCALAR)
+{
+    dVAR;
+    dXSARGS;
+    REGEXP * rx;
+    U32 flags;
+    SV * ret;
+    PERL_UNUSED_ARG(cv);
+
+    if (items != 1)
+        Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::SCALAR()");
+
+    rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
+
+    if (!rx)
+        XSRETURN_UNDEF;
+
+    SP -= items;
+
+    flags = (U32)INT2PTR(IV,SvIV(SvRV((SV*)ST(0))));
+    ret = CALLREG_NAMED_BUFF_SCALAR(rx, flags);
+
+    SPAGAIN;
+
+    if (ret) {
+        XPUSHs(ret);
+        PUTBACK;
+        return;
+    } else {
+        XSRETURN_UNDEF;
+    }
+}
+
+XS(XS_Tie_Hash_NamedCapture_flags)
+{
+    dVAR;
+    dXSARGS;
+    PERL_UNUSED_ARG(cv);
+
+    if (items != 0)
+        Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::flags()");
+
+       mXPUSHu(RXapif_ONE);
+       mXPUSHu(RXapif_ALL);
+       PUTBACK;
+       return;
 }