This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Revert 4 regex commits to ease rebasing
[perl5.git] / pp_ctl.c
index 85ee20f..2cde665 100644 (file)
--- a/pp_ctl.c
+++ b/pp_ctl.c
@@ -205,7 +205,9 @@ PP(pp_regcomp)
            if (PL_op->op_flags & OPf_SPECIAL)
                PL_reginterp_cnt = I32_MAX; /* Mark as safe.  */
 
-           if (!DO_UTF8(tmpstr) && SvUTF8(tmpstr)) {
+           if (DO_UTF8(tmpstr)) {
+               assert (SvUTF8(tmpstr));
+           } else if (SvUTF8(tmpstr)) {
                /* Not doing UTF-8, despite what the SV says. Is this only if
                   we're trapped in use 'bytes'?  */
                /* Make a copy of the octet sequence, but without the flag on,
@@ -214,11 +216,19 @@ PP(pp_regcomp)
                const char *const p = SvPV(tmpstr, len);
                tmpstr = newSVpvn_flags(p, len, SVs_TEMP);
            }
-           else if (SvAMAGIC(tmpstr) || SvGMAGICAL(tmpstr)) {
+           else if (SvAMAGIC(tmpstr)) {
                /* make a copy to avoid extra stringifies */
                tmpstr = newSVpvn_flags(t, len, SVs_TEMP | SvUTF8(tmpstr));
            }
 
+           /* If it is gmagical, create a mortal copy, but without calling
+              get-magic, as we have already done that. */
+           if(SvGMAGICAL(tmpstr)) {
+               SV *mortalcopy = sv_newmortal();
+               sv_setsv_flags(mortalcopy, tmpstr, 0);
+               tmpstr = mortalcopy;
+           }
+
            if (eng)
                PM_SETRE(pm, CALLREGCOMP_ENG(eng, tmpstr, pm_flags));
            else
@@ -295,13 +305,6 @@ PP(pp_substcont)
        s -= RX_GOFS(rx);
 
        /* Are we done */
-       /* I believe that we can't set REXEC_SCREAM here if
-          SvSCREAM(cx->sb_targ) is true because SvPVX(cx->sb_targ) isn't always
-          equal to s.  [See the comment before Perl_re_intuit_start(), which is
-          called from Perl_regexec_flags(), which says that it should be when
-          SvSCREAM() is true.]  s, cx->sb_strend and orig will be consistent
-          with SvPVX(cx->sb_targ), as substconst doesn't modify cx->sb_targ
-          during the match.  */
        if (CxONCE(cx) || s < orig ||
                !CALLREGEXEC(rx, s, cx->sb_strend, orig,
                             (s == m) + RX_GOFS(rx), cx->sb_targ, NULL,
@@ -328,11 +331,9 @@ PP(pp_substcont)
                targ = dstr;
            }
            else {
-#ifdef PERL_OLD_COPY_ON_WRITE
                if (SvIsCOW(targ)) {
                    sv_force_normal_flags(targ, SV_COW_DROP_PV);
                } else
-#endif
                {
                    SvPV_free(targ);
                }
@@ -1381,7 +1382,7 @@ static const char * const context_name[] = {
 };
 
 STATIC I32
-S_dopoptolabel(pTHX_ const char *label)
+S_dopoptolabel(pTHX_ const char *label, STRLEN len, U32 flags)
 {
     dVAR;
     register I32 i;
@@ -1396,6 +1397,7 @@ S_dopoptolabel(pTHX_ const char *label)
        case CXt_FORMAT:
        case CXt_EVAL:
        case CXt_NULL:
+           /* diag_listed_as: Exiting subroutine via %s */
            Perl_ck_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s",
                           context_name[CxTYPE(cx)], OP_NAME(PL_op));
            if (CxTYPE(cx) == CXt_NULL)
@@ -1406,8 +1408,20 @@ S_dopoptolabel(pTHX_ const char *label)
        case CXt_LOOP_FOR:
        case CXt_LOOP_PLAIN:
          {
-           const char *cx_label = CxLABEL(cx);
-           if (!cx_label || strNE(label, cx_label) ) {
+            STRLEN cx_label_len = 0;
+            U32 cx_label_flags = 0;
+           const char *cx_label = CxLABEL_len_flags(cx, &cx_label_len, &cx_label_flags);
+           if (!cx_label || !(
+                    ( (cx_label_flags & SVf_UTF8) != (flags & SVf_UTF8) ) ?
+                        (flags & SVf_UTF8)
+                            ? (bytes_cmp_utf8(
+                                        (const U8*)cx_label, cx_label_len,
+                                        (const U8*)label, len) == 0)
+                            : (bytes_cmp_utf8(
+                                        (const U8*)label, len,
+                                        (const U8*)cx_label, cx_label_len) == 0)
+                    : (len == cx_label_len && ((cx_label == label)
+                                    || memEQ(cx_label, label, len))) )) {
                DEBUG_l(Perl_deb(aTHX_ "(poptolabel(): skipping label at cx=%ld %s)\n",
                        (long)i, cx_label));
                continue;
@@ -1533,6 +1547,7 @@ S_dopoptoloop(pTHX_ I32 startingblock)
        case CXt_FORMAT:
        case CXt_EVAL:
        case CXt_NULL:
+           /* diag_listed_as: Exiting subroutine via %s */
            Perl_ck_warner(aTHX_ packWARN(WARN_EXITING), "Exiting %s via %s",
                           context_name[CxTYPE(cx)], OP_NAME(PL_op));
            if ((CxTYPE(cx)) == CXt_NULL)
@@ -1878,7 +1893,11 @@ PP(pp_caller)
        RETURN;
     }
 
-    stash_hek = HvNAME_HEK((HV*)CopSTASH(cx->blk_oldcop));
+    DEBUG_CX("CALLER");
+    assert(CopSTASH(cx->blk_oldcop));
+    stash_hek = SvTYPE(CopSTASH(cx->blk_oldcop)) == SVt_PVHV
+      ? HvNAME_HEK((HV*)CopSTASH(cx->blk_oldcop))
+      : NULL;
     if (GIMME != G_ARRAY) {
         EXTEND(SP, 1);
        if (!stash_hek)
@@ -2351,13 +2370,24 @@ S_return_lvalues(pTHX_ SV **mark, SV **sp, SV **newsp, I32 gimme,
        if (MARK < SP) {
              copy_sv:
                if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
+                   if (!SvPADTMP(*SP)) {
                        *++newsp = SvREFCNT_inc(*SP);
                        FREETMPS;
                        sv_2mortal(*newsp);
+                   }
+                   else {
+                       /* FREETMPS could clobber it */
+                       SV *sv = SvREFCNT_inc(*SP);
+                       FREETMPS;
+                       *++newsp = sv_mortalcopy(sv);
+                       SvREFCNT_dec(sv);
+                   }
                }
                else
                    *++newsp =
-                       !SvTEMP(*SP)
+                     SvPADTMP(*SP)
+                      ? sv_mortalcopy(*SP)
+                      : !SvTEMP(*SP)
                          ? sv_2mortal(SvREFCNT_inc_simple_NN(*SP))
                          : *SP;
        }
@@ -2377,10 +2407,10 @@ S_return_lvalues(pTHX_ SV **mark, SV **sp, SV **newsp, I32 gimme,
        if (ref || !CxLVAL(cx))
            while (++MARK <= SP)
                *++newsp =
-                    SvTEMP(*MARK)
-                      ? *MARK
-                      : ref && SvFLAGS(*MARK) & SVs_PADTMP
+                      SvFLAGS(*MARK) & SVs_PADTMP
                           ? sv_mortalcopy(*MARK)
+                    : SvTEMP(*MARK)
+                          ? *MARK
                           : sv_2mortal(SvREFCNT_inc_simple_NN(*MARK));
        else while (++MARK <= SP) {
            if (*MARK != &PL_sv_undef
@@ -2397,6 +2427,7 @@ S_return_lvalues(pTHX_ SV **mark, SV **sp, SV **newsp, I32 gimme,
                    POPSUB(cx,sv);
                    PL_curpm = newpm;
                    LEAVESUB(sv);
+              /* diag_listed_as: Can't return %s from lvalue subroutine */
                    Perl_croak(aTHX_
                        "Can't return a %s from lvalue subroutine",
                        SvREADONLY(TOPs) ? "readonly value" : "temporary");
@@ -2486,7 +2517,7 @@ PP(pp_return)
        retop = cx->blk_sub.retop;
        break;
     default:
-       DIE(aTHX_ "panic: return");
+       DIE(aTHX_ "panic: return, type=%u", (unsigned) CxTYPE(cx));
     }
 
     TAINT_NOT;
@@ -2496,7 +2527,8 @@ PP(pp_return)
        if (MARK < SP) {
            if (popsub2) {
                if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
-                   if (SvTEMP(TOPs) && SvREFCNT(TOPs) == 1) {
+                   if (SvTEMP(TOPs) && SvREFCNT(TOPs) == 1
+                        && !SvMAGICAL(TOPs)) {
                        *++newsp = SvREFCNT_inc(*SP);
                        FREETMPS;
                        sv_2mortal(*newsp);
@@ -2508,7 +2540,8 @@ PP(pp_return)
                        SvREFCNT_dec(sv);
                    }
                }
-               else if (SvTEMP(*SP) && SvREFCNT(*SP) == 1) {
+               else if (SvTEMP(*SP) && SvREFCNT(*SP) == 1
+                         && !SvMAGICAL(*SP)) {
                    *++newsp = *SP;
                }
                else
@@ -2523,6 +2556,7 @@ PP(pp_return)
       else if (gimme == G_ARRAY) {
        while (++MARK <= SP) {
            *++newsp = popsub2 && SvTEMP(*MARK) && SvREFCNT(*MARK) == 1
+                              && !SvGMAGICAL(*MARK)
                        ? *MARK : sv_mortalcopy(*MARK);
            TAINT_NOT;          /* Each item is independent */
        }
@@ -2598,9 +2632,14 @@ PP(pp_last)
            DIE(aTHX_ "Can't \"last\" outside a loop block");
     }
     else {
-       cxix = dopoptolabel(cPVOP->op_pv);
+        cxix = dopoptolabel(cPVOP->op_pv, strlen(cPVOP->op_pv),
+                           (cPVOP->op_private & OPpPV_IS_UTF8) ? SVf_UTF8 : 0);
        if (cxix < 0)
-           DIE(aTHX_ "Label not found for \"last %s\"", cPVOP->op_pv);
+           DIE(aTHX_ "Label not found for \"last %"SVf"\"",
+                                        SVfARG(newSVpvn_flags(cPVOP->op_pv,
+                                                    strlen(cPVOP->op_pv),
+                                                    ((cPVOP->op_private & OPpPV_IS_UTF8)
+                                                    ? SVf_UTF8 : 0) | SVs_TEMP)));
     }
     if (cxix < cxstack_ix)
        dounwind(cxix);
@@ -2630,7 +2669,7 @@ PP(pp_last)
        nextop = cx->blk_sub.retop;
        break;
     default:
-       DIE(aTHX_ "panic: last");
+       DIE(aTHX_ "panic: last, type=%u", (unsigned) CxTYPE(cx));
     }
 
     TAINT_NOT;
@@ -2674,9 +2713,14 @@ PP(pp_next)
            DIE(aTHX_ "Can't \"next\" outside a loop block");
     }
     else {
-       cxix = dopoptolabel(cPVOP->op_pv);
-       if (cxix < 0)
-           DIE(aTHX_ "Label not found for \"next %s\"", cPVOP->op_pv);
+       cxix = dopoptolabel(cPVOP->op_pv, strlen(cPVOP->op_pv),
+                           (cPVOP->op_private & OPpPV_IS_UTF8) ? SVf_UTF8 : 0);
+       if (cxix < 0)
+           DIE(aTHX_ "Label not found for \"next %"SVf"\"",
+                                        SVfARG(newSVpvn_flags(cPVOP->op_pv, 
+                                                    strlen(cPVOP->op_pv),
+                                                    ((cPVOP->op_private & OPpPV_IS_UTF8)
+                                                    ? SVf_UTF8 : 0) | SVs_TEMP)));
     }
     if (cxix < cxstack_ix)
        dounwind(cxix);
@@ -2705,9 +2749,14 @@ PP(pp_redo)
            DIE(aTHX_ "Can't \"redo\" outside a loop block");
     }
     else {
-       cxix = dopoptolabel(cPVOP->op_pv);
-       if (cxix < 0)
-           DIE(aTHX_ "Label not found for \"redo %s\"", cPVOP->op_pv);
+       cxix = dopoptolabel(cPVOP->op_pv, strlen(cPVOP->op_pv),
+                           (cPVOP->op_private & OPpPV_IS_UTF8) ? SVf_UTF8 : 0);
+       if (cxix < 0)
+           DIE(aTHX_ "Label not found for \"redo %"SVf"\"",
+                                        SVfARG(newSVpvn_flags(cPVOP->op_pv,
+                                                    strlen(cPVOP->op_pv),
+                                                    ((cPVOP->op_private & OPpPV_IS_UTF8)
+                                                    ? SVf_UTF8 : 0) | SVs_TEMP)));
     }
     if (cxix < cxstack_ix)
        dounwind(cxix);
@@ -2729,7 +2778,7 @@ PP(pp_redo)
 }
 
 STATIC OP *
-S_dofindlabel(pTHX_ OP *o, const char *label, OP **opstack, OP **oplimit)
+S_dofindlabel(pTHX_ OP *o, const char *label, STRLEN len, U32 flags, OP **opstack, OP **oplimit)
 {
     dVAR;
     OP **ops = opstack;
@@ -2755,8 +2804,21 @@ S_dofindlabel(pTHX_ OP *o, const char *label, OP **opstack, OP **oplimit)
        /* First try all the kids at this level, since that's likeliest. */
        for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
            if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) {
-               const char *kid_label = CopLABEL(kCOP);
-               if (kid_label && strEQ(kid_label, label))
+                STRLEN kid_label_len;
+                U32 kid_label_flags;
+               const char *kid_label = CopLABEL_len_flags(kCOP,
+                                                    &kid_label_len, &kid_label_flags);
+               if (kid_label && (
+                    ( (kid_label_flags & SVf_UTF8) != (flags & SVf_UTF8) ) ?
+                        (flags & SVf_UTF8)
+                            ? (bytes_cmp_utf8(
+                                        (const U8*)kid_label, kid_label_len,
+                                        (const U8*)label, len) == 0)
+                            : (bytes_cmp_utf8(
+                                        (const U8*)label, len,
+                                        (const U8*)kid_label, kid_label_len) == 0)
+                    : ( len == kid_label_len && ((kid_label == label)
+                                    || memEQ(kid_label, label, len)))))
                    return kid;
            }
        }
@@ -2772,7 +2834,7 @@ S_dofindlabel(pTHX_ OP *o, const char *label, OP **opstack, OP **oplimit)
                else
                    *ops++ = kid;
            }
-           if ((o = dofindlabel(kid, label, ops, oplimit)))
+           if ((o = dofindlabel(kid, label, len, flags, ops, oplimit)))
                return o;
        }
     }
@@ -2789,6 +2851,8 @@ PP(pp_goto)
 #define GOTO_DEPTH 64
     OP *enterops[GOTO_DEPTH];
     const char *label = NULL;
+    STRLEN label_len = 0;
+    U32 label_flags = 0;
     const bool do_dump = (PL_op->op_type == OP_DUMP);
     static const char must_have_label[] = "goto must have label";
 
@@ -2839,8 +2903,10 @@ PP(pp_goto)
            /* ban goto in eval: see <20050521150056.GC20213@iabyn.com> */
            if (CxTYPE(cx) == CXt_EVAL) {
                if (CxREALEVAL(cx))
+               /* diag_listed_as: Can't goto subroutine from an eval-%s */
                    DIE(aTHX_ "Can't goto subroutine from an eval-string");
                else
+               /* diag_listed_as: Can't goto subroutine from an eval-%s */
                    DIE(aTHX_ "Can't goto subroutine from an eval-block");
            }
            else if (CxMULTICALL(cx))
@@ -2879,6 +2945,19 @@ PP(pp_goto)
            oldsave = PL_scopestack[PL_scopestack_ix - 1];
            LEAVE_SCOPE(oldsave);
 
+           /* A destructor called during LEAVE_SCOPE could have undefined
+            * our precious cv.  See bug #99850. */
+           if (!CvROOT(cv) && !CvXSUB(cv)) {
+               const GV * const gv = CvGV(cv);
+               if (gv) {
+                   SV * const tmpstr = sv_newmortal();
+                   gv_efullname3(tmpstr, gv, NULL);
+                   DIE(aTHX_ "Goto undefined subroutine &%"SVf"",
+                              SVfARG(tmpstr));
+               }
+               DIE(aTHX_ "Goto undefined subroutine");
+           }
+
            /* Now do some callish stuff. */
            SAVETMPS;
            SAVEFREESV(cv); /* later, undo the 'avoid premature free' hack */
@@ -2974,21 +3053,20 @@ PP(pp_goto)
            }
        }
        else {
-           label = SvPV_nolen_const(sv);
-           if (!(do_dump || *label))
-               DIE(aTHX_ must_have_label);
+           label       = SvPV_const(sv, label_len);
+            label_flags = SvUTF8(sv);
        }
     }
-    else if (PL_op->op_flags & OPf_SPECIAL) {
-       if (! do_dump)
-           DIE(aTHX_ must_have_label);
+    else if (!(PL_op->op_flags & OPf_SPECIAL)) {
+       label       = cPVOP->op_pv;
+        label_flags = (cPVOP->op_private & OPpPV_IS_UTF8) ? SVf_UTF8 : 0;
+        label_len   = strlen(label);
     }
-    else
-       label = cPVOP->op_pv;
+    if (!(do_dump || label_len)) DIE(aTHX_ must_have_label);
 
     PERL_ASYNC_CHECK();
 
-    if (label && *label) {
+    if (label_len) {
        OP *gotoprobe = NULL;
        bool leaving_eval = FALSE;
        bool in_block = FALSE;
@@ -3039,12 +3117,13 @@ PP(pp_goto)
                DIE(aTHX_ "Can't \"goto\" out of a pseudo block");
            default:
                if (ix)
-                   DIE(aTHX_ "panic: goto");
+                   DIE(aTHX_ "panic: goto, type=%u, ix=%ld",
+                       CxTYPE(cx), (long) ix);
                gotoprobe = PL_main_root;
                break;
            }
            if (gotoprobe) {
-               retop = dofindlabel(gotoprobe, label,
+               retop = dofindlabel(gotoprobe, label, label_len, label_flags,
                                    enterops, enterops + GOTO_DEPTH);
                if (retop)
                    break;
@@ -3052,7 +3131,8 @@ PP(pp_goto)
                        gotoprobe->op_sibling->op_type == OP_UNSTACK &&
                        gotoprobe->op_sibling->op_sibling) {
                    retop = dofindlabel(gotoprobe->op_sibling->op_sibling,
-                                       label, enterops, enterops + GOTO_DEPTH);
+                                       label, label_len, label_flags, enterops,
+                                       enterops + GOTO_DEPTH);
                    if (retop)
                        break;
                }
@@ -3060,7 +3140,9 @@ PP(pp_goto)
            PL_lastgotoprobe = gotoprobe;
        }
        if (!retop)
-           DIE(aTHX_ "Can't find label %s", label);
+           DIE(aTHX_ "Can't find label %"SVf,
+                            SVfARG(newSVpvn_flags(label, label_len,
+                                        SVs_TEMP | label_flags)));
 
        /* if we're leaving an eval, check before we pop any frames
            that we're not going to punt, otherwise the error
@@ -3352,9 +3434,9 @@ Perl_sv_compile_2op_is_broken(pTHX_ SV *sv, OP **startop, const char *code,
     CATCH_SET(TRUE);
 
     if (runtime)
-       (void) doeval(G_SCALAR, startop, runcv, PL_curcop->cop_seq);
+       (void) doeval(G_SCALAR, startop, runcv, PL_curcop->cop_seq, NULL);
     else
-       (void) doeval(G_SCALAR, startop, PL_compcv, PL_cop_seqmax);
+       (void) doeval(G_SCALAR, startop, PL_compcv, PL_cop_seqmax, NULL);
     CATCH_SET(need_catch);
     POPBLOCK(cx,PL_curpm);
     POPEVAL(cx);
@@ -3410,7 +3492,7 @@ Perl_find_runcv(pTHX_ U32 *db_seqp)
                return cv;
            }
            else if (CxTYPE(cx) == CXt_EVAL && !CxTRYBLOCK(cx))
-               return PL_compcv;
+               return cx->blk_eval.cv;
        }
     }
     return PL_main_cv;
@@ -3455,13 +3537,22 @@ S_try_yyparse(pTHX_ int gramtype)
  * pushes undef (also croaks if startop != NULL).
  */
 
+/* This function is called from three places, sv_compile_2op, pp_require
+ * and pp_entereval.  These can be distinguished as follows:
+ *    sv_compile_2op - startop is non-null
+ *    pp_require     - startop is null; saveop is not entereval
+ *    pp_entereval   - startop is null; saveop is entereval
+ */
+
 STATIC bool
-S_doeval(pTHX_ int gimme, OP** startop, CV* outside, U32 seq)
+S_doeval(pTHX_ int gimme, OP** startop, CV* outside, U32 seq, HV *hh)
 {
     dVAR; dSP;
     OP * const saveop = PL_op;
+    COP * const oldcurcop = PL_curcop;
     bool in_require = (saveop && saveop->op_type == OP_REQUIRE);
     int yystatus;
+    CV *evalcv;
 
     PL_in_eval = (in_require
                  ? (EVAL_INREQUIRE | (PL_in_eval & EVAL_INEVAL))
@@ -3469,24 +3560,23 @@ S_doeval(pTHX_ int gimme, OP** startop, CV* outside, U32 seq)
 
     PUSHMARK(SP);
 
-    SAVESPTR(PL_compcv);
-    PL_compcv = MUTABLE_CV(newSV_type(SVt_PVCV));
-    CvEVAL_on(PL_compcv);
+    evalcv = MUTABLE_CV(newSV_type(SVt_PVCV));
+    CvEVAL_on(evalcv);
     assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
-    cxstack[cxstack_ix].blk_eval.cv = PL_compcv;
+    cxstack[cxstack_ix].blk_eval.cv = evalcv;
     cxstack[cxstack_ix].blk_gimme = gimme;
 
-    CvOUTSIDE_SEQ(PL_compcv) = seq;
-    CvOUTSIDE(PL_compcv) = MUTABLE_CV(SvREFCNT_inc_simple(outside));
+    CvOUTSIDE_SEQ(evalcv) = seq;
+    CvOUTSIDE(evalcv) = MUTABLE_CV(SvREFCNT_inc_simple(outside));
 
     /* set up a scratch pad */
 
-    CvPADLIST(PL_compcv) = pad_new(padnew_SAVE);
+    CvPADLIST(evalcv) = pad_new(padnew_SAVE);
     PL_op = NULL; /* avoid PL_op and PL_curpad referring to different CVs */
 
 
     if (!PL_madskills)
-       SAVEMORTALIZESV(PL_compcv);     /* must remain until end of current statement */
+       SAVEMORTALIZESV(evalcv);        /* must remain until end of current statement */
 
     /* make sure we compile in the right package */
 
@@ -3507,6 +3597,10 @@ S_doeval(pTHX_ int gimme, OP** startop, CV* outside, U32 seq)
     PL_madskills = 0;
 #endif
 
+    if (!startop) ENTER_with_name("evalcomp");
+    SAVESPTR(PL_compcv);
+    PL_compcv = evalcv;
+
     /* try to compile it */
 
     PL_eval_root = NULL;
@@ -3516,6 +3610,49 @@ S_doeval(pTHX_ int gimme, OP** startop, CV* outside, U32 seq)
     else
        CLEAR_ERRSV();
 
+    if (!startop) {
+       bool clear_hints = saveop->op_type != OP_ENTEREVAL;
+       SAVEHINTS();
+       if (clear_hints) {
+           PL_hints = 0;
+           hv_clear(GvHV(PL_hintgv));
+       }
+       else {
+           PL_hints = saveop->op_private & OPpEVAL_COPHH
+                        ? oldcurcop->cop_hints : saveop->op_targ;
+           if (hh) {
+               /* SAVEHINTS created a new HV in PL_hintgv, which we need to GC */
+               SvREFCNT_dec(GvHV(PL_hintgv));
+               GvHV(PL_hintgv) = hh;
+           }
+       }
+       SAVECOMPILEWARNINGS();
+       if (clear_hints) {
+           if (PL_dowarn & G_WARN_ALL_ON)
+               PL_compiling.cop_warnings = pWARN_ALL ;
+           else if (PL_dowarn & G_WARN_ALL_OFF)
+               PL_compiling.cop_warnings = pWARN_NONE ;
+           else
+               PL_compiling.cop_warnings = pWARN_STD ;
+       }
+       else {
+           PL_compiling.cop_warnings =
+               DUP_WARNINGS(oldcurcop->cop_warnings);
+           cophh_free(CopHINTHASH_get(&PL_compiling));
+           if (Perl_cop_fetch_label(aTHX_ oldcurcop, NULL, NULL)) {
+               /* The label, if present, is the first entry on the chain. So rather
+                  than writing a blank label in front of it (which involves an
+                  allocation), just use the next entry in the chain.  */
+               PL_compiling.cop_hints_hash
+                   = cophh_copy(oldcurcop->cop_hints_hash->refcounted_he_next);
+               /* Check the assumption that this removed the label.  */
+               assert(Perl_cop_fetch_label(aTHX_ &PL_compiling, NULL, NULL) == NULL);
+           }
+           else
+               PL_compiling.cop_hints_hash = cophh_copy(oldcurcop->cop_hints_hash);
+       }
+    }
+
     CALL_BLOCK_HOOKS(bhk_eval, saveop);
 
     /* note that yyparse() may raise an exception, e.g. C<BEGIN{die}>,
@@ -3548,9 +3685,9 @@ S_doeval(pTHX_ int gimme, OP** startop, CV* outside, U32 seq)
                POPEVAL(cx);
                namesv = cx->blk_eval.old_namesv;
            }
-       }
-       if (yystatus != 3)
+           /* POPBLOCK renders LEAVE_with_name("evalcomp") unnecessary. */
            LEAVE_with_name("eval"); /* pp_entereval knows about this LEAVE.  */
+       }
 
        if (in_require) {
            if (!cx) {
@@ -3584,10 +3721,11 @@ S_doeval(pTHX_ int gimme, OP** startop, CV* outside, U32 seq)
                sv_setpvs(ERRSV, "Compilation error");
            }
        }
-       PUSHs(&PL_sv_undef);
+       if (gimme != G_ARRAY) PUSHs(&PL_sv_undef);
        PUTBACK;
        return FALSE;
     }
+    else if (!startop) LEAVE_with_name("evalcomp");
     CopLINE_set(&PL_compiling, 0);
     if (startop) {
        *startop = PL_eval_root;
@@ -3616,7 +3754,7 @@ S_doeval(pTHX_ int gimme, OP** startop, CV* outside, U32 seq)
 
     /* compiled okay, so do it */
 
-    CvDEPTH(PL_compcv) = 1;
+    CvDEPTH(evalcv) = 1;
     SP = PL_stack_base + POPMARK;              /* pop original mark */
     PL_op = saveop;                    /* The caller may need it. */
     PL_parser->lex_state = LEX_NOTPARSING;     /* $^S needs this. */
@@ -4052,18 +4190,6 @@ PP(pp_require)
     CopFILE_set(&PL_compiling, tryname);
     lex_start(NULL, tryrsfp, 0);
 
-    SAVEHINTS();
-    PL_hints = 0;
-    hv_clear(GvHV(PL_hintgv));
-
-    SAVECOMPILEWARNINGS();
-    if (PL_dowarn & G_WARN_ALL_ON)
-        PL_compiling.cop_warnings = pWARN_ALL ;
-    else if (PL_dowarn & G_WARN_ALL_OFF)
-        PL_compiling.cop_warnings = pWARN_NONE ;
-    else
-        PL_compiling.cop_warnings = pWARN_STD ;
-
     if (filter_sub || filter_cache) {
        /* We can use the SvPV of the filter PVIO itself as our cache, rather
           than hanging another SV from it. In turn, filter_add() optionally
@@ -4089,7 +4215,7 @@ PP(pp_require)
     encoding = PL_encoding;
     PL_encoding = NULL;
 
-    if (doeval(gimme, NULL, NULL, PL_curcop->cop_seq))
+    if (doeval(gimme, NULL, NULL, PL_curcop->cop_seq, NULL))
        op = DOCATCH(PL_eval_start);
     else
        op = PL_op->op_next;
@@ -4154,7 +4280,7 @@ PP(pp_entereval)
            SvPVbyte_force(sv, len);
     }
     else if (bytes && SvUTF8(sv)) {
-       /* Don’t modify someone else’s scalar */
+       /* Don't modify someone else's scalar */
        STRLEN len;
        sv = newSVsv(sv);
        (void)sv_2mortal(sv);
@@ -4189,33 +4315,6 @@ PP(pp_entereval)
     CopFILE_set(&PL_compiling, tmpbuf+2);
     SAVECOPLINE(&PL_compiling);
     CopLINE_set(&PL_compiling, 1);
-    /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
-       deleting the eval's FILEGV from the stash before gv_check() runs
-       (i.e. before run-time proper). To work around the coredump that
-       ensues, we always turn GvMULTI_on for any globals that were
-       introduced within evals. See force_ident(). GSAR 96-10-12 */
-    SAVEHINTS();
-    PL_hints = PL_op->op_private & OPpEVAL_COPHH
-                ? PL_curcop->cop_hints : PL_op->op_targ;
-    if (saved_hh) {
-       /* SAVEHINTS created a new HV in PL_hintgv, which we need to GC */
-       SvREFCNT_dec(GvHV(PL_hintgv));
-       GvHV(PL_hintgv) = saved_hh;
-    }
-    SAVECOMPILEWARNINGS();
-    PL_compiling.cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings);
-    cophh_free(CopHINTHASH_get(&PL_compiling));
-    if (Perl_cop_fetch_label(aTHX_ PL_curcop, NULL, NULL)) {
-       /* The label, if present, is the first entry on the chain. So rather
-          than writing a blank label in front of it (which involves an
-          allocation), just use the next entry in the chain.  */
-       PL_compiling.cop_hints_hash
-           = cophh_copy(PL_curcop->cop_hints_hash->refcounted_he_next);
-       /* Check the assumption that this removed the label.  */
-       assert(Perl_cop_fetch_label(aTHX_ &PL_compiling, NULL, NULL) == NULL);
-    }
-    else
-       PL_compiling.cop_hints_hash = cophh_copy(PL_curcop->cop_hints_hash);
     /* special case: an eval '' executed within the DB package gets lexically
      * placed in the first non-DB CV rather than the current CV - this
      * allows the debugger to execute code, find lexicals etc, in the
@@ -4232,6 +4331,11 @@ PP(pp_entereval)
     if ((PERLDB_LINE || PERLDB_SAVESRC) && PL_curstash != PL_debstash)
        save_lines(CopFILEAV(&PL_compiling), PL_parser->linestr);
     else {
+       /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
+          deleting the eval's FILEGV from the stash before gv_check() runs
+          (i.e. before run-time proper). To work around the coredump that
+          ensues, we always turn GvMULTI_on for any globals that were
+          introduced within evals. See force_ident(). GSAR 96-10-12 */
        char *const safestr = savepvn(tmpbuf, len);
        SAVEDELETE(PL_defstash, safestr, len);
        saved_delete = TRUE;
@@ -4239,7 +4343,7 @@ PP(pp_entereval)
     
     PUTBACK;
 
-    if (doeval(gimme, NULL, runcv, seq)) {
+    if (doeval(gimme, NULL, runcv, seq, saved_hh)) {
        if (was != PL_breakable_sub_gen /* Some subs defined here. */
            ? (PERLDB_LINE || PERLDB_SAVESRC)
            :  PERLDB_SAVESRC_NOSUBS) {
@@ -4274,12 +4378,14 @@ PP(pp_leaveeval)
     const U8 save_flags = PL_op -> op_flags;
     I32 optype;
     SV *namesv;
+    CV *evalcv;
 
     PERL_ASYNC_CHECK();
     POPBLOCK(cx,newpm);
     POPEVAL(cx);
     namesv = cx->blk_eval.old_namesv;
     retop = cx->blk_eval.retop;
+    evalcv = cx->blk_eval.cv;
 
     TAINT_NOT;
     SP = adjust_stack_on_leave((gimme == G_VOID) ? SP : newsp, SP, newsp,
@@ -4287,9 +4393,9 @@ PP(pp_leaveeval)
     PL_curpm = newpm;  /* Don't pop $1 et al till now */
 
 #ifdef DEBUGGING
-    assert(CvDEPTH(PL_compcv) == 1);
+    assert(CvDEPTH(evalcv) == 1);
 #endif
-    CvDEPTH(PL_compcv) = 0;
+    CvDEPTH(evalcv) = 0;
 
     if (optype == OP_REQUIRE &&
        !(gimme == G_SCALAR ? SvTRUE(*SP) : SP > newsp))
@@ -4511,7 +4617,7 @@ S_do_smartmatch(pTHX_ HV *seen_this, HV *seen_other, const bool copied)
        DEBUG_M(Perl_deb(aTHX_ "    applying rule Any-Object\n"));
        DEBUG_M(Perl_deb(aTHX_ "        attempting overload\n"));
 
-       tmpsv = amagic_call(d, e, smart_amg, 0);
+       tmpsv = amagic_call(d, e, smart_amg, AMGf_noleft);
        if (tmpsv) {
            SPAGAIN;
            (void)POPs;
@@ -4990,7 +5096,9 @@ PP(pp_leavewhen)
 
     cxix = dopoptogiven(cxstack_ix);
     if (cxix < 0)
-       DIE(aTHX_ "Can't use when() outside a topicalizer");
+       /* diag_listed_as: Can't "when" outside a topicalizer */
+       DIE(aTHX_ "Can't \"%s\" outside a topicalizer",
+                  PL_op->op_flags & OPf_SPECIAL ? "default" : "when");
 
     POPBLOCK(cx,newpm);
     assert(CxTYPE(cx) == CXt_WHEN);
@@ -5433,14 +5541,11 @@ S_run_user_filter(pTHX_ int idx, SV *buf_sv, int maxlen)
        int count;
 
        ENTER_with_name("call_filter_sub");
-       save_gp(PL_defgv, 0);
-       GvINTRO_off(PL_defgv);
-       SAVEGENERICSV(GvSV(PL_defgv));
+       SAVE_DEFSV;
        SAVETMPS;
        EXTEND(SP, 2);
 
        DEFSV_set(upstream);
-       SvREFCNT_inc_simple_void_NN(upstream);
        PUSHMARK(SP);
        mPUSHi(0);
        if (filter_state) {
@@ -5560,8 +5665,8 @@ S_path_is_absolute(const char *name)
  * Local variables:
  * c-indentation-style: bsd
  * c-basic-offset: 4
- * indent-tabs-mode: t
+ * indent-tabs-mode: nil
  * End:
  *
- * ex: set ts=8 sts=4 sw=4 noet:
+ * ex: set ts=8 sts=4 sw=4 et:
  */