~ubuntu-branches/ubuntu/lucid/pdl/lucid

« back to all changes in this revision

Viewing changes to Basic/AutoLoader.pm

  • Committer: Bazaar Package Importer
  • Author(s): Ben Gertzfield
  • Date: 2002-04-08 18:47:16 UTC
  • Revision ID: james.westby@ubuntu.com-20020408184716-0hf64dc96kin3htp
Tags: upstream-2.3.2
ImportĀ upstreamĀ versionĀ 2.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
=head1 NAME
 
3
 
 
4
PDL::AutoLoader - MatLab style AutoLoader for PDL
 
5
 
 
6
=head1 SYNOPSIS
 
7
 
 
8
 use PDL::AutoLoader;
 
9
 $a = func1(...);   # Load file func1.pdl
 
10
 $b = func2(...);   # Load file func2.pdl
 
11
 
 
12
 $PDL::AutoLoader::Rescan = 1; # Enable re-scanning
 
13
 
 
14
=head1 DESCRIPTION
 
15
 
 
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.
 
19
 
 
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.
 
23
 
 
24
e.g. in csh
 
25
 
 
26
 setenv PDLLIB "/home/kgb/pdllib:/local/pdllib"
 
27
 
 
28
Note this is kept seperate from C<PERL5LIB> just in case.
 
29
 
 
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.  
 
35
 
 
36
For example,
 
37
  setenv PDLLIB "+~kgb/PDL"
 
38
 
 
39
will search /home/kgb/PDL and all its subdirectories for .pdl files.
 
40
 
 
41
=head2 AUTO-SCANNING
 
42
 
 
43
The variable C<$PDL::AutoLoader::Rescan> controls whether files
 
44
are automatically re-scanned for changes at the C<perldl> command
 
45
line.
 
46
 
 
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.
 
52
 
 
53
The default is '0' - i.e. to have this feature disabled.
 
54
 
 
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
 
57
eat it too!
 
58
 
 
59
Note: files are only re-evaled if they are determined to have
 
60
been changed according to their date/time stamp.
 
61
 
 
62
No doubt this interface could be improved upon some more. :-)
 
63
 
 
64
=head2 Sample file:
 
65
 
 
66
 sub foo { # file 'foo.pdl' - define the 'foo' function
 
67
   my $x=shift;
 
68
   return sqrt($x**2 + $x**3 + 2);
 
69
 }
 
70
 1; # File returns true (i.e. loaded successfully)
 
71
 
 
72
=head1 AUTHOR
 
73
 
 
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.
 
80
 
 
81
=head1 BUGS
 
82
 
 
83
No doubt this interface could be improved upon some more. :-)
 
84
 
 
85
Will probably be quite slow if C<$PDL::AutoLoader::Rescan == 1>
 
86
and thousands of functions have been autoloaded.
 
87
 
 
88
There could be a race condition in which the file changes
 
89
while the internal autoloader code is being executed but it
 
90
should be harmless.
 
91
 
 
92
Probably has not been tested enough!
 
93
 
 
94
=cut
 
95
 
 
96
BEGIN{
 
97
  @PDLLIB = (".",split(':',$ENV{"PDLLIB"})) if defined $ENV{"PDLLIB"};
 
98
  $PDL::AutoLoader::Rescan=0;
 
99
  %PDL::AutoLoader::FileInfo = ();
 
100
}
 
101
 
 
102
# Code to reload stuff if changed
 
103
 
 
104
sub PDL::AutoLoader::reloader {
 
105
   return unless $PDL::AutoLoader::Rescan;
 
106
 
 
107
   # Now check functions and reload if changed
 
108
 
 
109
   my ($file, $old_t);
 
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] ];
 
116
       }
 
117
   }
 
118
}
 
119
 
 
120
sub PDL::AutoLoader::import {
 
121
 
 
122
my $pkg = (caller())[0];
 
123
my $toeval = "package $pkg;\n";
 
124
 
 
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);
 
128
 
 
129
$toeval .= <<'EOD';
 
130
$PDLLIB_CT = 0;
 
131
 
 
132
push @PERLDL::AUTO, \&PDL::AutoLoader::reloader;
 
133
 
 
134
sub AUTOLOAD {
 
135
    local @INC = @INC;
 
136
    $AUTOLOAD =~ /::([^:]*)$/;
 
137
    my $func = $1;
 
138
 
 
139
    # Trap spurious calls from 'use UnknownModule'
 
140
 
 
141
    goto &$AUTOLOAD if ord($func)==0;
 
142
 
 
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.
 
148
 
 
149
   if($PDLLIB_CT != scalar(@PDLLIB)) {
 
150
     print "Expanding directories from ".join(':',@PDLLIB)."...\n"
 
151
       if($PDL::verbose);
 
152
     local $_;
 
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]).$_ );
 
158
       
 
159
       # If there's a leading '+', include all subdirs too.
 
160
       push(@PDLLIB_EXPANDED,
 
161
           s/^\+// ? &PDL::AutoLoader::expand_dir($_) : $_
 
162
           );
 
163
     }
 
164
   }
 
165
 
 
166
 
 
167
    print "Loading $func.pdl...\n" if $PDL::verbose;
 
168
    for my $dir (@PDLLIB_EXPANDED) {
 
169
        my $file = $dir . "/" . "$func.pdl";
 
170
        if (-e $file) {
 
171
          
 
172
          PDL::AutoLoader::autoloader_do($file);
 
173
           # Remember autoloaded functions and do some reasonably
 
174
           # smart cacheing of file/directory change times
 
175
 
 
176
           if ($PDL::AutoLoader::Rescan) {
 
177
              $PDL::AutoLoader::FileInfo{$func} = [ $file, (stat($file))[9] ];
 
178
           }
 
179
 
 
180
           # Now go to the autoloaed function
 
181
 
 
182
           goto &$AUTOLOAD unless $@;
 
183
        }
 
184
    }
 
185
    die "PDL autoloader: Undefined subroutine $func() cannot be AutoLoaded\n";
 
186
}
 
187
 
 
188
EOD
 
189
 
 
190
eval $toeval;
 
191
 
 
192
}
 
193
 
 
194
 
 
195
# Simple 'do' doesn't work with preprocessing -- this replaces
 
196
# "do file" and sticks NiceSlice in manually if it's needed (yuck).
 
197
 
 
198
sub PDL::AutoLoader::autoloader_do {
 
199
  my ($file) = shift;
 
200
  
 
201
  if(defined($PDL::NiceSlice::VERSION)) {
 
202
    
 
203
    print "AutoLoader: NiceSlice enabled...\n" if($PDL::debug);
 
204
    
 
205
    if(open(AUTOLOAD_FILE,"<$file")) {
 
206
      my($script) = &PDL::NiceSlice::perldlpp(join("",<AUTOLOAD_FILE>));
 
207
      eval $script;
 
208
    }
 
209
  } else {
 
210
    print "AutoLoader: no NiceSlice...\n" if($PDL::debug);
 
211
    do $file;
 
212
  }
 
213
}
 
214
 
 
215
 
 
216
# Expand directories recursively...
 
217
sub PDL::AutoLoader::expand_dir {
 
218
  local $d;  
 
219
  local @list;  
 
220
  local @subdirs;  
 
221
 
 
222
  local $dir = shift;
 
223
    
 
224
  if(! -d $dir) { return undef; }
 
225
  push(@list,$dir);
 
226
 
 
227
  opendir(FOO,$dir);
 
228
 
 
229
  @subdirs = grep((!m/^\./ && ($_="$dir/$_") && (-d $_)), readdir(FOO));
 
230
  closedir FOO;
 
231
 
 
232
  while(defined ($d = shift @subdirs)) {
 
233
    push(@list,&PDL::AutoLoader::expand_dir($d));
 
234
  }
 
235
  return @list;
 
236
}
 
237
 
 
238
 
 
239
;# Exit with OK status
 
240
 
 
241
1;