# define A_HAS_RPEEP A_HAS_PERL(5, 13, 5)
 #endif
 
+#ifndef A_HAS_OPFREEHOOK
+# define A_HAS_OPFREEHOOK A_HAS_PERL(5, 12, 0)
+#endif
+
 /* ... Thread safety and multiplicity ...................................... */
 
 /* Always safe when the workaround isn't needed */
  o = a_old_ck_padany(aTHX_ o);
 
  hint = a_hint();
- if (hint & A_HINT_DO)
+ if (hint & A_HINT_DO) {
   a_map_store_root(o, o->op_ppaddr, hint);
- else
+#if !A_HAS_OPFREEHOOK
+ } else {
   a_map_delete(o);
+#endif
+ }
 
  return o;
 }
  if (hint & A_HINT_DO) {
   a_map_store_root(o, o->op_ppaddr, hint);
   o->op_ppaddr = a_pp_deref;
- } else
+#if !A_HAS_OPFREEHOOK
+ } else {
   a_map_delete(o);
+#endif
+ }
 
  return o;
 }
  if (hint & A_HINT_DO) {
   a_map_store_root(o, o->op_ppaddr, hint);
   o->op_ppaddr = a_pp_deref;
- } else
+#if !A_HAS_OPFREEHOOK
+ } else {
   a_map_delete(o);
+#endif
+ }
 
  return o;
 }
  if (hint & A_HINT_DO && !(hint & A_HINT_STRICT)) {
   a_map_store_root(o, o->op_ppaddr, hint);
   o->op_ppaddr = new_pp;
- } else
+#if !A_HAS_OPFREEHOOK
+ } else {
   a_map_delete(o);
+#endif
+ }
 
  return o;
 }
 
  if (hint & A_HINT_DO) {
   a_map_store_root(o, 0, hint);
- } else
+#if !A_HAS_OPFREEHOOK
+ } else {
   a_map_delete(o);
+#endif
+ }
 
  return o;
 }
   } else {
    a_map_cancel(o);
   }
- } else
+#if !A_HAS_OPFREEHOOK
+ } else {
   a_map_delete(o);
+#endif
+ }
 
  return o;
 }
  ptable_seen_clear(seen);
 }
 
+/* ... Our op free hook (when available) ................................... */
+
+#if A_HAS_OPFREEHOOK
+
+STATIC void (*a_old_opfreehook)(pTHX_ OP *) = 0;
+
+STATIC void a_opfreehook(pTHX_ OP *o) {
+ a_map_delete(o);
+
+ if (a_old_opfreehook)
+  a_old_opfreehook(aTHX_ o);
+}
+
+#endif
+
 /* --- Interpreter setup/teardown ------------------------------------------ */
 
 STATIC U32 a_initialized = 0;
 #endif
  a_old_peep = 0;
 
+#if A_HAS_OPFREEHOOK
+ PL_opfreehook    = a_old_opfreehook;
+ a_old_opfreehook = 0;
+#endif
+
  a_initialized = 0;
 }
 
  PL_peepp   = a_peep;
 #endif
 
+#if A_HAS_OPFREEHOOK
+ a_old_opfreehook = PL_opfreehook;
+ PL_opfreehook    = a_opfreehook;
+#endif
+
 #if A_MULTIPLICITY
  call_atexit(a_teardown, aTHX);
 #else