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.

169 lines
4.2 KiB

  1. # FindBin.pm
  2. #
  3. # Copyright (c) 1995 Graham Barr & Nick Ing-Simmons. All rights reserved.
  4. # This program is free software; you can redistribute it and/or modify it
  5. # under the same terms as Perl itself.
  6. =head1 NAME
  7. FindBin - Locate directory of original perl script
  8. =head1 SYNOPSIS
  9. use FindBin;
  10. use lib "$FindBin::Bin/../lib";
  11. or
  12. use FindBin qw($Bin);
  13. use lib "$Bin/../lib";
  14. =head1 DESCRIPTION
  15. Locates the full path to the script bin directory to allow the use
  16. of paths relative to the bin directory.
  17. This allows a user to setup a directory tree for some software with
  18. directories E<lt>rootE<gt>/bin and E<lt>rootE<gt>/lib and then the above example will allow
  19. the use of modules in the lib directory without knowing where the software
  20. tree is installed.
  21. If perl is invoked using the B<-e> option or the perl script is read from
  22. C<STDIN> then FindBin sets both C<$Bin> and C<$RealBin> to the current
  23. directory.
  24. =head1 EXPORTABLE VARIABLES
  25. $Bin - path to bin directory from where script was invoked
  26. $Script - basename of script from which perl was invoked
  27. $RealBin - $Bin with all links resolved
  28. $RealScript - $Script with all links resolved
  29. =head1 KNOWN BUGS
  30. if perl is invoked as
  31. perl filename
  32. and I<filename> does not have executable rights and a program called I<filename>
  33. exists in the users C<$ENV{PATH}> which satisfies both B<-x> and B<-T> then FindBin
  34. assumes that it was invoked via the C<$ENV{PATH}>.
  35. Workaround is to invoke perl as
  36. perl ./filename
  37. =head1 AUTHORS
  38. FindBin is supported as part of the core perl distribution. Please send bug
  39. reports to E<lt>F<[email protected]>E<gt> using the perlbug program included with perl.
  40. Graham Barr E<lt>F<[email protected]>E<gt>
  41. Nick Ing-Simmons E<lt>F<[email protected]>E<gt>
  42. =head1 COPYRIGHT
  43. Copyright (c) 1995 Graham Barr & Nick Ing-Simmons. All rights reserved.
  44. This program is free software; you can redistribute it and/or modify it
  45. under the same terms as Perl itself.
  46. =cut
  47. package FindBin;
  48. use Carp;
  49. require 5.000;
  50. require Exporter;
  51. use Cwd qw(getcwd abs_path);
  52. use Config;
  53. use File::Basename;
  54. use File::Spec;
  55. @EXPORT_OK = qw($Bin $Script $RealBin $RealScript $Dir $RealDir);
  56. %EXPORT_TAGS = (ALL => [qw($Bin $Script $RealBin $RealScript $Dir $RealDir)]);
  57. @ISA = qw(Exporter);
  58. $VERSION = $VERSION = "1.42";
  59. BEGIN
  60. {
  61. *Dir = \$Bin;
  62. *RealDir = \$RealBin;
  63. if($0 eq '-e' || $0 eq '-')
  64. {
  65. # perl invoked with -e or script is on C<STDIN>
  66. $Script = $RealScript = $0;
  67. $Bin = $RealBin = getcwd();
  68. }
  69. else
  70. {
  71. my $script = $0;
  72. if ($^O eq 'VMS')
  73. {
  74. ($Bin,$Script) = VMS::Filespec::rmsexpand($0) =~ /(.*\])(.*)/;
  75. ($RealBin,$RealScript) = ($Bin,$Script);
  76. }
  77. else
  78. {
  79. my $IsWin32 = $^O eq 'MSWin32';
  80. unless(($script =~ m#/# || ($IsWin32 && $script =~ m#\\#))
  81. && -f $script)
  82. {
  83. my $dir;
  84. foreach $dir (File::Spec->path)
  85. {
  86. my $scr = File::Spec->catfile($dir, $script);
  87. if(-r $scr && (!$IsWin32 || -x _))
  88. {
  89. $script = $scr;
  90. if (-f $0)
  91. {
  92. # $script has been found via PATH but perl could have
  93. # been invoked as 'perl file'. Do a dumb check to see
  94. # if $script is a perl program, if not then $script = $0
  95. #
  96. # well we actually only check that it is an ASCII file
  97. # we know its executable so it is probably a script
  98. # of some sort.
  99. $script = $0 unless(-T $script);
  100. }
  101. last;
  102. }
  103. }
  104. }
  105. croak("Cannot find current script '$0'") unless(-f $script);
  106. # Ensure $script contains the complete path incase we C<chdir>
  107. $script = File::Spec->catfile(getcwd(), $script)
  108. unless File::Spec->file_name_is_absolute($script);
  109. ($Script,$Bin) = fileparse($script);
  110. # Resolve $script if it is a link
  111. while(1)
  112. {
  113. my $linktext = readlink($script);
  114. ($RealScript,$RealBin) = fileparse($script);
  115. last unless defined $linktext;
  116. $script = (File::Spec->file_name_is_absolute($linktext))
  117. ? $linktext
  118. : File::Spec->catfile($RealBin, $linktext);
  119. }
  120. # Get absolute paths to directories
  121. $Bin = abs_path($Bin) if($Bin);
  122. $RealBin = abs_path($RealBin) if($RealBin);
  123. }
  124. }
  125. }
  126. 1; # Keep require happy