home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl_mlb.zip / lib.pm < prev    next >
Text File  |  1997-11-25  |  4KB  |  136 lines

  1. package lib;
  2.  
  3. use vars qw(@ORIG_INC);
  4. use Config;
  5.  
  6. my $archname = $Config{'archname'};
  7.  
  8. @ORIG_INC = @INC;    # take a handy copy of 'original' value
  9.  
  10.  
  11. sub import {
  12.     shift;
  13.     foreach (reverse @_) {
  14.     ## Ignore this if not defined.
  15.     next unless defined($_);
  16.     if ($_ eq '') {
  17.         require Carp;
  18.         Carp::carp("Empty compile time value given to use lib");
  19.                             # at foo.pl line ...
  20.     }
  21.     unshift(@INC, $_);
  22.     # Put a corresponding archlib directory infront of $_ if it
  23.     # looks like $_ has an archlib directory below it.
  24.     if (-d "$_/$archname") {
  25.         unshift(@INC, "$_/$archname")    if -d "$_/$archname/auto";
  26.         unshift(@INC, "$_/$archname/$]") if -d "$_/$archname/$]/auto";
  27.     }
  28.     }
  29. }
  30.  
  31.  
  32. sub unimport {
  33.     shift;
  34.     my $mode = shift if $_[0] =~ m/^:[A-Z]+/;
  35.  
  36.     my %names;
  37.     foreach(@_) {
  38.     ++$names{$_};
  39.     ++$names{"$_/$archname"} if -d "$_/$archname/auto";
  40.     }
  41.  
  42.     if ($mode and $mode eq ':ALL') {
  43.     # Remove ALL instances of each named directory.
  44.     @INC = grep { !exists $names{$_} } @INC;
  45.     } else {
  46.     # Remove INITIAL instance(s) of each named directory.
  47.     @INC = grep { --$names{$_} < 0   } @INC;
  48.     }
  49. }
  50.  
  51. 1;
  52. __END__
  53.  
  54. =head1 NAME
  55.  
  56. lib - manipulate @INC at compile time
  57.  
  58. =head1 SYNOPSIS
  59.  
  60.     use lib LIST;
  61.  
  62.     no lib LIST;
  63.  
  64. =head1 DESCRIPTION
  65.  
  66. This is a small simple module which simplifies the manipulation of @INC
  67. at compile time.
  68.  
  69. It is typically used to add extra directories to perl's search path so
  70. that later C<use> or C<require> statements will find modules which are
  71. not located on perl's default search path.
  72.  
  73. =head2 ADDING DIRECTORIES TO @INC
  74.  
  75. The parameters to C<use lib> are added to the start of the perl search
  76. path. Saying
  77.  
  78.     use lib LIST;
  79.  
  80. is I<almost> the same as saying
  81.  
  82.     BEGIN { unshift(@INC, LIST) }
  83.  
  84. For each directory in LIST (called $dir here) the lib module also
  85. checks to see if a directory called $dir/$archname/auto exists.
  86. If so the $dir/$archname directory is assumed to be a corresponding
  87. architecture specific directory and is added to @INC in front of $dir.
  88.  
  89. If LIST includes both $dir and $dir/$archname then $dir/$archname will
  90. be added to @INC twice (if $dir/$archname/auto exists).
  91.  
  92. =head2 DELETING DIRECTORIES FROM @INC
  93.  
  94. You should normally only add directories to @INC.  If you need to
  95. delete directories from @INC take care to only delete those which you
  96. added yourself or which you are certain are not needed by other modules
  97. in your script.  Other modules may have added directories which they
  98. need for correct operation.
  99.  
  100. By default the C<no lib> statement deletes the I<first> instance of
  101. each named directory from @INC.  To delete multiple instances of the
  102. same name from @INC you can specify the name multiple times.
  103.  
  104. To delete I<all> instances of I<all> the specified names from @INC you can
  105. specify ':ALL' as the first parameter of C<no lib>. For example:
  106.  
  107.     no lib qw(:ALL .);
  108.  
  109. For each directory in LIST (called $dir here) the lib module also
  110. checks to see if a directory called $dir/$archname/auto exists.
  111. If so the $dir/$archname directory is assumed to be a corresponding
  112. architecture specific directory and is also deleted from @INC.
  113.  
  114. If LIST includes both $dir and $dir/$archname then $dir/$archname will
  115. be deleted from @INC twice (if $dir/$archname/auto exists).
  116.  
  117. =head2 RESTORING ORIGINAL @INC
  118.  
  119. When the lib module is first loaded it records the current value of @INC
  120. in an array C<@lib::ORIG_INC>. To restore @INC to that value you
  121. can say
  122.  
  123.     @INC = @lib::ORIG_INC;
  124.  
  125.  
  126. =head1 SEE ALSO
  127.  
  128. FindBin - optional module which deals with paths relative to the source file.
  129.  
  130. =head1 AUTHOR
  131.  
  132. Tim Bunce, 2nd June 1995.
  133.  
  134. =cut
  135.  
  136.