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.
9 FindBin - Locate directory of original perl script
14 use lib "$FindBin::Bin/../lib";
19 use lib "$Bin/../lib";
23 Locates the full path to the script bin directory to allow the use
24 of paths relative to the bin directory.
26 This allows a user to setup a directory tree for some software with
27 directories E<lt>rootE<gt>/bin and E<lt>rootE<gt>/lib and then the above example will allow
28 the use of modules in the lib directory without knowing where the software
31 If perl is invoked using the B<-e> option or the perl script is read from
32 C<STDIN> then FindBin sets both C<$Bin> and C<$RealBin> to the current
35 =head1 EXPORTABLE VARIABLES
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
44 If there are two modules using C<FindBin> from different directories
45 under the same interpreter, this won't work. Since C<FindBin> uses
46 C<BEGIN> block, it'll be executed only once, and only the first caller
47 will get it right. This is a problem under mod_perl and other persistent
48 Perl environments, where you shouldn't use this module. Which also means
49 that you should avoid using C<FindBin> in modules that you plan to put
50 on CPAN. The only way to make sure that C<FindBin> will work is to force
51 the C<BEGIN> block to be executed again:
53 delete $INC{'FindBin.pm'};
62 and I<filename> does not have executable rights and a program called I<filename>
63 exists in the users C<$ENV{PATH}> which satisfies both B<-x> and B<-T> then FindBin
64 assumes that it was invoked via the C<$ENV{PATH}>.
66 Workaround is to invoke perl as
72 FindBin is supported as part of the core perl distribution. Please send bug
73 reports to E<lt>F<perlbug@perl.org>E<gt> using the perlbug program included with perl.
75 Graham Barr E<lt>F<gbarr@pobox.com>E<gt>
76 Nick Ing-Simmons E<lt>F<nik@tiuk.ti.com>E<gt>
80 Copyright (c) 1995 Graham Barr & Nick Ing-Simmons. All rights reserved.
81 This program is free software; you can redistribute it and/or modify it
82 under the same terms as Perl itself.
90 use Cwd qw(getcwd abs_path);
95 @EXPORT_OK = qw($Bin $Script $RealBin $RealScript $Dir $RealDir);
96 %EXPORT_TAGS = (ALL => [qw($Bin $Script $RealBin $RealScript $Dir $RealDir)]);
104 *RealDir = \$RealBin;
106 if($0 eq '-e' || $0 eq '-')
108 # perl invoked with -e or script is on C<STDIN>
110 $Script = $RealScript = $0;
111 $Bin = $RealBin = getcwd();
119 ($Bin,$Script) = VMS::Filespec::rmsexpand($0) =~ /(.*\])(.*)/s;
120 ($RealBin,$RealScript) = ($Bin,$Script);
124 my $dosish = ($^O eq 'MSWin32' or $^O eq 'os2');
125 unless(($script =~ m#/# || ($dosish && $script =~ m#\\#))
129 foreach $dir (File::Spec->path)
131 my $scr = File::Spec->catfile($dir, $script);
132 if(-r $scr && (!$dosish || -x _))
138 # $script has been found via PATH but perl could have
139 # been invoked as 'perl file'. Do a dumb check to see
140 # if $script is a perl program, if not then $script = $0
142 # well we actually only check that it is an ASCII file
143 # we know its executable so it is probably a script
146 $script = $0 unless(-T $script);
153 croak("Cannot find current script '$0'") unless(-f $script);
155 # Ensure $script contains the complete path incase we C<chdir>
157 $script = File::Spec->catfile(getcwd(), $script)
158 unless File::Spec->file_name_is_absolute($script);
160 ($Script,$Bin) = fileparse($script);
162 # Resolve $script if it is a link
165 my $linktext = readlink($script);
167 ($RealScript,$RealBin) = fileparse($script);
168 last unless defined $linktext;
170 $script = (File::Spec->file_name_is_absolute($linktext))
172 : File::Spec->catfile($RealBin, $linktext);
175 # Get absolute paths to directories
176 $Bin = abs_path($Bin) if($Bin);
177 $RealBin = abs_path($RealBin) if($RealBin);
182 1; # Keep require happy