# $Id: autoload.pl,v 1.4 91/07/15 00:20:57 tchrist Exp Locker: tchrist $


sub swapnload {
    local($__fun__,$__file__) = @_;
    local($__tmp__) = substr($_stub{$__fun__}, 7*4, 4);   
    substr($_stub{$__fun__}, 7*4, 4) = substr($_main{$__fun__}, 7*4, 4);
    substr($_main{$__fun__}, 7*4, 4) = $__tmp__;
    do "$__file__" || 
	die("autoloading of $__fun__ failed!  error was $__file__:\n\t".($@?$@:$!));
} 

sub autoload {
    local($__fun__, $__file__, $__i__);
    local($__debug__) = $Debug || defined $ENV{'debug_load'};
    for ($__i__ = 0; $__i__ < @_; $__i__ += 2) {
	($__fun__, $__file__) = @_[$__i__..$__i__+1];
	next unless $__fun__;
	$__file__ = "$__fun__.pl" unless $__file__;
	print STDERR "stubbing $__fun__ from $__file__\n" if $__debug__;
	eval <<EOCODE;
	    sub $__fun__ { 
		print STDERR "autoloading $__fun__ from $__file__...\n"
		    if $__debug__;
		local(*stub'$__fun__); 
		&swapnload("$__fun__", "$__file__"); 
		&$__fun__; 
	}
EOCODE
	die "autoload failure creating stub for $__fun__: $@" if $@;
    } 
} 


1;
