This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Tweak Tie::Hash::NamedCapture's BOOT code - get the stash from the CV.
authorNicholas Clark <nick@ccl4.org>
Tue, 8 Mar 2011 20:01:46 +0000 (20:01 +0000)
committerNicholas Clark <nick@ccl4.org>
Tue, 8 Mar 2011 21:32:36 +0000 (21:32 +0000)
This will result in less work than using newSVrv(), as the character string
passed to that is used to walk the symbol table to find the stash. We already
have a fast way to get to the stash - via the CV's GV. So use that directly.

ext/Tie-Hash-NamedCapture/NamedCapture.xs

index 459a998..1956b50 100644 (file)
 #define SCALAR_ALIAS (RXapif_SCALAR | (1 << EXPECT_SHIFT))
 
 static
-tie_it(pTHX_ const char name, UV flag)
+tie_it(pTHX_ const char name, UV flag, HV *const stash)
 {
     GV *const gv = gv_fetchpvn(&name, 1, GV_ADDMULTI|GV_NOTQUAL, SVt_PVHV);
     HV *const hv = GvHV(gv);
     SV *rv = newSV_type(SVt_RV);
 
-    sv_setuv(newSVrv(rv, "Tie::Hash::NamedCapture"), flag);
+    SvRV_set(rv, newSVuv(flag));
+    SvROK_on(rv);
+    sv_bless(rv, stash);
 
     sv_unmagic((SV *)hv, PERL_MAGIC_tied);
     sv_magic((SV *)hv, rv, PERL_MAGIC_tied, NULL, 0);
@@ -33,8 +35,11 @@ MODULE = Tie::Hash::NamedCapture     PACKAGE = Tie::Hash::NamedCapture
 PROTOTYPES: DISABLE
 
 BOOT:
-       tie_it(aTHX_ '-', RXapif_ALL);
-       tie_it(aTHX_ '+', RXapif_ONE);
+       {
+           HV *const stash = GvSTASH(CvGV(cv));
+           tie_it(aTHX_ '-', RXapif_ALL, stash);
+           tie_it(aTHX_ '+', RXapif_ONE, stash);
+       }
 
 SV *
 TIEHASH(package, ...)