File Coverage

blib/lib/FindBin.pm
Criterion Covered Total %
statement 42 46 91.3
branch 11 22 50.0
condition 2 3 66.6
subroutine 9 9 100.0
pod 0 2 0.0
total 64 82 78.0


line stmt bran cond sub pod time code
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              
9             FindBin - Locate directory of original Perl script
10              
11             =head1 SYNOPSIS
12              
13             use FindBin;
14             use lib "$FindBin::Bin/../lib";
15              
16             use FindBin qw($Bin);
17             use lib "$Bin/../lib";
18              
19             =head1 DESCRIPTION
20              
21             Locates the full path to the script bin directory to allow the use
22             of paths relative to the bin directory.
23              
24             This allows a user to setup a directory tree for some software with
25             directories C<< /bin >> and C<< /lib >>, and then the above
26             example will allow the use of modules in the lib directory without knowing
27             where the software tree is installed.
28              
29             If C is invoked using the C<-e> option or the Perl script is read from
30             C, then C sets both C<$Bin> and C<$RealBin> to the current
31             directory.
32              
33             =head1 EXPORTABLE VARIABLES
34              
35             =over
36              
37             =item C<$Bin> or C<$Dir>
38              
39             Path to the bin B from where script was invoked
40              
41             =item C<$Script>
42              
43             B of the script from which C was invoked
44              
45             =item C<$RealBin> or C<$RealDir>
46              
47             C<$Bin> with all links resolved
48              
49             =item C<$RealScript>
50              
51             C<$Script> with all links resolved
52              
53             =back
54              
55             You can also use the C tag to export all of the above variables together:
56              
57             use FindBin ':ALL';
58              
59             =head1 KNOWN ISSUES
60              
61             If there are two modules using C from different directories
62             under the same interpreter, this won't work. Since C uses a
63             C block, it'll be executed only once, and only the first caller
64             will get it right. This is a problem under C and other persistent
65             Perl environments, where you shouldn't use this module. Which also means
66             that you should avoid using C in modules that you plan to put
67             on CPAN. Call the C function to make sure that C will work:
68              
69             use FindBin;
70             FindBin::again(); # or FindBin->again;
71              
72             In former versions of C there was no C function.
73             The workaround was to force the C block to be executed again:
74              
75             delete $INC{'FindBin.pm'};
76             require FindBin;
77              
78             =head1 AUTHORS
79              
80             C is supported as part of the core perl distribution. Please submit bug
81             reports at L.
82              
83             Graham Barr EFE
84             Nick Ing-Simmons EFE
85              
86             =head1 COPYRIGHT
87              
88             Copyright (c) 1995 Graham Barr & Nick Ing-Simmons. All rights reserved.
89             This program is free software; you can redistribute it and/or modify it
90             under the same terms as Perl itself.
91              
92             =cut
93              
94             package FindBin;
95 1     1   6736 use strict;
  1         1  
  1         71  
96 1     1   8 use warnings;
  1         2  
  1         67  
97              
98 1     1   8 use Carp;
  1         2  
  1         126  
99             require Exporter;
100 1     1   8 use Cwd qw(getcwd cwd abs_path);
  1         4  
  1         173  
101 1     1   9 use File::Basename;
  1         2  
  1         113  
102 1     1   7 use File::Spec;
  1         2  
  1         978  
103              
104             our ($Bin, $Script, $RealBin, $RealScript, $Dir, $RealDir);
105             our @EXPORT_OK = qw($Bin $Script $RealBin $RealScript $Dir $RealDir);
106             our %EXPORT_TAGS = (ALL => [qw($Bin $Script $RealBin $RealScript $Dir $RealDir)]);
107             our @ISA = qw(Exporter);
108              
109             our $VERSION = "1.54";
110              
111             # needed for VMS-specific filename translation
112             if( $^O eq 'VMS' ) {
113             require VMS::Filespec;
114             VMS::Filespec->import;
115             }
116              
117             sub cwd2 {
118 2     2 0 25 my $cwd = getcwd();
119             # getcwd might fail if it hasn't access to the current directory.
120             # try harder.
121 2 50       6 defined $cwd or $cwd = cwd();
122 2         27 $cwd;
123             }
124              
125             sub init
126             {
127 2     2 0 207394 *Dir = \$Bin;
128 2         5 *RealDir = \$RealBin;
129              
130 2 100 66     18 if($0 eq '-e' || $0 eq '-')
131             {
132             # perl invoked with -e or script is on C
133 1         4 $Script = $RealScript = $0;
134 1         5 $Bin = $RealBin = cwd2();
135 1 50       9 $Bin = VMS::Filespec::unixify($Bin) if $^O eq 'VMS';
136             }
137             else
138             {
139 1         15 my $script = $0;
140              
141 1 50       4 if ($^O eq 'VMS')
142             {
143 0         0 ($Bin,$Script) = VMS::Filespec::rmsexpand($0) =~ /(.*[\]>\/]+)(.*)/s;
144             # C isn't going to work, so unixify first
145 0         0 ($Bin = VMS::Filespec::unixify($Bin)) =~ s/\/\z//;
146 0         0 ($RealBin,$RealScript) = ($Bin,$Script);
147             }
148             else
149             {
150 1 50       28 croak("Cannot find current script '$0'") unless(-f $script);
151              
152             # Ensure $script contains the complete path in case we C
153              
154 1 50       12 $script = File::Spec->catfile(cwd2(), $script)
155             unless File::Spec->file_name_is_absolute($script);
156              
157 1         50 ($Script,$Bin) = fileparse($script);
158              
159             # Resolve $script if it is a link
160 1         3 while(1)
161             {
162 1         17 my $linktext = readlink($script);
163              
164 1         14 ($RealScript,$RealBin) = fileparse($script);
165 1 50       16 last unless defined $linktext;
166              
167 0 0       0 $script = (File::Spec->file_name_is_absolute($linktext))
168             ? $linktext
169             : File::Spec->catfile($RealBin, $linktext);
170             }
171              
172             # Get absolute paths to directories
173 1 50       4 if ($Bin) {
174 1         2 my $BinOld = $Bin;
175 1         46 $Bin = abs_path($Bin);
176 1 50       6 defined $Bin or $Bin = File::Spec->canonpath($BinOld);
177             }
178 1 50       125 $RealBin = abs_path($RealBin) if($RealBin);
179             }
180             }
181             }
182              
183 1     1   5 BEGIN { init }
184              
185             *again = \&init;
186              
187             1; # Keep require happy