Commit | Line | Data |
---|---|---|
49624702 CS |
1 | package blib; |
2 | ||
3 | =head1 NAME | |
4 | ||
5 | blib - Use MakeMaker's uninstalled version of a package | |
6 | ||
7 | =head1 SYNOPSIS | |
8 | ||
9 | perl -Mblib script [args...] | |
10 | ||
11 | perl -Mblib=dir script [args...] | |
12 | ||
13 | =head1 DESCRIPTION | |
14 | ||
5230ff8e | 15 | Looks for MakeMaker-like I<'blib'> directory structure starting in |
49624702 CS |
16 | I<dir> (or current directory) and working back up to five levels of '..'. |
17 | ||
18 | Intended for use on command line with B<-M> option as a way of testing | |
3c4b39be | 19 | arbitrary scripts against an uninstalled version of a package. |
49624702 | 20 | |
5230ff8e | 21 | However it is possible to : |
49624702 | 22 | |
5230ff8e FR |
23 | use blib; |
24 | or | |
49624702 CS |
25 | use blib '..'; |
26 | ||
27 | etc. if you really must. | |
28 | ||
29 | =head1 BUGS | |
30 | ||
31 | Pollutes global name space for development only task. | |
32 | ||
33 | =head1 AUTHOR | |
34 | ||
35 | Nick Ing-Simmons nik@tiuk.ti.com | |
36 | ||
e69a2255 | 37 | =cut |
49624702 CS |
38 | |
39 | use Cwd; | |
e69a2255 | 40 | use File::Spec; |
49624702 | 41 | |
a635c943 | 42 | use vars qw($VERSION $Verbose); |
5230ff8e | 43 | $VERSION = '1.06'; |
a635c943 | 44 | $Verbose = 0; |
49624702 CS |
45 | |
46 | sub import | |
47 | { | |
48 | my $package = shift; | |
6c6463e2 | 49 | my $dir; |
c773137e | 50 | if ($^O eq "MSWin32" && -f "Win32.xs") { |
6c6463e2 JD |
51 | # We don't use getcwd() on Windows because it will internally |
52 | # call Win32::GetCwd(), which will get the Win32 module loaded. | |
53 | # That means that it would not be possible to run `make test` | |
54 | # for the Win32 module because blib.pm would always load the | |
55 | # installed version before @INC gets updated with the blib path. | |
56 | chomp($dir = `cd`); | |
57 | } | |
58 | else { | |
59 | $dir = getcwd; | |
60 | } | |
fe6f1558 | 61 | if ($^O eq 'VMS') { ($dir = VMS::Filespec::unixify($dir)) =~ s-/\z--; } |
49624702 CS |
62 | if (@_) |
63 | { | |
49624702 | 64 | $dir = shift; |
fe6f1558 GS |
65 | $dir =~ s/blib\z//; |
66 | $dir =~ s,/+\z,,; | |
e69a2255 | 67 | $dir = File::Spec->curdir unless ($dir); |
49624702 CS |
68 | die "$dir is not a directory\n" unless (-d $dir); |
69 | } | |
3f9bb6b0 DM |
70 | |
71 | # detaint: if the user asked for blib, s/he presumably knew | |
72 | # what s/he wanted | |
73 | $dir = $1 if $dir =~ /^(.*)$/; | |
74 | ||
e69a2255 JH |
75 | my $i = 5; |
76 | my($blib, $blib_lib, $blib_arch); | |
49624702 CS |
77 | while ($i--) |
78 | { | |
e69a2255 JH |
79 | $blib = File::Spec->catdir($dir, "blib"); |
80 | $blib_lib = File::Spec->catdir($blib, "lib"); | |
f64e7edf | 81 | $blib_arch = File::Spec->catdir($blib, "arch"); |
e69a2255 JH |
82 | |
83 | if (-d $blib && -d $blib_arch && -d $blib_lib) | |
49624702 | 84 | { |
e69a2255 | 85 | unshift(@INC,$blib_arch,$blib_lib); |
a635c943 | 86 | warn "Using $blib\n" if $Verbose; |
49624702 CS |
87 | return; |
88 | } | |
e69a2255 | 89 | $dir = File::Spec->catdir($dir, File::Spec->updir); |
49624702 CS |
90 | } |
91 | die "Cannot find blib even in $dir\n"; | |
92 | } | |
93 | ||
94 | 1; |