Filename | /usr/share/perl/5.20/FindBin.pm |
Statements | Executed 38 statements in 3.74ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 511µs | 511µs | CORE:readlink (opcode) | FindBin::
1 | 1 | 1 | 378µs | 378µs | CORE:ftfile (opcode) | FindBin::
1 | 1 | 1 | 68µs | 3.22ms | init | FindBin::
1 | 1 | 1 | 15µs | 60µs | BEGIN@80 | FindBin::
1 | 1 | 1 | 9µs | 37µs | BEGIN@84 | FindBin::
1 | 1 | 1 | 8µs | 34µs | BEGIN@83 | FindBin::
1 | 1 | 1 | 6µs | 3.22ms | BEGIN@166 | FindBin::
1 | 1 | 1 | 6µs | 6µs | BEGIN@85 | FindBin::
1 | 1 | 1 | 5µs | 15µs | cwd2 | FindBin::
Line | State ments |
Time on line |
Calls | Time in subs |
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<< <root>/bin >> and C<< <root>/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<STDIN> 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<FindBin> from different directories | ||||
45 | under the same interpreter, this won't work. Since C<FindBin> uses a | ||||
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. To make sure that C<FindBin> will work is to call the C<again> | ||||
51 | function: | ||||
52 | |||||
53 | use FindBin; | ||||
54 | FindBin::again(); # or FindBin->again; | ||||
55 | |||||
56 | In former versions of FindBin there was no C<again> function. The | ||||
57 | workaround was to force the C<BEGIN> 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 E<lt>F<perlbug@perl.org>E<gt> using the perlbug program | ||||
66 | included with perl. | ||||
67 | |||||
68 | Graham Barr E<lt>F<gbarr@pobox.com>E<gt> | ||||
69 | Nick Ing-Simmons E<lt>F<nik@tiuk.ti.com>E<gt> | ||||
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 | 2 | 39µs | 2 | 105µs | # spent 60µs (15+45) within FindBin::BEGIN@80 which was called:
# once (15µs+45µs) by C4::Auth_with_cas::BEGIN@27 at line 80 # spent 60µs making 1 call to FindBin::BEGIN@80
# spent 45µs making 1 call to Exporter::import |
81 | 1 | 8µs | require 5.000; | ||
82 | 1 | 500ns | require Exporter; | ||
83 | 2 | 23µs | 2 | 60µs | # spent 34µs (8+26) within FindBin::BEGIN@83 which was called:
# once (8µs+26µs) by C4::Auth_with_cas::BEGIN@27 at line 83 # spent 34µs making 1 call to FindBin::BEGIN@83
# spent 26µs making 1 call to Exporter::import |
84 | 2 | 22µs | 2 | 65µs | # spent 37µs (9+28) within FindBin::BEGIN@84 which was called:
# once (9µs+28µs) by C4::Auth_with_cas::BEGIN@27 at line 84 # spent 37µs making 1 call to FindBin::BEGIN@84
# spent 28µs making 1 call to Exporter::import |
85 | 2 | 441µs | 1 | 6µs | # spent 6µs within FindBin::BEGIN@85 which was called:
# once (6µs+0s) by C4::Auth_with_cas::BEGIN@27 at line 85 # spent 6µs making 1 call to FindBin::BEGIN@85 |
86 | |||||
87 | 1 | 1µs | @EXPORT_OK = qw($Bin $Script $RealBin $RealScript $Dir $RealDir); | ||
88 | 1 | 2µs | %EXPORT_TAGS = (ALL => [qw($Bin $Script $RealBin $RealScript $Dir $RealDir)]); | ||
89 | 1 | 5µs | @ISA = qw(Exporter); | ||
90 | |||||
91 | 1 | 100ns | $VERSION = "1.51"; | ||
92 | |||||
93 | |||||
94 | # needed for VMS-specific filename translation | ||||
95 | 1 | 1µs | if( $^O eq 'VMS' ) { | ||
96 | require VMS::Filespec; | ||||
97 | VMS::Filespec->import; | ||||
98 | } | ||||
99 | |||||
100 | # spent 15µs (5+10) within FindBin::cwd2 which was called:
# once (5µs+10µs) by FindBin::init at line 137 | ||||
101 | 1 | 14µs | 1 | 10µs | my $cwd = getcwd(); # spent 10µs making 1 call to Cwd::getcwd |
102 | # getcwd might fail if it hasn't access to the current directory. | ||||
103 | # try harder. | ||||
104 | 1 | 200ns | defined $cwd or $cwd = cwd(); | ||
105 | 1 | 2µs | $cwd; | ||
106 | } | ||||
107 | |||||
108 | sub init | ||||
109 | # spent 3.22ms (68µs+3.15) within FindBin::init which was called:
# once (68µs+3.15ms) by FindBin::BEGIN@166 at line 166 | ||||
110 | 1 | 700ns | *Dir = \$Bin; | ||
111 | 1 | 200ns | *RealDir = \$RealBin; | ||
112 | |||||
113 | 1 | 4µs | if($0 eq '-e' || $0 eq '-') | ||
114 | { | ||||
115 | # perl invoked with -e or script is on C<STDIN> | ||||
116 | $Script = $RealScript = $0; | ||||
117 | $Bin = $RealBin = cwd2(); | ||||
118 | $Bin = VMS::Filespec::unixify($Bin) if $^O eq 'VMS'; | ||||
119 | } | ||||
120 | else | ||||
121 | { | ||||
122 | 1 | 500ns | my $script = $0; | ||
123 | |||||
124 | 1 | 1µs | if ($^O eq 'VMS') | ||
125 | { | ||||
126 | ($Bin,$Script) = VMS::Filespec::rmsexpand($0) =~ /(.*[\]>\/]+)(.*)/s; | ||||
127 | # C<use disk:[dev]/lib> isn't going to work, so unixify first | ||||
128 | ($Bin = VMS::Filespec::unixify($Bin)) =~ s/\/\z//; | ||||
129 | ($RealBin,$RealScript) = ($Bin,$Script); | ||||
130 | } | ||||
131 | else | ||||
132 | { | ||||
133 | 1 | 387µs | 1 | 378µs | croak("Cannot find current script '$0'") unless(-f $script); # spent 378µs making 1 call to FindBin::CORE:ftfile |
134 | |||||
135 | # Ensure $script contains the complete path in case we C<chdir> | ||||
136 | |||||
137 | 1 | 24µs | 6 | 43µs | $script = File::Spec->catfile(cwd2(), $script) # spent 15µs making 1 call to FindBin::cwd2
# spent 13µs making 1 call to File::Spec::Unix::catfile
# spent 9µs making 1 call to File::Spec::Unix::file_name_is_absolute
# spent 4µs making 1 call to File::Spec::Unix::catdir
# spent 2µs making 2 calls to File::Spec::Unix::canonpath, avg 900ns/call |
138 | unless File::Spec->file_name_is_absolute($script); | ||||
139 | |||||
140 | 1 | 2µs | 1 | 37µs | ($Script,$Bin) = fileparse($script); # spent 37µs making 1 call to File::Basename::fileparse |
141 | |||||
142 | # Resolve $script if it is a link | ||||
143 | 1 | 200ns | while(1) | ||
144 | { | ||||
145 | 1 | 520µs | 1 | 511µs | my $linktext = readlink($script); # spent 511µs making 1 call to FindBin::CORE:readlink |
146 | |||||
147 | 1 | 2µs | 1 | 14µs | ($RealScript,$RealBin) = fileparse($script); # spent 14µs making 1 call to File::Basename::fileparse |
148 | 1 | 900ns | last unless defined $linktext; | ||
149 | |||||
150 | $script = (File::Spec->file_name_is_absolute($linktext)) | ||||
151 | ? $linktext | ||||
152 | : File::Spec->catfile($RealBin, $linktext); | ||||
153 | } | ||||
154 | |||||
155 | # Get absolute paths to directories | ||||
156 | 1 | 300ns | if ($Bin) { | ||
157 | 1 | 100ns | my $BinOld = $Bin; | ||
158 | 1 | 1.14ms | 1 | 1.13ms | $Bin = abs_path($Bin); # spent 1.13ms making 1 call to Cwd::abs_path |
159 | 1 | 600ns | defined $Bin or $Bin = File::Spec->canonpath($BinOld); | ||
160 | } | ||||
161 | 1 | 1.05ms | 1 | 1.04ms | $RealBin = abs_path($RealBin) if($RealBin); # spent 1.04ms making 1 call to Cwd::abs_path |
162 | } | ||||
163 | } | ||||
164 | } | ||||
165 | |||||
166 | 1 | 38µs | 2 | 6.44ms | # spent 3.22ms (6µs+3.22) within FindBin::BEGIN@166 which was called:
# once (6µs+3.22ms) by C4::Auth_with_cas::BEGIN@27 at line 166 # spent 3.22ms making 1 call to FindBin::BEGIN@166
# spent 3.22ms making 1 call to FindBin::init |
167 | |||||
168 | 1 | 900ns | *again = \&init; | ||
169 | |||||
170 | 1 | 5µs | 1; # Keep require happy | ||
# spent 378µs within FindBin::CORE:ftfile which was called:
# once (378µs+0s) by FindBin::init at line 133 | |||||
# spent 511µs within FindBin::CORE:readlink which was called:
# once (511µs+0s) by FindBin::init at line 145 |