| 1 | package import; | ||
| 2 | # I have chosen a lowercase name, as the intent is a pragma | ||
| 3 | |||
| 4 | # SCCS INFO: @(#) import.pm 1.01 99/10/10 | ||
| 5 | # RCS INFO: $Id: import.pm,v 1.01 1999/10/10 mak Exp $ | ||
| 6 | # | ||
| 7 | # Copyright (C) 1997,1998 Michael King (mike808@mo.net) | ||
| 8 | # Saint Louis, MO USA. | ||
| 9 | # | ||
| 10 | # This module is free software; you can redistribute it and/or | ||
| 11 | # modify it under the same terms as Perl itself. | ||
| 12 | |||
| 13 | =head1 NAME | ||
| 14 | |||
| 15 | import - import all modules with a package prefix, aliasing it away. | ||
| 16 | |||
| 17 | 'import' acts as a pragma that performs a 'use' on all modules that can be | ||
| 18 | found with the given package prefix. Any modules found have a 'use' statement | ||
| 19 | performed, and the fully qualified package name is aliased to one without the | ||
| 20 | given prefix. The functionality is similar to Java's 'import' statement. | ||
| 21 | |||
| 22 | See import.pod for details. | ||
| 23 | |||
| 24 | =head1 HISTORY | ||
| 25 | |||
| 26 | import.pm | ||
| 27 | v1.01 10/10/99 mak | ||
| 28 | |||
| 29 | =head1 COPYRIGHT | ||
| 30 | |||
| 31 | Copyright (C) 1999 Michael King ( mike808@mo.net ) | ||
| 32 | Saint Louis, MO USA. | ||
| 33 | |||
| 34 | This module is free software; you can redistribute it and/or | ||
| 35 | modify it under the same terms as Perl itself. | ||
| 36 | |||
| 37 | This module is copyright (c) 1997,1998 by Michael King ( mike808@mo.net ) and is | ||
| 38 | made available to the Perl public under terms of the Artistic License used to | ||
| 39 | cover Perl itself. See the file Artistic in the distribution of Perl 5.002 or | ||
| 40 | later for details of copy and distribution terms. | ||
| 41 | |||
| 42 | =head1 AVAILABILITY | ||
| 43 | |||
| 44 | The latest version of this module is likely to be available from: | ||
| 45 | |||
| 46 | http://walden.mo.net/~mike808/import_pm | ||
| 47 | |||
| 48 | The best place to discuss this code is via email with the author. | ||
| 49 | |||
| 50 | =cut | ||
| 51 | |||
| 52 | # --- END OF PAGE ---.#- - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| 53 | |||
| 54 | use strict; | ||
| 55 | |||
| 56 | # CORE module usage | ||
| 57 | use Carp; | ||
| 58 | use DirHandle; | ||
| 59 | |||
| 60 | # Declare our own version | ||
| 61 | $import::VERSION = 1.01; | ||
| 62 | sub VERSION { $import::VERSION }; | ||
| 63 | |||
| 64 | # Use a custom import | ||
| 65 | sub import { | ||
| 66 | my $selfpkg = shift; | ||
| 67 | my $callpkg = caller; | ||
| 68 | my @dir = @_; | ||
| 69 | foreach my $pkg (@dir) { | ||
| 70 | my $dir = $pkg; | ||
| 71 | $dir =~ s|::|/|g; # Convert :: to / | ||
| 72 | $dir =~ s{(^/|/$)}{}g; # Strip leading trailing slashes | ||
| 73 | |||
| 74 | my %PKG; # Holds lists of modules found keyed to their package name | ||
| 75 | my @PKG; # Preserves the order of packages inserted into %PKG | ||
| 76 | |||
| 77 | # Look in each of the @INC dirs | ||
| 78 | foreach my $inc_dir (@INC) { | ||
| 79 | my $dirpath = "$inc_dir/$dir"; | ||
| 80 | |||
| 81 | # Try for a module file first | ||
| 82 | my $pm = "${dirpath}.pm"; | ||
| 83 | if ( -f $pm ) { # Found a module file! | ||
| 84 | $pm =~ s|.*/||; # strip path prefix | ||
| 85 | $pm =~ s|\.pm$||; # strip .pm suffix | ||
| 86 | |||
| 87 | # Save the module | ||
| 88 | %PKG = ( # Assign the package list | ||
| 89 | $inc_dir => [ $pm ], # %PKG holds array refs | ||
| 90 | ); | ||
| 91 | @PKG = ( $inc_dir ); # Preserve order in %PKG | ||
| 92 | $dir =~ s|/$pm$||; # Drop our package name portion | ||
| 93 | last; # We're done since we found a module | ||
| 94 | } | ||
| 95 | |||
| 96 | next unless -d $dirpath; # Found a directory! | ||
| 97 | |||
| 98 | # Get ready to read it | ||
| 99 | my $dh = DirHandle->new($dirpath) | ||
| 100 | or croak "Could not opendir '$dirpath': $!"; | ||
| 101 | |||
| 102 | # Grab all the .pms | ||
| 103 | my @pm = sort # sort them | ||
| 104 | grep { -f } # only plain files | ||
| 105 | map { "$dirpath/$_" } # full path | ||
| 106 | grep { !/^\./ } # filter out dot files | ||
| 107 | grep { /\.pm$/ } # filter in pm files | ||
| 108 | $dh->read; # read all entries in the dir | ||
| 109 | |||
| 110 | map { s|^$dirpath/|| } @pm; # strip dirpath prefix | ||
| 111 | map { s|\.pm$|| } @pm; # strip .pm suffix | ||
| 112 | |||
| 113 | # Save the list | ||
| 114 | $PKG{$inc_dir} = \@pm; | ||
| 115 | push @PKG, $inc_dir; # Preserve order in %PKG | ||
| 116 | } | ||
| 117 | |||
| 118 | # Alias the symbol tables | ||
| 119 | foreach (@PKG) { | ||
| 120 | foreach my $alias (@{$PKG{$_}}) { | ||
| 121 | my $fq_pkg = $dir . "/" . $alias . ".pm"; | ||
| 122 | # Do a 'use' - dups are ignored by require so we don't have to | ||
| 123 | require $fq_pkg && $fq_pkg->import(); | ||
| 124 | no strict qw( refs ); | ||
| 125 | *{$alias . "::"} = *{$pkg . "::" . $alias . "::"}; | ||
| 126 | } | ||
| 127 | } | ||
| 128 | } | ||
| 129 | # Kaboom! | ||
| 130 | |||
| 131 | # Hunt ourselves down in the %INC - we are a pragma | ||
| 132 | foreach my $key (sort keys %INC) { | ||
| 133 | next unless $key =~ m|/?${selfpkg}.pm$|; | ||
| 134 | $INC{$key} =~ m|/?${selfpkg}.pm$|; | ||
| 135 | my $self = $`; | ||
| 136 | use Cwd qw(); | ||
| 137 | if ($self) { | ||
| 138 | $self = Cwd::cwd() . "/" . $self unless $self =~ m/^\//; | ||
| 139 | } else { | ||
| 140 | $self = Cwd::cwd(); | ||
| 141 | } | ||
| 142 | delete $INC{$key}; # Remove ourselves from the %INC | ||
| 143 | last; | ||
| 144 | } | ||
| 145 | } | ||
| 146 | |||
| 147 | 1; |