OS2::PrfDB was exploiting a bug in U32 XSUBs
[p5sagit/p5-mst-13.2.git] / lib / FindBin.pm
CommitLineData
a73990fd 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
7=head1 NAME
8
9FindBin - Locate directory of original perl script
10
11=head1 SYNOPSIS
12
13 use FindBin;
d7791a84 14 use lib "$FindBin::Bin/../lib";
a73990fd 15
8b88ae92 16 or
a73990fd 17
18 use FindBin qw($Bin);
d7791a84 19 use lib "$Bin/../lib";
a73990fd 20
21=head1 DESCRIPTION
22
23Locates the full path to the script bin directory to allow the use
24of paths relative to the bin directory.
25
26This allows a user to setup a directory tree for some software with
1fef88e7 27directories E<lt>rootE<gt>/bin and E<lt>rootE<gt>/lib and then the above example will allow
a73990fd 28the use of modules in the lib directory without knowing where the software
29tree is installed.
30
84dc3c4d 31If perl is invoked using the B<-e> option or the perl script is read from
a73990fd 32C<STDIN> then FindBin sets both C<$Bin> and C<$RealBin> to the current
33directory.
34
35=head1 EXPORTABLE VARIABLES
36
37 $Bin - path to bin directory from where script was invoked
38 $Script - basename of script from which perl was invoked
39 $RealBin - $Bin with all links resolved
40 $RealScript - $Script with all links resolved
41
42=head1 KNOWN BUGS
43
44if perl is invoked as
45
46 perl filename
47
48and I<filename> does not have executable rights and a program called I<filename>
84dc3c4d 49exists in the users C<$ENV{PATH}> which satisfies both B<-x> and B<-T> then FindBin
a73990fd 50assumes that it was invoked via the C<$ENV{PATH}>.
51
52Workaround is to invoke perl as
53
54 perl ./filename
55
56=head1 AUTHORS
57
1fef88e7 58Graham Barr E<lt>F<bodg@tiuk.ti.com>E<gt>
59Nick Ing-Simmons E<lt>F<nik@tiuk.ti.com>E<gt>
a73990fd 60
61=head1 COPYRIGHT
62
63Copyright (c) 1995 Graham Barr & Nick Ing-Simmons. All rights reserved.
64This program is free software; you can redistribute it and/or modify it
65under the same terms as Perl itself.
66
67=head1 REVISION
68
69$Revision: 1.4 $
70
71=cut
72
73package FindBin;
74use Carp;
75require 5.000;
76require Exporter;
51a19bc0 77use File::Spec;
8b88ae92 78use Cwd qw(getcwd abs_path);
79use Config;
80use File::Basename;
a73990fd 81
82@EXPORT_OK = qw($Bin $Script $RealBin $RealScript $Dir $RealDir);
83%EXPORT_TAGS = (ALL => [qw($Bin $Script $RealBin $RealScript $Dir $RealDir)]);
84@ISA = qw(Exporter);
85
63abd723 86$VERSION = $VERSION = sprintf("%d.%02d", q$Revision: 1.42 $ =~ /(\d+)\.(\d+)/);
a73990fd 87
a73990fd 88BEGIN
89{
90 *Dir = \$Bin;
91 *RealDir = \$RealBin;
92
93 if($0 eq '-e' || $0 eq '-')
94 {
95 # perl invoked with -e or script is on C<STDIN>
96
97 $Script = $RealScript = $0;
98 $Bin = $RealBin = getcwd();
99 }
100 else
101 {
102 my $script = $0;
103
104 if ($^O eq 'VMS')
105 {
106 ($Bin,$Script) = VMS::Filespec::rmsexpand($0) =~ /(.*\])(.*)/;
107 ($RealBin,$RealScript) = ($Bin,$Script);
108 }
109 else
110 {
8b88ae92 111 my $IsWin32 = $^O eq 'MSWin32';
112 unless(($script =~ m#/# || ($IsWin32 && $script =~ m#\\#))
113 && -f $script)
a73990fd 114 {
115 my $dir;
25931d33 116 my $pathvar = 'PATH';
8b88ae92 117
118 foreach $dir (split(/$Config{'path_sep'}/,$ENV{$pathvar}))
a73990fd 119 {
8b88ae92 120 if(-r "$dir/$script" && (!$IsWin32 || -x _))
a73990fd 121 {
122 $script = "$dir/$script";
8b88ae92 123
124 if (-f $0)
a73990fd 125 {
126 # $script has been found via PATH but perl could have
127 # been invoked as 'perl file'. Do a dumb check to see
128 # if $script is a perl program, if not then $script = $0
129 #
130 # well we actually only check that it is an ASCII file
131 # we know its executable so it is probably a script
132 # of some sort.
8b88ae92 133
a73990fd 134 $script = $0 unless(-T $script);
135 }
136 last;
137 }
138 }
139 }
8b88ae92 140
a73990fd 141 croak("Cannot find current script '$0'") unless(-f $script);
8b88ae92 142
a73990fd 143 # Ensure $script contains the complete path incase we C<chdir>
8b88ae92 144
51a19bc0 145 $script = getcwd() . "/" . $script unless File::Spec->file_name_is_absolute($script);
8b88ae92 146
147 ($Script,$Bin) = fileparse($script);
148
a73990fd 149 # Resolve $script if it is a link
150 while(1)
151 {
152 my $linktext = readlink($script);
8b88ae92 153
154 ($RealScript,$RealBin) = fileparse($script);
a73990fd 155 last unless defined $linktext;
8b88ae92 156
51a19bc0 157 $script = (File::Spec->file_name_is_absolute($linktext))
a73990fd 158 ? $linktext
159 : $RealBin . "/" . $linktext;
160 }
161
162 # Get absolute paths to directories
163 $Bin = abs_path($Bin) if($Bin);
164 $RealBin = abs_path($RealBin) if($RealBin);
165 }
166 }
167}
168
1691; # Keep require happy
170