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