@@ -3731,16 +3731,25 @@ S_refkids(pTHX_ OP *o, I32 type)
37313731}
37323732
37333733
3734- /* Apply reference (autovivification) context to the subtree at o.
3735- * For example in
3736- * push @{expression}, ....;
3737- * o will be the head of 'expression' and type will be OP_RV2AV.
3738- * It marks the op o (or a suitable child) as autovivifying, e.g. by
3739- * setting OPf_MOD.
3740- * For OP_RV2AV/OP_PADAV and OP_RV2HV/OP_PADHV sets OPf_REF too if
3741- * set_op_ref is true.
3734+ /* doref(): apply reference autovivification context (and scalar and
3735+ * lvalue context) to a subtree. For example, in:
3736+ *
3737+ * @{expression} = ...;
3738+ *
3739+ * the expression is expected to return an AV ref. If the expression
3740+ * is (for example) $h{foo}, then the OP_HELEM op associated with the
3741+ * expression needs to be flagged with:
3742+ * - OPf_MOD to indicate that it should autovivify if the element
3743+ * doesn't exist
3744+ * - OPpDEREF_AV to indicate that the autovivified return value should
3745+ * be [] rather than undef.
3746+ *
3747+ * The 'o' parameter is the head of the expression and 'type' is the
3748+ * context to apply (OP_RV2AV in the example above).
3749+ *
3750+ * If 'set_op_ref' is true, it also sets the OPf_REF flag on OP_RV2[AH]V
3751+ * and OP_PAD[AH]V ops
37423752 *
3743- * Also calls scalar(o).
37443753 */
37453754
37463755OP *
@@ -3753,11 +3762,16 @@ Perl_doref(pTHX_ OP *o, I32 type, bool set_op_ref)
37533762 if (PL_parser && PL_parser->error_count)
37543763 return o;
37553764
3765+ /* iterate down the tree */
3766+
37563767 while (1) {
37573768 switch (o->op_type) {
37583769 case OP_ENTERSUB:
37593770 if ((type == OP_EXISTS || type == OP_DEFINED) &&
3760- !(o->op_flags & OPf_STACKED)) {
3771+ !(o->op_flags & OPf_STACKED))
3772+ {
3773+ /* 'defined &foo' etc: downgrade from a func call
3774+ * to just a special CV retrieval */
37613775 OpTYPE_set(o, OP_RV2CV); /* entersub => rv2cv */
37623776 assert(cUNOPo->op_first->op_type == OP_NULL);
37633777 /* disable pushmark */
@@ -3796,6 +3810,7 @@ Perl_doref(pTHX_ OP *o, I32 type, bool set_op_ref)
37963810 o->op_flags |= OPf_MOD;
37973811 }
37983812 if (o->op_flags & OPf_KIDS && o->op_type != OP_ENTERSUB) {
3813+ /* propagate the context of *this* op to its children */
37993814 type = o->op_type;
38003815 o = cUNOPo->op_first;
38013816 continue;
0 commit comments