4
PDL::AutoLoader - MatLab style AutoLoader for PDL
9
$a = func1(...); # Load file func1.pdl
10
$b = func2(...); # Load file func2.pdl
12
$PDL::AutoLoader::Rescan = 1; # Enable re-scanning
16
This module implements a MatLab style AutoLoader for PDL. If a unknown
17
function 'func()' is called then a file 'func.pdl' is searched for and
18
if found is read in to define 'func()' which is then executed.
20
Files are seached for using the directories in seach path C<@PDLLIB>, which
21
is initialised from the shell environment variable C<PDLLIB> which is a
22
colon seperated list of directories.
26
setenv PDLLIB "/home/kgb/pdllib:/local/pdllib"
28
Note this is kept seperate from C<PERL5LIB> just in case.
30
As an added bonus, you can use a leading '+' on a directory name to
31
search not just that directory but the entire directory tree under it
32
(excluding symlinks). The subdirs are determined by explicit search,
33
and searches occur at startup and again each time you change the number
34
of elements in @PDLLIB.
37
setenv PDLLIB "+~kgb/PDL"
39
will search /home/kgb/PDL and all its subdirectories for .pdl files.
43
The variable C<$PDL::AutoLoader::Rescan> controls whether files
44
are automatically re-scanned for changes at the C<perldl> command
47
If C<$PDL::AutoLoader::Rescan == 1> and the file is changed
48
then the new definition is reloaded auto-matically before
49
executing the C<perldl> command line. Which means in practice
50
you can edit files, save changes and have C<perldl> see the
51
changes automatically.
53
The default is '0' - i.e. to have this feature disabled.
55
As this feature is only pertinent to the C<perldl> shell it imposes
56
no overhead on PDL scripts. Yes Bob you can have your cake and
59
Note: files are only re-evaled if they are determined to have
60
been changed according to their date/time stamp.
62
No doubt this interface could be improved upon some more. :-)
66
sub foo { # file 'foo.pdl' - define the 'foo' function
68
return sqrt($x**2 + $x**3 + 2);
70
1; # File returns true (i.e. loaded successfully)
74
Copyright(C) 1997 Karl Glazebrook (kgb@aaoepp.aao.gov.au).
75
All rights reserved. There is no warranty. You are allowed
76
to redistribute this software / documentation under certain
77
conditions. For details, see the file COPYING in the PDL
78
distribution. If this file is separated from the PDL distribution,
79
the copyright notice should be included in the file.
83
No doubt this interface could be improved upon some more. :-)
85
Will probably be quite slow if C<$PDL::AutoLoader::Rescan == 1>
86
and thousands of functions have been autoloaded.
88
There could be a race condition in which the file changes
89
while the internal autoloader code is being executed but it
92
Probably has not been tested enough!
97
@PDLLIB = (".",split(':',$ENV{"PDLLIB"})) if defined $ENV{"PDLLIB"};
98
$PDL::AutoLoader::Rescan=0;
99
%PDL::AutoLoader::FileInfo = ();
102
# Code to reload stuff if changed
104
sub PDL::AutoLoader::reloader {
105
return unless $PDL::AutoLoader::Rescan;
107
# Now check functions and reload if changed
110
for my $func (keys %PDL::AutoLoader::FileInfo) {
111
($file, $old_t) = @{ $PDL::AutoLoader::FileInfo{$func} };
112
if ( (stat($file))[9]>$old_t ) { # Reload
113
print "Reloading $file as file changed...\n" if $PDL::verbose;
114
&PDL::AutoLoader::autoloader_do($file);
115
$PDL::AutoLoader::FileInfo{$func} = [ $file, (stat($file))[9] ];
120
sub PDL::AutoLoader::import {
122
my $pkg = (caller())[0];
123
my $toeval = "package $pkg;\n";
125
# Make sure that the eval gets NiceSlice if we have it in this level
126
# (it's a drag that preprocessors aren't transitive...)
127
$toeval .= "use PDL::NiceSlice;\n" if(defined $PDL::NiceSlice::VERSION);
132
push @PERLDL::AUTO, \&PDL::AutoLoader::reloader;
136
$AUTOLOAD =~ /::([^:]*)$/;
139
# Trap spurious calls from 'use UnknownModule'
141
goto &$AUTOLOAD if ord($func)==0;
143
# Check if the PDLLIB needs to be expanded and, if so, expand it.
144
# This only updates when PDLLIB changes size, which should be OK
145
# for most things but doesn't catch new directories in expanded
146
# directory trees. It seems like an OK compromise between never
147
# catching anything and always thrashing through the directories.
149
if($PDLLIB_CT != scalar(@PDLLIB)) {
150
print "Expanding directories from ".join(':',@PDLLIB)."...\n"
153
$PDLLIB_CT = scalar(@PDLLIB);
154
foreach $_(@PDLLIB) {
155
# Expand ~{name} and ~ conventions
156
s/^(\+?)~([a-zA-Z0-9]*)// &&
157
($_ = $1.((getpwnam($2 || getlogin))[7]).$_ );
159
# If there's a leading '+', include all subdirs too.
160
push(@PDLLIB_EXPANDED,
161
s/^\+// ? &PDL::AutoLoader::expand_dir($_) : $_
167
print "Loading $func.pdl...\n" if $PDL::verbose;
168
for my $dir (@PDLLIB_EXPANDED) {
169
my $file = $dir . "/" . "$func.pdl";
172
PDL::AutoLoader::autoloader_do($file);
173
# Remember autoloaded functions and do some reasonably
174
# smart cacheing of file/directory change times
176
if ($PDL::AutoLoader::Rescan) {
177
$PDL::AutoLoader::FileInfo{$func} = [ $file, (stat($file))[9] ];
180
# Now go to the autoloaed function
182
goto &$AUTOLOAD unless $@;
185
die "PDL autoloader: Undefined subroutine $func() cannot be AutoLoaded\n";
195
# Simple 'do' doesn't work with preprocessing -- this replaces
196
# "do file" and sticks NiceSlice in manually if it's needed (yuck).
198
sub PDL::AutoLoader::autoloader_do {
201
if(defined($PDL::NiceSlice::VERSION)) {
203
print "AutoLoader: NiceSlice enabled...\n" if($PDL::debug);
205
if(open(AUTOLOAD_FILE,"<$file")) {
206
my($script) = &PDL::NiceSlice::perldlpp(join("",<AUTOLOAD_FILE>));
210
print "AutoLoader: no NiceSlice...\n" if($PDL::debug);
216
# Expand directories recursively...
217
sub PDL::AutoLoader::expand_dir {
224
if(! -d $dir) { return undef; }
229
@subdirs = grep((!m/^\./ && ($_="$dir/$_") && (-d $_)), readdir(FOO));
232
while(defined ($d = shift @subdirs)) {
233
push(@list,&PDL::AutoLoader::expand_dir($d));
239
;# Exit with OK status