blib/lib/PDL/AutoLoader.pm

filter: Statementstmt 72.3% 68 / 94 Branchbran 38.0% 19 / 50 Conditioncond 12.5% 2 / 16 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 85.7% 12 / 14 Podpod 25.0% 1 / 4 totaltotal 54.5% 102 / 187 CC 36 SCAR 26.3
CC
36
Cov
56%
reloader
30.0
expand_path
20.2
expand_dir
20.0
CRAP 13.9
« blib/lib/PDL/Reduce.pm ↑ Summary blib/lib/PDL/Demos.pm »
1 =head1 NAME
2  
3 PDL::AutoLoader - MatLab style AutoLoader for PDL
4  
5 =head1 SYNOPSIS
6  
7 use PDL::AutoLoader;
8 $x = func1(...); # Load file func1.pdl
9 $y = func2(...); # Load file func2.pdl
10  
11 $PDL::AutoLoader::Rescan = 1; # Enable re-scanning
12  
13 =head1 DESCRIPTION
14  
15 This module implements a MatLab style AutoLoader for PDL. If an unknown
16 function C<func()> is called, PDL looks for a file called C<func.pdl>.
17 If it finds one, it compiles the file and calls the function C<func>.
18  
19 The list of directories to search in is given by the shell environment
20 variable C<PDLLIB>. This is a colon-separated list of directories. On
21 MSWindows systems, is it a I<semicolon> -separated list of directories.
22  
23 For example, in csh:
24  
25 setenv PDLLIB "/home/joe/pdllib:/local/pdllib"
26  
27 B<Note>: This variable is unrelated to Perl's C<PERL5LIB>.
28  
29 If you add a leading '+' on a directory name, PDL will search the
30 entire directory tree below that point. Internally, PDL stores the
31 directory list in the variable C<@PDLLIB>, which can be modified at
32 run time.
33  
34 For example, in csh:
35  
36 setenv PDLLIB "+/home/joe/PDL"
37  
38 will search /home/joe/PDL and all its subdirectories for .pdl files.
39  
40 =head2 AUTO-SCANNING
41  
42 The variable C<$PDL::AutoLoader::Rescan> controls whether files
43 are automatically re-scanned for changes at the C<perldl>
44 command line.
45  
46 If C<$PDL::AutoLoader::Rescan == 1> and the file is changed
47 then the new definition is reloaded auto-matically before
48 executing the C<perldl> command line. Which means
49 in practice you can edit files, save changes and have C<perldl>
50 see the changes automatically.
51  
52 The default is '0' - i.e. to have this feature disabled.
53  
54 As this feature is only pertinent to the PDL shell it imposes
55 no overhead on PDL scripts. Yes Bob you can have your cake and
56 eat it too!
57  
58 Note: files are only re-evaled if they are determined to have
59 been changed according to their date/time stamp.
60  
61 No doubt this interface could be improved upon some more. :-)
62  
63 =head2 Sample file:
64  
65 sub foo { # file 'foo.pdl' - define the 'foo' function
66 my $x=shift;
67 return sqrt($x**2 + $x**3 + 2);
68 }
69 1; # File returns true (i.e. loaded successfully)
70  
71 =head1 AUTHOR
72  
73 Copyright(C) 1997 Karl Glazebrook (kgb@aaoepp.aao.gov.au);
74 several extensions by Craig DeForest (deforest@boulder.swri.edu)
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 =head1 SEE ALSO
95  
96 For an alternative approach to managing a personal collaction of
97 modules and functions, see L<local::lib>.
98  
99 =cut
100  
101 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
102 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
103  
104 1 our @PDLLIB;
105 1 ▶BEGIN{
Subroutine BEGIN: called CC=4 SCAR=21.7
106 1 ▶ if (defined $ENV{"PDLLIB"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{"PDLLIB"}) 0 1 1
107 0 ▶ if ( $^O eq 'MSWin32' ) { # win32 flavors
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 0 0
108 0 @PDLLIB = (".",split(';',$ENV{"PDLLIB"}));
109 0 s/"//g for @PDLLIB;
110 } else { # unixen systems
111 0 @PDLLIB = (".",split(':',$ENV{"PDLLIB"}));
112 }
113 0 @PDLLIB = grep length, @PDLLIB;
114 }
115 1 $PDL::AutoLoader::Rescan=0;
116 1 %PDL::AutoLoader::FileInfo = ();
117 }
118  
119 # Code to reload stuff if changed
120  
121 0 ▶sub PDL::AutoLoader::reloader {
Subroutine reloader: not called CC=5 SCAR=34.0
Pod undocumented
122 0 ▶ return unless $PDL::AutoLoader::Rescan;
Branch: 1 branch
BranchTrueFalseTotal
unless $PDL::AutoLoader::Rescan 0 0 0
123  
124 # Now check functions and reload if changed
125  
126 0 my ($file, $old_t);
127 0 for my $func (sort keys %PDL::AutoLoader::FileInfo) {
128 0 ($file, $old_t) = @{ $PDL::AutoLoader::FileInfo{$func} };
129 0 ▶ if ( (stat($file))[9]>$old_t ) { # Reload
Branch: 1 branch
BranchTrueFalseTotal
if ((stat $file)[9] > $old_t) 0 0 0
130 0 ▶ print "Reloading $file as file changed...\n" if $PDL::verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::verbose 0 0 0
131 0 PDL::AutoLoader::autoloader_do($file);
132 0 $PDL::AutoLoader::FileInfo{$func} = [ $file, (stat($file))[9] ];
133 }
134 }
135 }
136  
137 # Used for Beta, and should probably be used generall in this mod
138 #use File::Spec;
139  
140 1 ▶sub PDL::AutoLoader::import {
Subroutine import: called CC=2 SCAR=7.3
141  
142 # Beta folder support
143 # foreach (@INC) {
144 # $Beta_dir = File::Spec->catfile($_, 'PDL', 'Beta');
145 # push @PDLLIB, "+$Beta_dir" if -d $Beta_dir;
146 # }
147  
148 1 my $pkg = (caller())[0];
149 1 my $toeval = "package $pkg; no strict; no warnings;\n";
150  
151 # Make sure that the eval gets NiceSlice if we have it in this level
152 # (it's a drag that preprocessors aren't transitive...)
153 1 ▶$toeval .= "use PDL::NiceSlice;\n" if(defined $PDL::NiceSlice::VERSION);
Branch: 1 branch
BranchTrueFalseTotal
if defined $PDL::NiceSlice::VERSION 1 0 1
154  
155 1 $toeval .= <<'EOD';
156 $PDLLIB_CT = 0;
157  
158 push @PERLDL::AUTO, \&PDL::AutoLoader::reloader;
159  
160 sub AUTOLOAD {
161 local @INC = @INC;
162 my @args = @_;
163 $AUTOLOAD =~ /::([^:]*)$/;
164 my $func = $1;
165  
166 # Trap spurious calls from 'use UnknownModule'
167  
168 goto &$AUTOLOAD if ord($func)==0;
169  
170 # Check if the PDLLIB needs to be expanded and, if so, expand it.
171 # This only updates when PDLLIB changes size, which should be OK
172 # for most things but doesn't catch new directories in expanded
173 # directory trees. It seems like an OK compromise between never
174 # catching anything and always thrashing through the directories.
175 if($PDLLIB_CT != scalar(@PDLLIB)) {
176 @PDLLIB_EXPANDED = PDL::AutoLoader::expand_path(@PDLLIB);
177 $PDLLIB_CT = scalar(@PDLLIB);
178 }
179  
180 print "Loading $func.pdl ..." if $PDL::verbose;
181 my $file;
182  
183 my $s = "PDL AutoLoader: Undefined subroutine $func() cannot be autoloaded.\n";
184  
185 for my $dir (@PDLLIB_EXPANDED) {
186 $file = $dir . "/" . "$func.pdl";
187 if (-e $file) {
188
189 print "found $file\n" if $PDL::verbose;
190  
191 PDL::AutoLoader::autoloader_do($file);
192
193
194 # Remember autoloaded functions and do some reasonably
195 # smart cacheing of file/directory change times
196
197 if ($PDL::AutoLoader::Rescan) {
198 $PDL::AutoLoader::FileInfo{$func} = [ $file, (stat($file))[9] ];
199 }
200
201 # Now go to the autoload function
202 ##goto &$AUTOLOAD(@args) unless ($@ || !defined(&{$AUTOLOAD}));
203 return &$AUTOLOAD(@args) unless ($@ || !defined(&{$AUTOLOAD}));
204  
205 die $s."\tWhile parsing file `$file':\n$@\n" if($@);
206 die $s."\tFile `$file' doesn't \n\tdefine ${AUTOLOAD}().\n"
207 }
208 }
209  
210 die $s."\tNo file `$func.pdl' was found in your \@PDLLIB path.\n";
211 }
212  
213 EOD
214  
215 0 ▶eval $toeval;
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine AUTOLOAD: called CC=11 SCAR=26.3
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
216  
217 }
218  
219  
220 # Simple 'do' doesn't work with preprocessing -- this replaces
221 # "do file" and sticks NiceSlice in manually if it's needed (yuck).
222  
223 1 ▶sub PDL::AutoLoader::autoloader_do {
Subroutine autoloader_do: called CC=5 SCAR=17.4
Pod undocumented
224 1 my ($file) = shift;
225 1 ▶ if(defined($PDL::NiceSlice::VERSION)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $PDL::NiceSlice::VERSION) { } 1 0 1
226 1 ▶ print "AutoLoader: NiceSlice enabled...\n" if($PDL::debug);
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::debug 0 1 1
227 1 ▶ return if !open my $fh ,"<", $file;
Branch: 1 branch
BranchTrueFalseTotal
unless open my $fh, "<", $file 0 1 1
228 1 my $text = join "", <$fh>;
229 1 my $script = PDL::NiceSlice::perldlpp("PDL::NiceSlice", $text);
230 1 ▶ eval $script;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine autoload_func: called CC=1 SCAR=0.0
231 } else {
232 0 ▶ print "AutoLoader: no NiceSlice...\n" if($PDL::debug);
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::debug 0 0 0
233 0 do $file;
234 }
235 }
236  
237  
238 # Expand directories recursively...
239 0 ▶sub PDL::AutoLoader::expand_dir {
Subroutine expand_dir: not called CC=4 SCAR=30.0
Pod undocumented
240 0 my $dir = shift;
241 0 ▶ return undef if !-d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 0 0
242 0 opendir my($dh), $dir;
243 0 ▶ ($dir, map PDL::AutoLoader::expand_dir($_),
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
244 grep +(!m/^\./ && ($_="$dir/$_") && (-d $_)), readdir $dh);
245 }
246  
247  
248 =head2 PDL::AutoLoader::expand_path
249  
250 =for ref
251  
252 Expand a compactified path into a dir list
253  
254 You supply a pathlist and leading '+' and '~' characters get expanded into
255 full directories. Normally you don't want to use this -- it's internal to the
256 autoloader -- but some utilities, like the online documentation searcher, need
257 to be able to use it.
258  
259 =cut
260  
261 2 ▶sub PDL::AutoLoader::expand_path {
Subroutine expand_path: called CC=11 SCAR=30.1
Pod documented
262 2 my @PDLLIB = @_;
263 2 my @PDLLIB_EXPANDED;
264
265 2 ▶ print "AutoLoader: Expanding directories from ".join(':',@PDLLIB)."...\n"
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::debug 0 2 2
266 if($PDL::debug);
267 2 local $_;
268 2 foreach $_(@PDLLIB) {
269 # Expand ~{name} and ~ conventions.
270 2 ▶ if(s/^(\+?)\~(\+||[a-zA-Z0-9]*)//) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/^(\+?)\~(\+||[a-zA-Z0-9]*)//) 1 1 2
271 1 ▶ if($2 eq '+') {
Branch: 2 branches
BranchTrueFalseTotal
if ($2 eq '+') { } 0 1 1
elsif (not $2) { } 1 0 1
272 # Expand shell '+' to CWD.
273 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
274 } elsif(!$2) {
275 # No name mentioned -- use current user.
276 # Ideally would use File::HomeDir->my_home() here
277 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
278 } else {
279 # Name mentioned - try to get that user's home directory.
280 0 $_ = $1 . ( (getpwnam($2))[7] ) . $_;
281 }
282 }
283
284 # If there's a leading '+', include all subdirs too.
285 2 ▶ push(@PDLLIB_EXPANDED,
Branch: 1 branch
BranchTrueFalseTotal
s/^\+// ? : 0 2 2
286 s/^\+// ? PDL::AutoLoader::expand_dir($_) : $_
287 );
288 }
289  
290 2 ▶ print "AutoLoader: returning ",join(",",@PDLLIB_EXPANDED),"\n" if($PDL::debug);
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::debug 0 2 2
291 2 @PDLLIB_EXPANDED;
292 }
293  
294  
295 ;# Exit with OK status
296  
297 1 1;
« blib/lib/PDL/Reduce.pm ↑ Summary blib/lib/PDL/Demos.pm »