| 1 | package BaseLib; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | ## $Id: BaseLib.pm,v 1.6 2002/10/23 12:04:31 hasant Exp $ | ||||||||||||||||||||||||||||||||||||||||||
| 4 | ## Manipulate @INC dynamically for independent installation path | ||||||||||||||||||||||||||||||||||||||||||
| 5 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 6 | ## Written by: Hasanuddin Tamir <hasant@trabas.com> | ||||||||||||||||||||||||||||||||||||||||||
| 7 | ## Copyright (C) 2000 Trabas. All rights reserved. | ||||||||||||||||||||||||||||||||||||||||||
| 8 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 9 | ## This program is free software; you may redistribute it | ||||||||||||||||||||||||||||||||||||||||||
| 10 | ## and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use File::Spec; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use File::Basename 'dirname'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use Cwd 'abs_path'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | $BaseLib::VERSION = '0.05'; | |||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 1 | ▶ | sub import { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=11 SCAR=37.0
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | shift; | |||||||||||||||||||||||||||||||||||||||||
| 21 | 1 | my($base, $lib, $glob) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | # the base directory name is now mandatory | ||||||||||||||||||||||||||||||||||||||||||
| 24 | 1 | ▶ | croak('No base directory supplied') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $base and $base ne ""
MC/DC0%0/2
defined $base and $base ne ""
defined $base
$base ne ""
Truth table33%1/3
A and B
A= defined $baseB= $base ne ""
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | unless defined $base && $base ne ''; | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 1 | my $dirname = abs_path(dirname $0); | |||||||||||||||||||||||||||||||||||||||||
| 28 | 1 | my @dirs = File::Spec->splitdir($dirname); | |||||||||||||||||||||||||||||||||||||||||
| 29 | 1 | ▶ | while (@dirs) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 7 | ▶ | last if $dirs[-1] eq $base; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 7 | pop @dirs; | |||||||||||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 1 | ▶ | @dirs || croak("Couldn't find the base `$base' in `$dirname'"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | my $basedir = File::Spec->catdir(@dirs); | |||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | ▶ | $lib = File::Spec->catdir(qw(lib perl)) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
defined $lib and $lib ne "" and $lib ne "-"
defined $lib and $lib ne ""
MC/DC0%0/3
defined $lib and $lib ne "" and $lib ne "-"
defined $lib
$lib ne ""
$lib ne "-"
Truth table0%0/4
A and B and C
A= defined $libB= $lib ne ""C= $lib ne "-"
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | unless defined $lib and $lib ne '' and $lib ne '-'; | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | my $libdir = File::Spec->catdir($basedir, $lib); | |||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | # ask lib.pm to finish the job :-) well, assummed it's not loaded yet | ||||||||||||||||||||||||||||||||||||||||||
| 42 | # FIXME: is it necessary to check this? | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | my %inc = map { $_ => 1 } @INC; | |||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | unless ($inc{$libdir}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | require lib; | |||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | lib::import('lib', $libdir); | |||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | # give them if they want it | ||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | if ($glob) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | my $pack = caller; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | ${"$pack\:\:$glob"} = $basedir; | |||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | $BaseLib::BaseDir = $basedir; | |||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | # private routine | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 1 | ▶ | sub croak { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
croak: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 1 | require Carp; | |||||||||||||||||||||||||||||||||||||||||
| 63 | 1 | Carp::croak(__PACKAGE__, ": @_"); | |||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | BaseLib - manipulate @INC dynamically for independent installation path | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | use BaseLib qw(BASEDIR LIBDIR); | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | or just, | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | use BaseLib; | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | If you have a set of application with certain file structure under a | ||||||||||||||||||||||||||||||||||||||||||
| 86 | base directory, e.g. C<pim>, and it has a private modules directory | ||||||||||||||||||||||||||||||||||||||||||
| 87 | called C<lib-perl>, and it's intalled under C</usr/local>. For the | ||||||||||||||||||||||||||||||||||||||||||
| 88 | application to work, the scripts may need to say: | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | use lib '/usr/local/pim/lib-perl'; | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | Someday, you need to move the application to, say, C</vhost/www.host.com>, | ||||||||||||||||||||||||||||||||||||||||||
| 93 | so you need to change the use statement all over the scripts to: | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | use lib '/vhost/www.some.host.com/pim/lib-perl'; | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | If you do this a lot and get tired of changing the line for every | ||||||||||||||||||||||||||||||||||||||||||
| 98 | different installation path in every script, or, you don't want to | ||||||||||||||||||||||||||||||||||||||||||
| 99 | bother people using your application to change the line to meet their | ||||||||||||||||||||||||||||||||||||||||||
| 100 | conditions, then you might need this module. | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | Now your scripts can say: | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | use BaseLib qw(pim lib-perl); | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | where C<pim> and C<lib-perl> are the BASEDIR and LIBDIR arguments, | ||||||||||||||||||||||||||||||||||||||||||
| 107 | respectively. | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | So there's no need to worry about different installation layout, wherever | ||||||||||||||||||||||||||||||||||||||||||
| 110 | the C<pim> base directory is put under. The BASEDIR argument is mandatory | ||||||||||||||||||||||||||||||||||||||||||
| 111 | while LIBDIR is optional (defaults to C<lib/perl>). | ||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | As addition, you can use B<BaseLib>'s global package variable, | ||||||||||||||||||||||||||||||||||||||||||
| 114 | B<$BaseDir> to refer to the full path to C<pim>. For example, | ||||||||||||||||||||||||||||||||||||||||||
| 115 | assuming the application is installed under C</usr/local>, then | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | print "Data dir: $BaseLib::BaseDir/data\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | will print, | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | Data dir: /usr/local/pim/data | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | You have to use the fully qualified package name since it's not | ||||||||||||||||||||||||||||||||||||||||||
| 124 | exportable. | ||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | You can use your own global variable by supplying the third argument | ||||||||||||||||||||||||||||||||||||||||||
| 127 | in the use statement. For example, | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | use BaseLib qw(pim lib-perl MyOwnBaseDir); | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | So saying, | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | print "Data dir: $MyOwnBaseDir/data\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | will result the same thing. If you use 'use strict;', which is a good | ||||||||||||||||||||||||||||||||||||||||||
| 136 | thing, you need to explicitly declare it as global variable. Either, | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | use vars '$MyOwnBaseDir'; | ||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | or, if you use version 5.6 or later, | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | our $MyOwnBaseDir; | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | If you want to use the default LIBDIR (C<lib/perl>), you can indicate | ||||||||||||||||||||||||||||||||||||||||||
| 145 | so by using dash ("-"). | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | use BaseLib qw(pim - MyOwnBaseDir); | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | Or, just give an empty string, | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | use BaseLib ('pim', '', 'MyOwnBaseDir'); | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | =head1 NOTES | ||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | I rewrote the implementation to make this module more reusable in | ||||||||||||||||||||||||||||||||||||||||||
| 157 | different environment. No path resolving is hardcoded. Unfortunately, I | ||||||||||||||||||||||||||||||||||||||||||
| 158 | have no any chance to test it. | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | B<BaseLib> will find the last occurence of I<BASEDIR> string. If you mean | ||||||||||||||||||||||||||||||||||||||||||
| 161 | the base directory as C</usr/local/myapp>, while the script uses the module | ||||||||||||||||||||||||||||||||||||||||||
| 162 | locates in C</usr/local/myapp/bin/sample/myapp/test/script.pl>, then the | ||||||||||||||||||||||||||||||||||||||||||
| 163 | full path to the application base directory ends in | ||||||||||||||||||||||||||||||||||||||||||
| 164 | C</usr/local/myapp/bin/sample/myapp>. | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | Any improvements/suggestions for wider support are welcome. A simple | ||||||||||||||||||||||||||||||||||||||||||
| 167 | comment on this module will do as well. | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | This module is written by Hasanuddin Tamir E<lt>hasant@trabas.comE<gt>. | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | Copyright (C) 2000 B<Trabas>. All rights reserved. | ||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | This program is free software; you can redistribute it | ||||||||||||||||||||||||||||||||||||||||||
| 176 | and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | L<lib>, L<FindBin> | ||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | =cut | ||||||||||||||||||||||||||||||||||||||||||