blib/lib/PDL/AutoLoader.pm

filter: Statementstmt 58.8% 50 / 85 Branchbran 36.5% 19 / 52 Conditioncond 12.5% 2 / 16 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 77.7% 7 / 9 Podpod 25.0% 1 / 4 totaltotal 45.1% 79 / 175 CC 37 SCAR 28.3
CC
37
Cov
46%
reloader
30.0
expand_dir
30.0
expand_path
20.2
CRAP 17.0
« blib/lib/PDL/Demos/Transform_demo.pm ↑ Summary blib/lib/PDL/Image2D.pm »
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 an unknown
17 function C<func()> is called, PDL looks for a file called C<func.pdl>.
18 If it finds one, it compiles the file and calls the function C<func>.
19  
20 The list of directories to search in is given by the shell environment
21 variable C<PDLLIB>. This is a colon-separated list of directories. On
22 MSWindows systems, is it a I<semicolon> -separated list of directories.
23  
24 For example, in csh:
25  
26 setenv PDLLIB "/home/joe/pdllib:/local/pdllib"
27  
28 B<Note>: This variable is unrelated to Perl's C<PERL5LIB>.
29  
30 If you add a leading '+' on a directory name, PDL will search the
31 entire directory tree below that point. Internally, PDL stores the
32 directory list in the variable C<@PDLLIB>, which can be modified at
33 run time.
34  
35 For example, in csh:
36  
37 setenv PDLLIB "+/home/joe/PDL"
38  
39 will search /home/joe/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> or
45 C<pdl2> command 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> or C<pdl2> command line. Which means
50 in practice you can edit files, save changes and have C<perldl>
51 or C<pdl2> see the 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 PDL 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 several extensions by Craig DeForest (deforest@boulder.swri.edu)
76 All rights reserved. There is no warranty. You are allowed
77 to redistribute this software / documentation under certain
78 conditions. For details, see the file COPYING in the PDL
79 distribution. If this file is separated from the PDL distribution,
80 the copyright notice should be included in the file.
81  
82 =head1 BUGS
83  
84 No doubt this interface could be improved upon some more. :-)
85  
86 Will probably be quite slow if C<$PDL::AutoLoader::Rescan == 1>
87 and thousands of functions have been autoloaded.
88  
89 There could be a race condition in which the file changes
90 while the internal autoloader code is being executed but it
91 should be harmless.
92  
93 Probably has not been tested enough!
94  
95 =head1 SEE ALSO
96  
97 For an alternative approach to managing a personal collaction of
98 modules and functions, see L<local::lib>.
99  
100 =cut
101  
102 BEGIN{
103 1 ▶ if (defined $ENV{"PDLLIB"}) {
Subroutine BEGIN: called CC=4 SCAR=21.7
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{"PDLLIB"}) 0 1 1
104 0 ▶ if ( $^O eq 'MSWin32' ) { # win32 flavors
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 0 0
105 0 @PDLLIB = (".",split(';',$ENV{"PDLLIB"}));
106 0 s/"//g for @PDLLIB;
107 } else { # unixen systems
108 0 @PDLLIB = (".",split(':',$ENV{"PDLLIB"}));
109 }
110 0 @PDLLIB = grep length, @PDLLIB;
111 }
112 1 $PDL::AutoLoader::Rescan=0;
113 1 %PDL::AutoLoader::FileInfo = ();
114 }
115  
116 # Code to reload stuff if changed
117  
118 sub PDL::AutoLoader::reloader {
119 0 ▶ return unless $PDL::AutoLoader::Rescan;
Subroutine reloader: not called CC=5 SCAR=34.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless $PDL::AutoLoader::Rescan 0 0 0
120  
121 # Now check functions and reload if changed
122  
123 0 my ($file, $old_t);
124 0 for my $func (keys %PDL::AutoLoader::FileInfo) {
125 0 ($file, $old_t) = @{ $PDL::AutoLoader::FileInfo{$func} };
126 0 ▶ if ( (stat($file))[9]>$old_t ) { # Reload
Branch: 1 branch
BranchTrueFalseTotal
if ((stat $file)[9] > $old_t) 0 0 0
127 0 ▶ print "Reloading $file as file changed...\n" if $PDL::verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::verbose 0 0 0
128 0 &PDL::AutoLoader::autoloader_do($file);
129 0 $PDL::AutoLoader::FileInfo{$func} = [ $file, (stat($file))[9] ];
130 }
131 }
132 }
133  
134 # Used for Beta, and should probably be used generall in this mod
135 #use File::Spec;
136  
137 sub PDL::AutoLoader::import {
138  
139 # Beta folder support
140 # foreach (@INC) {
141 # $Beta_dir = File::Spec->catfile($_, 'PDL', 'Beta');
142 # push @PDLLIB, "+$Beta_dir" if -d $Beta_dir;
143 # }
144  
145 1 ▶my $pkg = (caller())[0];
Subroutine import: called CC=2 SCAR=7.5
146 1 my $toeval = "package $pkg;\n";
147  
148 # Make sure that the eval gets NiceSlice if we have it in this level
149 # (it's a drag that preprocessors aren't transitive...)
150 1 ▶$toeval .= "use PDL::NiceSlice;\n" if(defined $PDL::NiceSlice::VERSION);
Branch: 1 branch
BranchTrueFalseTotal
if defined $PDL::NiceSlice::VERSION 1 0 1
151  
152 1 $toeval .= <<'EOD';
153 $PDLLIB_CT = 0;
154  
155 push @PERLDL::AUTO, \&PDL::AutoLoader::reloader;
156  
157  
158 sub AUTOLOAD {
159 local @INC = @INC;
160 my @args = @_;
161 $AUTOLOAD =~ /::([^:]*)$/;
162 my $func = $1;
163  
164 # Trap spurious calls from 'use UnknownModule'
165  
166 goto &$AUTOLOAD if ord($func)==0;
167  
168 # Check if the PDLLIB needs to be expanded and, if so, expand it.
169 # This only updates when PDLLIB changes size, which should be OK
170 # for most things but doesn't catch new directories in expanded
171 # directory trees. It seems like an OK compromise between never
172 # catching anything and always thrashing through the directories.
173 if($PDLLIB_CT != scalar(@PDLLIB)) {
174 @PDLLIB_EXPANDED = PDL::AutoLoader::expand_path(@PDLLIB);
175 $PDLLIB_CT = scalar(@PDLLIB);
176 }
177  
178 print "Loading $func.pdl ..." if $PDL::verbose;
179 my $file;
180  
181 my $s = "PDL AutoLoader: Undefined subroutine $func() cannot be autoloaded.\n";
182  
183 for my $dir (@PDLLIB_EXPANDED) {
184 $file = $dir . "/" . "$func.pdl";
185 if (-e $file) {
186
187 print "found $file\n" if $PDL::verbose;
188  
189 &PDL::AutoLoader::autoloader_do($file);
190
191
192 # Remember autoloaded functions and do some reasonably
193 # smart cacheing of file/directory change times
194
195 if ($PDL::AutoLoader::Rescan) {
196 $PDL::AutoLoader::FileInfo{$func} = [ $file, (stat($file))[9] ];
197 }
198
199 # Now go to the autoload function
200 ##goto &$AUTOLOAD(@args) unless ($@ || !defined(&{$AUTOLOAD}));
201 return &$AUTOLOAD(@args) unless ($@ || !defined(&{$AUTOLOAD}));
202  
203 die $s."\tWhile parsing file `$file':\n$@\n" if($@);
204 die $s."\tFile `$file' doesn't \n\tdefine ${AUTOLOAD}().\n"
205
206 }
207 }
208  
209 die $s."\tNo file `$func.pdl' was found in your \@PDLLIB path.\n";
210 }
211  
212 EOD
213  
214 1 ▶eval $toeval;
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine AUTOLOAD: called CC=11 SCAR=27.2
Branch: 8 branches
BranchTrueFalseTotal
if ord $func == 0 0 1 1
if ($PDLLIB_CT != scalar @PDLLIB) 1 0 1
if $PDL::verbose 0 1 1
if (-e $file) 1 0 1
if $PDL::verbose 0 1 1
if ($PDL::AutoLoader::Rescan) 0 1 1
unless $@ or not defined &{$AUTOLOAD;} 1 0 1
if $@ 0 0 0
Condition33%1/3
$@ or not defined &{$AUTOLOAD;}
l!l&&r!l&&!r
001
MC/DC0%0/2
$@ or not defined &{$AUTOLOAD;}
$@ not defined &{$AUTOLOAD;}
Truth table33%1/3
A or not(B)
A= $@B= not defined &{$AUTOLOAD;}
ABresult
1X1
011
000
215  
216 }
217  
218  
219 # Simple 'do' doesn't work with preprocessing -- this replaces
220 # "do file" and sticks NiceSlice in manually if it's needed (yuck).
221  
222 sub PDL::AutoLoader::autoloader_do {
223 1 ▶ my ($file) = shift;
Subroutine autoloader_do: called CC=5 SCAR=18.7
Pod undocumented
224
225 1 ▶ if(defined($PDL::NiceSlice::VERSION)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $PDL::NiceSlice::VERSION) { } 1 0 1
226
227 1 ▶ print "AutoLoader: NiceSlice enabled...\n" if($PDL::debug);
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::debug 1 0 1
228
229 1 ▶ if(open(AUTOLOAD_FILE,"<$file")) {
Branch: 1 branch
BranchTrueFalseTotal
if (open AUTOLOAD_FILE, "<$file") 1 0 1
230 1 my($script) = &PDL::NiceSlice::perldlpp("PDL::NiceSlice", join("",<AUTOLOAD_FILE>));
231 1 ▶ eval $script;
Subroutine func: called CC=1 SCAR=0.0
232 }
233 } else {
234 0 ▶ print "AutoLoader: no NiceSlice...\n" if($PDL::debug);
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::debug 0 0 0
235 0 do $file;
236 }
237 }
238  
239  
240 # Expand directories recursively...
241 sub PDL::AutoLoader::expand_dir {
242 0 ▶ local $d;
Subroutine expand_dir: not called CC=5 SCAR=34.0
Pod undocumented
243 0 local @list;
244 0 local @subdirs;
245  
246 0 local $dir = shift;
247
248 0 ▶ if(! -d $dir) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 0 0 0
249 0 push(@list,$dir);
250  
251 0 opendir(FOO,$dir);
252  
253 0 ▶ @subdirs = grep((!m/^\./ && ($_="$dir/$_") && (-d $_)), readdir(FOO));
Condition0%0/5
!/^\./ && ($_ = "$dir/$_") && -d $_
!ll&&!rl&&r
000
not /^\./ and $_ = "$dir/$_"
!ll
00
MC/DC0%0/2
!/^\./ && ($_ = "$dir/$_") && -d $_
not /^\./ -d $_
Truth table0%0/3
not(A) && B
A= not /^\./B= -d $_
ABresult
0X0
100
111
254 0 closedir FOO;
255  
256 0 ▶ while(defined ($d = shift @subdirs)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($d = shift @subdirs)) 0 0 0
257 0 push(@list,&PDL::AutoLoader::expand_dir($d));
258 }
259 0 return @list;
260 }
261  
262  
263 =head2 PDL::AutoLoader::expand_path
264  
265 =for ref
266  
267 Expand a compactified path into a dir list
268  
269 You supply a pathlist and leading '+' and '~' characters get expanded into
270 full directories. Normally you don't want to use this -- it's internal to the
271 autoloader -- but some utilities, like the online documentation searcher, need
272 to be able to use it.
273  
274 =cut
275  
276 sub PDL::AutoLoader::expand_path {
277 2 ▶ my @PDLLIB = @_;
Subroutine expand_path: called CC=11 SCAR=30.1
Pod documented
278 2 my @PDLLIB_EXPANDED;
279
280 2 ▶ print "AutoLoader: Expanding directories from ".join(':',@PDLLIB)."...\n"
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::debug 2 0 2
281 if($PDL::debug);
282 2 local $_;
283 2 foreach $_(@PDLLIB) {
284 # Expand ~{name} and ~ conventions.
285 2 ▶ if(s/^(\+?)\~(\+||[a-zA-Z0-9]*)//) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/^(\+?)\~(\+||[a-zA-Z0-9]*)//) 1 1 2
286 1 ▶ if($2 eq '+') {
Branch: 2 branches
BranchTrueFalseTotal
if ($2 eq '+') { } 0 1 1
elsif (not $2) { } 1 0 1
287 # Expand shell '+' to CWD.
288 0 ▶ $_= $1 . ($ENV{'PWD'} || '.');
Condition0%0/2
$ENV{'PWD'} || '.'
l!l
00
MC/DC0%0/1
$ENV{'PWD'} || '.'
$ENV{'PWD'}
Truth table0%0/2
A
A= $ENV{'PWD'}
Aresult
11
00
289 } elsif(!$2) {
290 # No name mentioned -- use current user.
291 # Ideally would use File::HomeDir->my_home() here
292 1 ▶ $_ = $1 . ( $ENV{'HOME'} || (( getpwnam( getlogin || getpwuid($<) ))[7]) ) . $_;
Condition16%1/6
$ENV{'HOME'} || (getpwnam(getlogin || getpwuid $<))[7]
l!l&&r!l&&!r
100
getlogin || getpwuid $<
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{'HOME'} || (getpwnam(getlogin || getpwuid $<))[7]
$ENV{'HOME'} (getpwnam(getlogin || getpwuid $<))[7]
MC/DC0%0/2
getlogin || getpwuid $<
getlogin getpwuid $<
Truth table33%1/3
A || B
A= $ENV{'HOME'}B= (getpwnam(getlogin || getpwuid $<))[7]
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= getloginB= getpwuid $<
ABresult
1X1
011
000
293 } else {
294 # Name mentioned - try to get that user's home directory.
295 0 $_ = $1 . ( (getpwnam($2))[7] ) . $_;
296 }
297 }
298
299 # If there's a leading '+', include all subdirs too.
300 2 ▶ push(@PDLLIB_EXPANDED,
Branch: 1 branch
BranchTrueFalseTotal
s/^\+// ? : 0 2 2
301 s/^\+// ? &PDL::AutoLoader::expand_dir($_) : $_
302 );
303 }
304  
305 2 ▶ print "AutoLoader: returning ",join(",",@PDLLIB_EXPANDED),"\n" if($PDL::debug);
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::debug 2 0 2
306 2 @PDLLIB_EXPANDED;
307 }
308  
309  
310 ;# Exit with OK status
311  
312 1 1;
« blib/lib/PDL/Demos/Transform_demo.pm ↑ Summary blib/lib/PDL/Image2D.pm »