@@ -6969,6 +6969,55 @@ require executes at all.
6969
6969
6970
6970
As of 5.37.7 C<@INC> values of undef will be silently ignored.
6971
6971
6972
+ The function C<require()> is difficult to wrap properly. Many modules consult
6973
+ the stack to find information about their caller, and injecting a new stack
6974
+ frame by wrapping C<require()> often breaks things. Nevertheless it can be
6975
+ very helpful to have the ability to perform actions before and after a
6976
+ C<require>, for instance for trace utilities like C<Devel::TraceUse> or to
6977
+ measure time to load and memory consumption of the require graph. Because of
6978
+ the difficulties in safely creating a C<require()> wrapper in 5.37.7 we
6979
+ introduced a new mechanism.
6980
+
6981
+ As of 5.37.7, prior to any other actions it performs, C<require> will check if
6982
+ C<$SIG{__REQUIRE__}> contains a coderef, and if it does it will be called with
6983
+ the filename form of the item being loaded. This function call is informative
6984
+ only, and will not affect the require process except by throwing a fatal
6985
+ exception, which will be treated as though the required code itself had thrown
6986
+ an exception. However the function may return a code reference, which will be
6987
+ executed in an eval with no arguments after the require completes, regardless
6988
+ of how the compilation completed even if the require throws a fatal exception.
6989
+ The function must consult C<%INC> to determine if the require failed or not.
6990
+ For instance the following code will print some diagnostics before and after
6991
+ every C<require> statement. The example also includes logic to chain the
6992
+ signal, so that multiple signals can cooperate. Well behaved
6993
+ C<$SIG{__REQUIRE__}> handlers should always take this into account.
6994
+
6995
+ {
6996
+ my $old_hook = $SIG{__REQUIRE__};
6997
+ local $SIG{__REQUIRE__} = sub {
6998
+ my ($name) = @_;
6999
+ my $old_hook_ret;
7000
+ $old_hook_ret = $old_hook->($name) if $old_hook;
7001
+ warn "Requiring: $name\n";
7002
+ return sub {
7003
+ $old_hook_ret->() if $old_hook_ret;
7004
+ warn sprintf "Finished requiring %s: %s\n",
7005
+ $name, $INC{$name} ? "loaded" :"failed";
7006
+ };
7007
+ };
7008
+ require Whatever;
7009
+ }
7010
+
7011
+ This hook executes for ALL C<require> statements, unlike C<INC> and C<INCDIR>
7012
+ hooks, which are only executed for relative file names, and it executes first
7013
+ before any other special behaviour inside of require. Note that the initial hook
7014
+ in C<$SIG{__REQUIRE__}> is *not* executed inside of an eval, and throwing an
7015
+ exception will stop further processing, but the after hook it may return is
7016
+ executed inside of an eval, and any exceptions it throws will be silently ignored.
7017
+ This is because it executes inside of the scope cleanup logic that is triggered
7018
+ after the require completes, and an exception at this time would not stop the
7019
+ module from being loaded, etc.
7020
+
6972
7021
For a yet-more-powerful import facility, see
6973
7022
L<C<use>|/use Module VERSION LIST> and L<perlmod>.
6974
7023
0 commit comments