Wrap the macro arguments for ck_proto in ().
[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
34da6cec 27directories C<< <root>/bin >> and C<< <root>/lib >>, and then the above
28example will allow the use of modules in the lib directory without knowing
29where the software tree is installed.
a73990fd 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
11cd4567 42=head1 KNOWN ISSUES
43
44If there are two modules using C<FindBin> from different directories
f5094128 45under the same interpreter, this won't work. Since C<FindBin> uses a
11cd4567 46C<BEGIN> block, it'll be executed only once, and only the first caller
47will get it right. This is a problem under mod_perl and other persistent
48Perl environments, where you shouldn't use this module. Which also means
49that you should avoid using C<FindBin> in modules that you plan to put
f5094128 50on CPAN. To make sure that C<FindBin> will work is to call the C<again>
51function:
52
53 use FindBin;
54 FindBin::again(); # or FindBin->again;
55
56In former versions of FindBin there was no C<again> function. The
57workaround was to force the C<BEGIN> block to be executed again:
11cd4567 58
59 delete $INC{'FindBin.pm'};
60 require FindBin;
61
a73990fd 62=head1 KNOWN BUGS
63
11cd4567 64If perl is invoked as
a73990fd 65
66 perl filename
67
34da6cec 68and I<filename> does not have executable rights and a program called
69I<filename> exists in the users C<$ENV{PATH}> which satisfies both B<-x>
70and B<-T> then FindBin assumes that it was invoked via the
71C<$ENV{PATH}>.
a73990fd 72
73Workaround is to invoke perl as
74
75 perl ./filename
76
77=head1 AUTHORS
78
d250f4d1 79FindBin is supported as part of the core perl distribution. Please send bug
34da6cec 80reports to E<lt>F<perlbug@perl.org>E<gt> using the perlbug program
81included with perl.
d250f4d1 82
83Graham Barr E<lt>F<gbarr@pobox.com>E<gt>
1fef88e7 84Nick Ing-Simmons E<lt>F<nik@tiuk.ti.com>E<gt>
a73990fd 85
86=head1 COPYRIGHT
87
88Copyright (c) 1995 Graham Barr & Nick Ing-Simmons. All rights reserved.
89This program is free software; you can redistribute it and/or modify it
90under the same terms as Perl itself.
91
a73990fd 92=cut
93
94package FindBin;
95use Carp;
96require 5.000;
97require Exporter;
7d161605 98use Cwd qw(getcwd cwd abs_path);
8b88ae92 99use Config;
100use File::Basename;
d250f4d1 101use File::Spec;
a73990fd 102
103@EXPORT_OK = qw($Bin $Script $RealBin $RealScript $Dir $RealDir);
104%EXPORT_TAGS = (ALL => [qw($Bin $Script $RealBin $RealScript $Dir $RealDir)]);
105@ISA = qw(Exporter);
106
af08d52d 107$VERSION = "1.48";
34da6cec 108
109sub cwd2 {
110 my $cwd = getcwd();
111 # getcwd might fail if it hasn't access to the current directory.
112 # try harder.
113 defined $cwd or $cwd = cwd();
114 $cwd;
115}
a73990fd 116
f5094128 117sub init
a73990fd 118{
119 *Dir = \$Bin;
120 *RealDir = \$RealBin;
121
122 if($0 eq '-e' || $0 eq '-')
123 {
124 # perl invoked with -e or script is on C<STDIN>
a73990fd 125 $Script = $RealScript = $0;
34da6cec 126 $Bin = $RealBin = cwd2();
a73990fd 127 }
128 else
129 {
130 my $script = $0;
131
132 if ($^O eq 'VMS')
133 {
4f44ac69 134 ($Bin,$Script) = VMS::Filespec::rmsexpand($0) =~ /(.*\])(.*)/s;
a73990fd 135 ($RealBin,$RealScript) = ($Bin,$Script);
136 }
137 else
138 {
386d1046 139 my $dosish = ($^O eq 'MSWin32' or $^O eq 'os2');
f5f423e4 140 unless(($script =~ m#/# || ($dosish && $script =~ m#\\#))
8b88ae92 141 && -f $script)
a73990fd 142 {
143 my $dir;
d250f4d1 144 foreach $dir (File::Spec->path)
af08d52d 145 {
d250f4d1 146 my $scr = File::Spec->catfile($dir, $script);
af08d52d 147
148 # $script can been found via PATH but perl could have
149 # been invoked as 'perl file'. Do a dumb check to see
150 # if $script is a perl program, if not then keep $script = $0
151 #
152 # well we actually only check that it is an ASCII file
153 # we know its executable so it is probably a script
154 # of some sort.
155 if(-f $scr && -r _ && ($dosish || -x _) && -s _ && -T _)
a73990fd 156 {
d250f4d1 157 $script = $scr;
a73990fd 158 last;
159 }
160 }
161 }
8b88ae92 162
a73990fd 163 croak("Cannot find current script '$0'") unless(-f $script);
8b88ae92 164
7d161605 165 # Ensure $script contains the complete path in case we C<chdir>
8b88ae92 166
34da6cec 167 $script = File::Spec->catfile(cwd2(), $script)
d250f4d1 168 unless File::Spec->file_name_is_absolute($script);
8b88ae92 169
170 ($Script,$Bin) = fileparse($script);
171
a73990fd 172 # Resolve $script if it is a link
173 while(1)
174 {
175 my $linktext = readlink($script);
8b88ae92 176
177 ($RealScript,$RealBin) = fileparse($script);
a73990fd 178 last unless defined $linktext;
8b88ae92 179
51a19bc0 180 $script = (File::Spec->file_name_is_absolute($linktext))
a73990fd 181 ? $linktext
d250f4d1 182 : File::Spec->catfile($RealBin, $linktext);
a73990fd 183 }
184
185 # Get absolute paths to directories
942068f5 186 if ($Bin) {
187 my $BinOld = $Bin;
188 $Bin = abs_path($Bin);
189 defined $Bin or $Bin = File::Spec->canonpath($BinOld);
190 }
a73990fd 191 $RealBin = abs_path($RealBin) if($RealBin);
192 }
193 }
194}
195
f5094128 196BEGIN { init }
197
198*again = \&init;
199
a73990fd 2001; # Keep require happy