Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
3.6 KiB

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