4 use Config qw(%Config);
7 use File::Path qw(mkpath);
10 $VERSION @ISA @EXPORT @EXPORT_OK
11 $Verbose $Keep $Maxlen $CheckForAutoloader $CheckModTime
16 @EXPORT = qw(&autosplit &autosplit_lib_modules);
17 @EXPORT_OK = qw($Verbose $Keep $Maxlen $CheckForAutoloader $CheckModTime);
21 AutoSplit - split a package for autoloading
25 autosplit($file, $dir, $keep, $check, $modtime);
27 autosplit_lib_modules(@modules);
31 This function will split up your program into files that the AutoLoader
32 module can handle. It is used by both the standard perl libraries and by
33 the MakeMaker utility, to automatically configure libraries for autoloading.
35 The C<autosplit> interface splits the specified file into a hierarchy
36 rooted at the directory C<$dir>. It creates directories as needed to reflect
37 class hierarchy, and creates the file F<autosplit.ix>. This file acts as
38 both forward declaration of all package routines, and as timestamp for the
39 last update of the hierarchy.
41 The remaining three arguments to C<autosplit> govern other options to
48 If the third argument, I<$keep>, is false, then any
49 pre-existing C<*.al> files in the autoload directory are removed if
50 they are no longer part of the module (obsoleted functions).
56 fourth argument, I<$check>, instructs C<autosplit> to check the module
57 currently being split to ensure that it does include a C<use>
58 specification for the AutoLoader module, and skips the module if
59 AutoLoader is not detected.
64 Lastly, the I<$modtime> argument specifies
65 that C<autosplit> is to check the modification time of the module
66 against that of the C<autosplit.ix> file, and only split the module if
68 $modtime defaults to 1.
72 Typical use of AutoSplit in the perl MakeMaker utility is via the command-line
75 perl -e 'use AutoSplit; autosplit($ARGV[0], $ARGV[1], 0, 1, 1)'
77 Defined as a Make macro, it is invoked with file and directory arguments;
78 C<autosplit> will split the specified file into the specified directory and
79 delete obsolete C<.al> files, after checking first that the module does use
80 the AutoLoader, and ensuring that the module is not already currently split
81 in its current form (the modtime test).
83 The C<autosplit_lib_modules> form is used in the building of perl. It takes
84 as input a list of files (modules) that are assumed to reside in a directory
85 B<lib> relative to the current directory. Each file is sent to the
86 autosplitter one at a time, to be split into the directory B<lib/auto>.
88 In both usages of the autosplitter, only subroutines defined following the
89 perl I<__END__> token are split out into separate files. Some
90 routines may be placed prior to this marker to force their immediate loading
93 =head2 Multiple packages
95 As of version 1.01 of the AutoSplit module it is possible to have
96 multiple packages within a single file. Both of the following cases
102 package NAME::option1;
104 package NAME::option2;
110 sub NAME::option1::BBB { ... }
111 sub NAME::option2::BBB { ... }
115 C<AutoSplit> will inform the user if it is necessary to create the
116 top-level directory specified in the invocation. It is preferred that
117 the script or installation process that invokes C<AutoSplit> have
118 created the full directory path ahead of time. This warning may
119 indicate that the module is being split into an incorrect path.
121 C<AutoSplit> will warn the user of all subroutines whose name causes
122 potential file naming conflicts on machines with drastically limited
123 (8 characters or less) file name length. Since the subroutine name is
124 used as the file name, these warnings can aid in portability to such
127 Warnings are issued and the file skipped if C<AutoSplit> cannot locate
128 either the I<__END__> marker or a "package Name;"-style specification.
130 C<AutoSplit> will also emit general diagnostics for inability to
131 create directories or files.
135 # for portability warn about names longer than $maxlen
136 $Maxlen = 8; # 8 for dos, 11 (14-".al") for SYSVR3
137 $Verbose = 1; # 0=none, 1=minimal, 2=list .al files
139 $CheckForAutoloader = 1;
142 my $IndexFile = "autosplit.ix"; # file also serves as timestamp
144 $maxflen = 14 if $Config{'d_flexfnam'} ne 'define';
145 if (defined (&Dos::UseLFN)) {
146 $maxflen = Dos::UseLFN() ? 255 : 11;
148 my $Is_VMS = ($^O eq 'VMS');
152 my($file, $autodir, $keep, $ckal, $ckmt) = @_;
153 # $file - the perl source file to be split (after __END__)
154 # $autodir - the ".../auto" dir below which to write split subs
155 # Handle optional flags:
156 $keep = $Keep unless defined $keep;
157 $ckal = $CheckForAutoloader unless defined $ckal;
158 $ckmt = $CheckModTime unless defined $ckmt;
159 autosplit_file($file, $autodir, $keep, $ckal, $ckmt);
163 # This function is used during perl building/installation
164 # ./miniperl -e 'use AutoSplit; autosplit_lib_modules(@ARGV)' ...
166 sub autosplit_lib_modules{
167 my(@modules) = @_; # list of Module names
169 while(defined($_ = shift @modules)){
170 s#::#/#g; # incase specified as ABC::XYZ
171 s|\\|/|g; # bug in ksh OS/2
172 s#^lib/##; # incase specified as lib/*.pm
173 if ($Is_VMS && /[:>\]]/) { # may need to convert VMS-style filespecs
174 my ($dir,$name) = (/(.*])(.*)/);
175 $dir =~ s/.*lib[\.\]]//;
176 $dir =~ s#[\.\]]#/#g;
179 autosplit_file("lib/$_", "lib/auto",
180 $Keep, $CheckForAutoloader, $CheckModTime);
189 my($filename, $autodir, $keep, $check_for_autoloader, $check_mod_time)
195 # where to write output files
196 $autodir ||= "lib/auto";
198 ($autodir = VMS::Filespec::unixpath($autodir)) =~ s|/$||;
199 $filename = VMS::Filespec::unixify($filename); # may have dirs
201 unless (-d $autodir){
202 mkpath($autodir,0,0755);
203 # We should never need to create the auto dir
204 # here. installperl (or similar) should have done
205 # it. Expecting it to exist is a valuable sanity check against
206 # autosplitting into some random directory by mistake.
207 print "Warning: AutoSplit had to create top-level " .
208 "$autodir unexpectedly.\n";
211 # allow just a package name to be used
212 $filename .= ".pm" unless ($filename =~ m/\.pm$/);
214 open(IN, "<$filename") or die "AutoSplit: Can't open $filename: $!\n";
215 my($pm_mod_time) = (stat($filename))[9];
216 my($autoloader_seen) = 0;
218 my($def_package,$last_package,$this_package,$fnr);
223 $in_pod = 0 if /^=cut/;
224 next if ($in_pod || /^=cut/);
226 # record last package name seen
227 $def_package = $1 if (m/^\s*package\s+([\w:]+)\s*;/);
228 ++$autoloader_seen if m/^\s*(use|require)\s+AutoLoader\b/;
229 ++$autoloader_seen if m/\bISA\s*=.*\bAutoLoader\b/;
232 if ($check_for_autoloader && !$autoloader_seen){
233 print "AutoSplit skipped $filename: no AutoLoader used\n"
237 $_ or die "Can't find __END__ in $filename\n";
239 $def_package or die "Can't find 'package Name;' in $filename\n";
241 my($modpname) = _modpname($def_package);
243 # this _has_ to match so we have a reasonable timestamp file
244 die "Package $def_package ($modpname.pm) does not ".
245 "match filename $filename"
246 unless ($filename =~ m/\Q$modpname.pm\E$/ or
247 ($^O eq 'dos') or ($^O eq 'MSWin32') or
248 $Is_VMS && $filename =~ m/$modpname.pm/i);
250 my($al_idx_file) = "$autodir/$modpname/$IndexFile";
252 if ($check_mod_time){
253 my($al_ts_time) = (stat("$al_idx_file"))[9] || 1;
254 if ($al_ts_time >= $pm_mod_time){
255 print "AutoSplit skipped ($al_idx_file newer than $filename)\n"
257 return undef; # one undef, not a list
261 print "AutoSplitting $filename ($autodir/$modpname)\n"
264 unless (-d "$autodir/$modpname"){
265 mkpath("$autodir/$modpname",0,0777);
268 # We must try to deal with some SVR3 systems with a limit of 14
269 # characters for file names. Sadly we *cannot* simply truncate all
270 # file names to 14 characters on these systems because we *must*
271 # create filenames which exactly match the names used by AutoLoader.pm.
272 # This is a problem because some systems silently truncate the file
273 # names while others treat long file names as an error.
275 my $Is83 = $maxflen==11; # plain, case INSENSITIVE dos filenames
277 my(@subnames, $subname, %proto, %package);
284 $in_pod = 0 if /^=cut/;
285 next if ($in_pod || /^=cut/);
286 # the following (tempting) old coding gives big troubles if a
287 # cut is forgotten at EOF:
288 # next if /^=\w/ .. /^=cut/;
289 if (/^package\s+([\w:]+)\s*;/) {
290 $this_package = $def_package = $1;
292 if (/^sub\s+([\w:]+)(\s*\(.*?\))?/) {
293 print OUT "# end of $last_package\::$subname\n1;\n"
296 my $proto = $2 || '';
297 if ($subname =~ s/(.*):://){
300 $this_package = $def_package;
302 my $fq_subname = "$this_package\::$subname";
303 $package{$fq_subname} = $this_package;
304 $proto{$fq_subname} = $proto;
305 push(@subnames, $fq_subname);
306 my($lname, $sname) = ($subname, substr($subname,0,$maxflen-3));
307 $modpname = _modpname($this_package);
308 mkpath("$autodir/$modpname",0,0777);
309 my($lpath) = "$autodir/$modpname/$lname.al";
310 my($spath) = "$autodir/$modpname/$sname.al";
312 if (!$Is83 and open(OUT, ">$lpath")){
314 # perl downcases all filenames on VMS (which upcases all filenames) so
315 # we'd better downcase the sub name list too, or subs with upper case
316 # letters in them will get their .al files deleted right after they're
317 # created. (The mixed case sub name won't match the all-lowercase
318 # filename, and so be cleaned up as a scrap file)
319 my $opath = ($Is_VMS or $Is83) ? lc($path) : $path;
320 $outfiles{$opath} = $path;
321 print " writing $lpath\n" if ($Verbose>=2);
324 # same as above comment
325 my $opath = ($Is_VMS or $Is83) ? lc($path) : $path;
326 my $mode = exists $outfiles{$opath} ? ">>" : ">";
327 open(OUT, "$mode$spath") or die "Can't create $spath: $!\n";
328 $outfiles{$opath} = $path;
329 print " writing $spath (with truncated name)\n"
333 # NOTE: Derived from $filename.
334 # Changes made here will be lost when autosplit again.
336 package $this_package;
338 #line $fnr "$filename (autosplit into $path)"
345 push(@cache, $_) if @cache || /\S/;
357 $last_package = $this_package if defined $this_package;
359 print OUT @cache,"1;\n# end of $last_package\::$subname\n";
363 if (!$keep){ # don't keep any obsolete *.al files in the directory
364 my(%outdirs,@outdirs);
365 for (values %outfiles) {
366 $outdirs{File::Basename::dirname($_)}||=1;
368 for my $dir (keys %outdirs) {
369 opendir(OUTDIR,$dir);
370 foreach (sort readdir(OUTDIR)){
372 my($file) = "$dir/$_";
373 $file = lc $file if $Is83 or $Is_VMS;
374 next if $outfiles{$file};
375 print " deleting $file\n" if ($Verbose>=2);
376 my($deleted,$thistime); # catch all versions on VMS
377 do { $deleted += ($thistime = unlink $file) } while ($thistime);
378 carp "Unable to delete $file: $!" unless $deleted;
384 open(TS,">$al_idx_file") or
385 carp "AutoSplit: unable to create timestamp file ($al_idx_file): $!";
386 print TS "# Index created by AutoSplit for $filename\n";
387 print TS "# (file acts as timestamp)\n";
389 for my $fqs (@subnames) {
391 $subname =~ s/.*:://;
392 print TS "package $package{$fqs};\n"
393 unless $last_package eq $package{$fqs};
394 print TS "sub $subname $proto{$fqs};\n";
395 $last_package = $package{$fqs};
405 my $modpname = $package;
406 if ($^O eq 'MSWin32') {
407 $modpname =~ s#::#\\#g;
409 $modpname =~ s#::#/#g;
417 # test functions so AutoSplit.pm can be applied to itself:
418 sub test1 ($) { "test 1\n"; }
419 sub test2 ($$) { "test 2\n"; }
420 sub test3 ($$$) { "test 3\n"; }
421 sub testtesttesttest4_1 { "test 4\n"; }
422 sub testtesttesttest4_2 { "duplicate test 4\n"; }
423 sub Just::Another::test5 { "another test 5\n"; }
424 sub test6 { return join ":", __FILE__,__LINE__; }
425 package Yet::Another::AutoSplit;
426 sub testtesttesttest4_1 ($) { "another test 4\n"; }
427 sub testtesttesttest4_2 ($$) { "another duplicate test 4\n"; }