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 send bug
65             reports to EFE using the perlbug program
66             included with perl.
67              
68             Graham Barr EFE
69             Nick Ing-Simmons EFE
70              
71             =head1 COPYRIGHT
72              
73             Copyright (c) 1995 Graham Barr & Nick Ing-Simmons. All rights reserved.
74             This program is free software; you can redistribute it and/or modify it
75             under the same terms as Perl itself.
76              
77             =cut
78              
79             package FindBin;
80 1     1   505 use strict;
  1         2  
  1         28  
81 1     1   6 use warnings;
  1         2  
  1         22  
82              
83 1     1   4 use Carp;
  1         2  
  1         114  
84             require Exporter;
85 1     1   6 use Cwd qw(getcwd cwd abs_path);
  1         2  
  1         64  
86 1     1   6 use File::Basename;
  1         2  
  1         146  
87 1     1   14 use File::Spec;
  1         3  
  1         591  
88              
89             our ($Bin, $Script, $RealBin, $RealScript, $Dir, $RealDir);
90             our @EXPORT_OK = qw($Bin $Script $RealBin $RealScript $Dir $RealDir);
91             our %EXPORT_TAGS = (ALL => [qw($Bin $Script $RealBin $RealScript $Dir $RealDir)]);
92             our @ISA = qw(Exporter);
93              
94             our $VERSION = "1.52_01";
95              
96             # needed for VMS-specific filename translation
97             if( $^O eq 'VMS' ) {
98             require VMS::Filespec;
99             VMS::Filespec->import;
100             }
101              
102             sub cwd2 {
103 2     2 0 19 my $cwd = getcwd();
104             # getcwd might fail if it hasn't access to the current directory.
105             # try harder.
106 2 50       13 defined $cwd or $cwd = cwd();
107 2         24 $cwd;
108             }
109              
110             sub init
111             {
112 2     2 0 64 *Dir = \$Bin;
113 2         5 *RealDir = \$RealBin;
114              
115 2 100 66     18 if($0 eq '-e' || $0 eq '-')
116             {
117             # perl invoked with -e or script is on C
118 1         4 $Script = $RealScript = $0;
119 1         4 $Bin = $RealBin = cwd2();
120 1 50       7 $Bin = VMS::Filespec::unixify($Bin) if $^O eq 'VMS';
121             }
122             else
123             {
124 1         3 my $script = $0;
125              
126 1 50       12 if ($^O eq 'VMS')
127             {
128 0         0 ($Bin,$Script) = VMS::Filespec::rmsexpand($0) =~ /(.*[\]>\/]+)(.*)/s;
129             # C isn't going to work, so unixify first
130 0         0 ($Bin = VMS::Filespec::unixify($Bin)) =~ s/\/\z//;
131 0         0 ($RealBin,$RealScript) = ($Bin,$Script);
132             }
133             else
134             {
135 1 50       30 croak("Cannot find current script '$0'") unless(-f $script);
136              
137             # Ensure $script contains the complete path in case we C
138              
139 1 50       18 $script = File::Spec->catfile(cwd2(), $script)
140             unless File::Spec->file_name_is_absolute($script);
141              
142 1         47 ($Script,$Bin) = fileparse($script);
143              
144             # Resolve $script if it is a link
145 1         3 while(1)
146             {
147 1         12 my $linktext = readlink($script);
148              
149 1         15 ($RealScript,$RealBin) = fileparse($script);
150 1 50       12 last unless defined $linktext;
151              
152 0 0       0 $script = (File::Spec->file_name_is_absolute($linktext))
153             ? $linktext
154             : File::Spec->catfile($RealBin, $linktext);
155             }
156              
157             # Get absolute paths to directories
158 1 50       4 if ($Bin) {
159 1         2 my $BinOld = $Bin;
160 1         45 $Bin = abs_path($Bin);
161 1 50       4 defined $Bin or $Bin = File::Spec->canonpath($BinOld);
162             }
163 1 50       92 $RealBin = abs_path($RealBin) if($RealBin);
164             }
165             }
166             }
167              
168 1     1   5 BEGIN { init }
169              
170             *again = \&init;
171              
172             1; # Keep require happy