develooper Front page | perl.perl5.porters | Postings from September 2001

[PATCH] Allow an @INC hook to set %INC

Thread Next
Rafael Garcia-Suarez
September 28, 2001 13:22
[PATCH] Allow an @INC hook to set %INC
Message ID:
In the process of getting something useful from this coderef-in-@INC feature...
here's a patch.

It allows an hook placed in @INC to set the entry in %INC for the files it has loaded.

For example, the hook may do something like this :

	push @INC, sub {
	    my $file = $_[1];
	    # ...figure out some $path...
	    open my $fh, "$path/$file" or return undef;
	    $INC{$file} = "$path/$file";
            return $fh;

And, with such a clever hook, dynaloaded modules (and other modules that use %INC)
will work seamlessly.

Sidenote: I'm proposing this patch because I'm writing an experimental pragma,
ex::newest. When 'use ex::newest;' in put in a program, this modifies the
behavior of use and require so that newest versions of modules are loaded
(for modules that are installed several times locally) : @INC is walked for
multiple occurences of the module to find the newest $VERSION. If this patch
is useful for my funny pragma, it may be useful for other programmers.

--- pp_ctl.c.orig	Wed Sep 26 15:59:55 2001
+++ pp_ctl.c	Fri Sep 28 21:50:06 2001
@@ -3323,10 +3323,14 @@
     /* Assume success here to prevent recursive requirement. */
-    (void)hv_store(GvHVn(PL_incgv), name, strlen(name),
-		   (hook_sv ? SvREFCNT_inc(hook_sv)
-			    : newSVpv(CopFILE(&PL_compiling), 0)),
-		   0 );
+    len = strlen(name);
+    /* Check whether a hook in @INC has already filled %INC */
+    if (!hook_sv || !(svp = hv_fetch(GvHVn(PL_incgv), name, len, 0))) {
+	(void)hv_store(GvHVn(PL_incgv), name, len,
+		       (hook_sv ? SvREFCNT_inc(hook_sv)
+				: newSVpv(CopFILE(&PL_compiling), 0)),
+		       0 );
+    }

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About