File: AutoLoader.pm

package info (click to toggle)
pdl 1.99988-5
  • links: PTS
  • area: main
  • in suites: slink
  • size: 3,908 kB
  • ctags: 3,426
  • sloc: perl: 15,352; ansic: 7,852; fortran: 3,327; makefile: 39; sh: 19
file content (158 lines) | stat: -rw-r--r-- 4,131 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158

=head1 NAME

PDL::AutoLoader -  MatLab style AutoLoader for PDL

=head1 SYNOPSIS

   use PDL::AutoLoader;
   $a = func1(...);   # Load file func1.pdl
   $b = func2(...);   # Load file func2.pdl

   $PDL::AutoLoader::Rescan = 1; # Enable re-scanning

=head1 DESCRIPTION

This module implements a MatLab style AutoLoader for PDL. If a unknown
function 'func()' is called then a file 'func.pdl' is searched for and
if found is read in to define 'func()' which is then executed.

Files are seached for using the directories in seach path @PDLLIB, which
is initialised from the shell environment variable PDLLIB which is a
colon seperated list of directories.

e.g. in csh

setenv PDLLIB "/home/kgb/pdllib:/local/pdllib"

Note this is kept seperate from PERL5LIB just in case....

=head2 AUTO-SCANNING

The variable $PDL::AutoLoader::Rescan controls whether files
are automatically re-scanned for changes at the C<perldl> command
line.

If C<$PDL::AutoLoader::Rescan == 1> and the file is changed
then the new definition is reloaded auto-matically before
executing the C<perldl> command line. Which means in practice
you can edit files, save changes and have C<perldl> see the
changes automatically.

The default is '0' - i.e. to have this feature disabled.

As this feature is only pertinent to the C<perldl> shell it imposes
no overhead on PDL scripts. Yes Bob you can have your cake and
eat it too!

Note: files are only re-evaled if they are determined to have
been changed according to their date/time stamp.

No doubt this interface could be improved upon some more. :-)

=head2 Sample file:

 sub foo { # file 'foo.pdl' - define the 'foo' function
   my $x=shift;
   return sqrt($x**2 + $x**3 + 2);
 }
 1; # File returns true (i.e. loaded successfully)


=head1 AUTHOR

Copyright(C) 1997 Karl Glazebrook (kgb@aaoepp.aao.gov.au).
All rights reserved. There is no warranty. You are allowed
to redistribute this software / documentation under certain
conditions. For details, see the file COPYING in the PDL
distribution. If this file is separated from the PDL distribution,
the copyright notice should be included in the file.

=head1 BUGS

No doubt this interface could be improved upon some more. :-)

Will probably be quite slow if C<$PDL::AutoLoader::Rescan == 1>
and thousands of functions have been autoloaded.

There could be a race condition in which the file changes
while the internal autoloader code is being executed but it
should be harmless.

Probably has not been tested enough!

=cut

BEGIN{
  @PDLLIB = (".",split(':',$ENV{"PDLLIB"})) if defined $ENV{"PDLLIB"};
  $PDL::AutoLoader::Rescan=0;
  %PDL::AutoLoader::FileInfo = ();
}

# Code to reload stuff if changed

sub PDL::AutoLoader::reloader {
   return unless $PDL::AutoLoader::Rescan;

   # Now check functions and reload if changed

   my ($file, $old_t);
   for my $func (keys %PDL::AutoLoader::FileInfo) {
       ($file, $old_t) = @{ $PDL::AutoLoader::FileInfo{$func} };
       if ( (stat($file))[9]>$old_t ) { # Reload
          print "Reloading $file as file changed...\n" if $PDL::verbose;
          do $file;
	  $PDL::AutoLoader::FileInfo{$func} = [ $file, (stat($file))[9] ];
       }
   }
}

sub PDL::AutoLoader::import {

my $pkg = (caller())[0];
my $toeval = "package $pkg;";

$toeval .= <<'EOD';

push @PERLDL::AUTO, \&PDL::AutoLoader::reloader;

sub AUTOLOAD {
    local @INC = @INC;
    $AUTOLOAD =~ /::([^:]*)$/;
    my $func = $1;

    # Trap spurious calls from 'use UnknownModule'

    goto &$AUTOLOAD if ord($func)==0;

    print "Loading $func.pdl...\n" if $PDL::verbose;
    for my $dir (@PDLLIB) {
        my $file = $dir . "/" . "$func.pdl";
	if (-e $file) {
	   # Autoload
           do $file;

	   # Remember autoloaded functions and do some reasonably
	   # smart cacheing of file/directory change times

	   if ($PDL::AutoLoader::Rescan) {
	      $PDL::AutoLoader::FileInfo{$func} = [ $file, (stat($file))[9] ];
	   }

	   # Now go to the autoloaed function

	   goto &$AUTOLOAD unless $@;
	}
    }
    die "PDL autoloader: Undefined subroutine $func() cannot be AutoLoaded\n";
}

EOD

eval $toeval;

}

;# Exit with OK status

1;