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