Skip to content

Commit 10ba1af

Browse files
committed
pp_ctl.c - support $INC in require_file @inc hook
$INC is localized to be the C level index of the loop over the @inc array. At the end of the hook its value is assigned back to the C level loop iterator (inc_idx). This allows a hook to control where in the @inc array the loop should continue, for instance -1 represents "reprocess from the beginning" (and as a convenience so does undef). This can be useful if the @inc array is modified by a hook. Normally we would just "continue along", but this may or may not be the right thing to do, so we let the user decide.
1 parent 901d80a commit 10ba1af

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

pp_ctl.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4307,6 +4307,9 @@ S_require_file(pTHX_ SV *sv)
43074307
SvSetSV_nosteal(nsv,sv);
43084308
}
43094309

4310+
SV * inc_idx_sv = save_scalar(PL_incgv);
4311+
sv_setiv(inc_idx_sv,inc_idx);
4312+
43104313
ENTER_with_name("call_INC_hook");
43114314
SAVETMPS;
43124315
EXTEND(SP, 2);
@@ -4412,6 +4415,21 @@ S_require_file(pTHX_ SV *sv)
44124415
/* FREETMPS may free our filter_cache */
44134416
SvREFCNT_inc_simple_void(filter_cache);
44144417

4418+
/*
4419+
Let the hook override which @INC entry we visit
4420+
next by setting $INC to a different value than it
4421+
was before we called the hook. If they have
4422+
completely rewritten the array they might want us
4423+
to start traversing from the beginning, which is
4424+
represented by -1. We use undef as an equivalent of
4425+
-1. This can't be used as a way to call a hook
4426+
twice, as we still dedupe.
4427+
We have to do this before we LEAVE, as we localized
4428+
$INC before we called the hook.
4429+
*/
4430+
inc_idx_sv = GvSVn(PL_incgv);
4431+
inc_idx = SvOK(inc_idx_sv) ? SvIV(inc_idx_sv) : -1;
4432+
44154433
PUTBACK;
44164434
FREETMPS;
44174435
LEAVE_with_name("call_INC_hook");

t/op/require_errors.t

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,6 @@ like $@, qr/^Can't locate \Q$nonsearch\E at/,
330330
{ }, 'INC hooks that overwrite @INC continue as expected (skips a and z)');
331331
}
332332
{
333-
local $::TODO = "Pending new feature \$INC";
334333
# as of 5.37.7
335334
fresh_perl_like(
336335
'@INC = (sub { @INC=qw(a b); undef $INC }, "z"); '

0 commit comments

Comments
 (0)