Free memory of large lexical variables when leaving scope
[p5sagit/p5-mst-13.2.git] / lib / AutoSplit.pm
CommitLineData
a0d0e21e 1package AutoSplit;
2
3require 5.000;
4require Exporter;
5
6use Config;
7use Carp;
8
9@ISA = qw(Exporter);
10@EXPORT = qw(&autosplit &autosplit_lib_modules);
3edbfbe5 11@EXPORT_OK = qw($Verbose $Keep $Maxlen $CheckForAutoloader $CheckModTime);
a0d0e21e 12
f06db76b 13=head1 NAME
14
15AutoSplit - split a package for autoloading
16
cb1a09d0 17=head1 SYNOPSIS
18
21c92a1d 19 perl -e 'use AutoSplit; autosplit_lib_modules(@ARGV)' ...
20
21 use AutoSplit; autosplit($file, $dir, $keep, $check, $modtime);
22
23for perl versions 5.002 and later:
84dc3c4d 24
21c92a1d 25 perl -MAutoSplit -e 'autosplit($ARGV[0], $ARGV[1], $k, $chk, $modtime)' ...
cb1a09d0 26
f06db76b 27=head1 DESCRIPTION
28
29This function will split up your program into files that the AutoLoader
21c92a1d 30module can handle. It is used by both the standard perl libraries and by
31the MakeMaker utility, to automatically configure libraries for autoloading.
32
33The C<autosplit> interface splits the specified file into a hierarchy
34rooted at the directory C<$dir>. It creates directories as needed to reflect
35class hierarchy, and creates the file F<autosplit.ix>. This file acts as
36both forward declaration of all package routines, and as timestamp for the
37last update of the hierarchy.
38
39The remaining three arguments to C<autosplit> govern other options to the
40autosplitter. If the third argument, I<$keep>, is false, then any pre-existing
41C<.al> files in the autoload directory are removed if they are no longer
42part of the module (obsoleted functions). The fourth argument, I<$check>,
43instructs C<autosplit> to check the module currently being split to ensure
44that it does include a C<use> specification for the AutoLoader module, and
45skips the module if AutoLoader is not detected. Lastly, the I<$modtime>
46argument specifies that C<autosplit> is to check the modification time of the
47module against that of the C<autosplit.ix> file, and only split the module
48if it is newer.
49
50Typical use of AutoSplit in the perl MakeMaker utility is via the command-line
51with:
52
53 perl -e 'use AutoSplit; autosplit($ARGV[0], $ARGV[1], 0, 1, 1)'
54
55Defined as a Make macro, it is invoked with file and directory arguments;
56C<autosplit> will split the specified file into the specified directory and
57delete obsolete C<.al> files, after checking first that the module does use
58the AutoLoader, and ensuring that the module is not already currently split
59in its current form (the modtime test).
60
61The C<autosplit_lib_modules> form is used in the building of perl. It takes
62as input a list of files (modules) that are assumed to reside in a directory
63B<lib> relative to the current directory. Each file is sent to the
64autosplitter one at a time, to be split into the directory B<lib/auto>.
65
66In both usages of the autosplitter, only subroutines defined following the
67perl special marker I<__END__> are split out into separate files. Some
68routines may be placed prior to this marker to force their immediate loading
69and parsing.
70
71=head1 CAVEATS
72
73Currently, C<AutoSplit> cannot handle multiple package specifications
74within one file.
75
76=head1 DIAGNOSTICS
77
78C<AutoSplit> will inform the user if it is necessary to create the top-level
79directory specified in the invocation. It is preferred that the script or
80installation process that invokes C<AutoSplit> have created the full directory
81path ahead of time. This warning may indicate that the module is being split
82into an incorrect path.
83
84C<AutoSplit> will warn the user of all subroutines whose name causes potential
85file naming conflicts on machines with drastically limited (8 characters or
86less) file name length. Since the subroutine name is used as the file name,
87these warnings can aid in portability to such systems.
88
89Warnings are issued and the file skipped if C<AutoSplit> cannot locate either
90the I<__END__> marker or a "package Name;"-style specification.
91
92C<AutoSplit> will also emit general diagnostics for inability to create
93directories or files.
f06db76b 94
95=cut
96
a0d0e21e 97# for portability warn about names longer than $maxlen
98$Maxlen = 8; # 8 for dos, 11 (14-".al") for SYSVR3
99$Verbose = 1; # 0=none, 1=minimal, 2=list .al files
100$Keep = 0;
3edbfbe5 101$CheckForAutoloader = 1;
102$CheckModTime = 1;
a0d0e21e 103
3edbfbe5 104$IndexFile = "autosplit.ix"; # file also serves as timestamp
a0d0e21e 105$maxflen = 255;
106$maxflen = 14 if $Config{'d_flexfnam'} ne 'define';
c6538b72 107$Is_VMS = ($^O eq 'VMS');
a0d0e21e 108
3edbfbe5 109
a0d0e21e 110sub autosplit{
75f92628 111 my($file, $autodir, $k, $ckal, $ckmt) = @_;
112 # $file - the perl source file to be split (after __END__)
113 # $autodir - the ".../auto" dir below which to write split subs
114 # Handle optional flags:
115 $keep = $Keep unless defined $k;
116 $ckal = $CheckForAutoloader unless defined $ckal;
117 $ckmt = $CheckModTime unless defined $ckmt;
118 autosplit_file($file, $autodir, $keep, $ckal, $ckmt);
a0d0e21e 119}
120
121
a0d0e21e 122# This function is used during perl building/installation
21c92a1d 123# ./miniperl -e 'use AutoSplit; autosplit_lib_modules(@ARGV)' ...
a0d0e21e 124
125sub autosplit_lib_modules{
126 my(@modules) = @_; # list of Module names
127
128 foreach(@modules){
129 s#::#/#g; # incase specified as ABC::XYZ
4633a7c4 130 s|\\|/|g; # bug in ksh OS/2
a0d0e21e 131 s#^lib/##; # incase specified as lib/*.pm
c6538b72 132 if ($Is_VMS && /[:>\]]/) { # may need to convert VMS-style filespecs
a0d0e21e 133 my ($dir,$name) = (/(.*])(.*)/);
134 $dir =~ s/.*lib[\.\]]//;
135 $dir =~ s#[\.\]]#/#g;
136 $_ = $dir . $name;
137 }
3edbfbe5 138 autosplit_file("lib/$_", "lib/auto", $Keep, $CheckForAutoloader, $CheckModTime);
a0d0e21e 139 }
140 0;
141}
142
143
144# private functions
145
146sub autosplit_file{
147 my($filename, $autodir, $keep, $check_for_autoloader, $check_mod_time) = @_;
148 my(@names);
149
150 # where to write output files
151 $autodir = "lib/auto" unless $autodir;
c6538b72 152 ($autodir = VMS::Filespec::unixpath($autodir)) =~ s#/$## if $Is_VMS;
3edbfbe5 153 unless (-d $autodir){
154 local($", @p)="/";
155 foreach(split(/\//,$autodir)){
156 push(@p, $_);
157 next if -d "@p/";
158 mkdir("@p",0755) or die "AutoSplit unable to mkdir @p: $!";
159 }
160 # We should never need to create the auto dir here. installperl
161 # (or similar) should have done it. Expecting it to exist is a valuable
162 # sanity check against autosplitting into some random directory by mistake.
163 print "Warning: AutoSplit had to create top-level $autodir unexpectedly.\n";
164 }
a0d0e21e 165
166 # allow just a package name to be used
167 $filename .= ".pm" unless ($filename =~ m/\.pm$/);
168
169 open(IN, "<$filename") || die "AutoSplit: Can't open $filename: $!\n";
170 my($pm_mod_time) = (stat($filename))[9];
171 my($autoloader_seen) = 0;
f06db76b 172 my($in_pod) = 0;
a0d0e21e 173 while (<IN>) {
f06db76b 174 # Skip pod text.
175 $in_pod = 1 if /^=/;
176 $in_pod = 0 if /^=cut/;
177 next if ($in_pod || /^=cut/);
178
a0d0e21e 179 # record last package name seen
180 $package = $1 if (m/^\s*package\s+([\w:]+)\s*;/);
3edbfbe5 181 ++$autoloader_seen if m/^\s*(use|require)\s+AutoLoader\b/;
a0d0e21e 182 ++$autoloader_seen if m/\bISA\s*=.*\bAutoLoader\b/;
183 last if /^__END__/;
184 }
3edbfbe5 185 if ($check_for_autoloader && !$autoloader_seen){
186 print "AutoSplit skipped $filename: no AutoLoader used\n" if ($Verbose>=2);
187 return 0
188 }
a0d0e21e 189 $_ or die "Can't find __END__ in $filename\n";
190
191 $package or die "Can't find 'package Name;' in $filename\n";
192
193 my($modpname) = $package; $modpname =~ s#::#/#g;
194 my($al_idx_file) = "$autodir/$modpname/$IndexFile";
195
196 die "Package $package does not match filename $filename"
197 unless ($filename =~ m/$modpname.pm$/ or
55497cff 198 ($^O eq "msdos") or
c6538b72 199 $Is_VMS && $filename =~ m/$modpname.pm/i);
a0d0e21e 200
201 if ($check_mod_time){
202 my($al_ts_time) = (stat("$al_idx_file"))[9] || 1;
203 if ($al_ts_time >= $pm_mod_time){
204 print "AutoSplit skipped ($al_idx_file newer that $filename)\n"
205 if ($Verbose >= 2);
206 return undef; # one undef, not a list
207 }
208 }
209
210 my($from) = ($Verbose>=2) ? "$filename => " : "";
211 print "AutoSplitting $package ($from$autodir/$modpname)\n"
212 if $Verbose;
213
214 unless (-d "$autodir/$modpname"){
215 local($", @p)="/";
216 foreach(split(/\//,"$autodir/$modpname")){
217 push(@p, $_);
42793c05 218 next if -d "@p/";
a0d0e21e 219 mkdir("@p",0777) or die "AutoSplit unable to mkdir @p: $!";
220 }
221 }
222
223 # We must try to deal with some SVR3 systems with a limit of 14
224 # characters for file names. Sadly we *cannot* simply truncate all
225 # file names to 14 characters on these systems because we *must*
226 # create filenames which exactly match the names used by AutoLoader.pm.
227 # This is a problem because some systems silently truncate the file
228 # names while others treat long file names as an error.
229
230 # We do not yet deal with multiple packages within one file.
231 # Ideally both of these styles should work.
232 #
233 # package NAME;
234 # __END__
235 # sub AAA { ... }
236 # package NAME::option1;
237 # sub BBB { ... }
238 # package NAME::option2;
239 # sub BBB { ... }
240 #
241 # package NAME;
242 # __END__
243 # sub AAA { ... }
244 # sub NAME::option1::BBB { ... }
245 # sub NAME::option2::BBB { ... }
246 #
247 # For now both of these produce warnings.
248
249 open(OUT,">/dev/null") || open(OUT,">nla0:"); # avoid 'not opened' warning
4633a7c4 250 my(@subnames, %proto);
a0d0e21e 251 while (<IN>) {
252 if (/^package ([\w:]+)\s*;/) {
253 warn "package $1; in AutoSplit section ignored. Not currently supported.";
254 }
4633a7c4 255 if (/^sub\s+([\w:]+)(\s*\(.*?\))?/) {
a0d0e21e 256 print OUT "1;\n";
4633a7c4 257 my $subname = $1;
258 $proto{$1} = $2 or '';
a0d0e21e 259 if ($subname =~ m/::/){
260 warn "subs with package names not currently supported in AutoSplit section";
261 }
262 push(@subnames, $subname);
263 my($lname, $sname) = ($subname, substr($subname,0,$maxflen-3));
264 my($lpath) = "$autodir/$modpname/$lname.al";
265 my($spath) = "$autodir/$modpname/$sname.al";
266 unless(open(OUT, ">$lpath")){
267 open(OUT, ">$spath") or die "Can't create $spath: $!\n";
268 push(@names, $sname);
269 print " writing $spath (with truncated name)\n"
270 if ($Verbose>=1);
271 }else{
272 push(@names, $lname);
273 print " writing $lpath\n" if ($Verbose>=2);
274 }
275 print OUT "# NOTE: Derived from $filename. ",
276 "Changes made here will be lost.\n";
277 print OUT "package $package;\n\n";
278 }
279 print OUT $_;
280 }
281 print OUT "1;\n";
282 close(OUT);
283 close(IN);
284
285 if (!$keep){ # don't keep any obsolete *.al files in the directory
286 my(%names);
287 @names{@names} = @names;
288 opendir(OUTDIR,"$autodir/$modpname");
289 foreach(sort readdir(OUTDIR)){
290 next unless /\.al$/;
291 my($subname) = m/(.*)\.al$/;
292 next if $names{substr($subname,0,$maxflen-3)};
293 my($file) = "$autodir/$modpname/$_";
294 print " deleting $file\n" if ($Verbose>=2);
f06db76b 295 my($deleted,$thistime); # catch all versions on VMS
296 do { $deleted += ($thistime = unlink $file) } while ($thistime);
297 carp "Unable to delete $file: $!" unless $deleted;
a0d0e21e 298 }
299 closedir(OUTDIR);
300 }
301
302 open(TS,">$al_idx_file") or
303 carp "AutoSplit: unable to create timestamp file ($al_idx_file): $!";
304 print TS "# Index created by AutoSplit for $filename (file acts as timestamp)\n";
f06db76b 305 print TS "package $package;\n";
4633a7c4 306 print TS map("sub $_$proto{$_} ;\n", @subnames);
f06db76b 307 print TS "1;\n";
a0d0e21e 308 close(TS);
309
310 check_unique($package, $Maxlen, 1, @names);
311
312 @names;
313}
314
315
316sub check_unique{
317 my($module, $maxlen, $warn, @names) = @_;
318 my(%notuniq) = ();
319 my(%shorts) = ();
320 my(@toolong) = grep(length > $maxlen, @names);
321
322 foreach(@toolong){
323 my($trunc) = substr($_,0,$maxlen);
324 $notuniq{$trunc}=1 if $shorts{$trunc};
325 $shorts{$trunc} = ($shorts{$trunc}) ? "$shorts{$trunc}, $_" : $_;
326 }
327 if (%notuniq && $warn){
328 print "$module: some names are not unique when truncated to $maxlen characters:\n";
329 foreach(keys %notuniq){
330 print " $shorts{$_} truncate to $_\n";
331 }
332 }
333 %notuniq;
334}
335
3361;
337__END__
338
339# test functions so AutoSplit.pm can be applied to itself:
340sub test1{ "test 1\n"; }
341sub test2{ "test 2\n"; }
342sub test3{ "test 3\n"; }
343sub test4{ "test 4\n"; }
344
345