| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | ######### | 
| 2 |  |  |  |  |  |  | # Author:        rmp | 
| 3 |  |  |  |  |  |  | # Maintainer:    rmp | 
| 4 |  |  |  |  |  |  | # Created:       2005-02-14 | 
| 5 |  |  |  |  |  |  | # Last Modified: $Date: 2008-02-28$ | 
| 6 |  |  |  |  |  |  | # Id:            $Id$ | 
| 7 |  |  |  |  |  |  | # Source:        $Source$ | 
| 8 |  |  |  |  |  |  | # $HeadURL$ | 
| 9 |  |  |  |  |  |  | # | 
| 10 |  |  |  |  |  |  | package Module::PortablePath; | 
| 11 | 1 |  |  | 1 |  | 747 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 36 |  | 
| 12 | 1 |  |  | 1 |  | 7 | use warnings; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 36 |  | 
| 13 | 1 |  |  | 1 |  | 945 | use Sys::Hostname; | 
|  | 1 |  |  |  |  | 1180 |  | 
|  | 1 |  |  |  |  | 51 |  | 
| 14 | 1 |  |  | 1 |  | 1149 | use Config::IniFiles; | 
|  | 1 |  |  |  |  | 33831 |  | 
|  | 1 |  |  |  |  | 34 |  | 
| 15 | 1 |  |  | 1 |  | 11 | use Carp; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 50 |  | 
| 16 | 1 |  |  | 1 |  | 828 | use English qw(-no_match_vars); | 
|  | 1 |  |  |  |  | 2103 |  | 
|  | 1 |  |  |  |  | 5 |  | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | our $VERSION = q[0.17]; | 
| 19 |  |  |  |  |  |  | our $CONFIGS = { | 
| 20 |  |  |  |  |  |  | default => map { m{([[:lower:][:digit:]_./]+)}smix } ($ENV{MODULE_PORTABLEPATH_CONF} || q[/etc/perlconfig.ini]), | 
| 21 |  |  |  |  |  |  | }; | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | sub config { | 
| 24 | 0 |  |  | 0 | 1 | 0 | my $cfgfile  = $CONFIGS->{'default'}; | 
| 25 | 0 |  | 0 |  |  | 0 | my $hostname = hostname() || q[]; | 
| 26 |  |  |  |  |  |  |  | 
| 27 | 0 |  |  |  |  | 0 | for my $k (sort { length $a <=> length $b } keys %{$CONFIGS}) { | 
|  | 0 |  |  |  |  | 0 |  | 
|  | 0 |  |  |  |  | 0 |  | 
| 28 | 0 | 0 |  |  |  | 0 | if($hostname =~ /$k/smx) { | 
| 29 | 0 |  |  |  |  | 0 | $cfgfile = $CONFIGS->{$k}; | 
| 30 | 0 |  |  |  |  | 0 | last; | 
| 31 |  |  |  |  |  |  | } | 
| 32 |  |  |  |  |  |  | } | 
| 33 |  |  |  |  |  |  |  | 
| 34 | 0 |  |  |  |  | 0 | my $config; | 
| 35 | 0 | 0 |  |  |  | 0 | if(-f $cfgfile) { | 
| 36 | 0 |  |  |  |  | 0 | $config = Config::IniFiles->new( | 
| 37 |  |  |  |  |  |  | -file => $cfgfile, | 
| 38 |  |  |  |  |  |  | ); | 
| 39 |  |  |  |  |  |  | } else { | 
| 40 | 0 |  |  |  |  | 0 | $config = Config::IniFiles->new(); | 
| 41 |  |  |  |  |  |  | } | 
| 42 |  |  |  |  |  |  |  | 
| 43 | 0 |  |  |  |  | 0 | return $config; | 
| 44 |  |  |  |  |  |  | } | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | sub import { | 
| 47 | 1 |  |  | 1 |  | 12 | my ($pkg, @args) = @_; | 
| 48 | 1 | 50 |  |  |  | 3 | if(!scalar @args) { | 
| 49 | 1 |  |  |  |  | 12 | return; | 
| 50 |  |  |  |  |  |  | } | 
| 51 |  |  |  |  |  |  |  | 
| 52 | 0 |  |  |  |  |  | my $config = config(); | 
| 53 | 0 |  |  |  |  |  | $pkg->_import_libs($config, @args); | 
| 54 | 0 |  |  |  |  |  | $pkg->_import_ldlibs($config, @args); | 
| 55 |  |  |  |  |  |  |  | 
| 56 | 0 |  |  |  |  |  | return; | 
| 57 |  |  |  |  |  |  | } | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | sub _import_libs { | 
| 60 | 0 |  |  | 0 |  |  | my ($pkg, $config, @args) = @_; | 
| 61 | 0 |  |  |  |  |  | my $forward      = {}; | 
| 62 | 0 |  |  |  |  |  | my $reverse      = {}; | 
| 63 |  |  |  |  |  |  |  | 
| 64 | 0 |  |  |  |  |  | for my $param ($config->Parameters('libs')) { | 
| 65 | 0 |  | 0 |  |  |  | for my $v (split /[,\s;:]+/smx, $config->val('libs', $param)||q[]) { | 
| 66 | 0 |  |  |  |  |  | $reverse->{$v} = $param; | 
| 67 | 0 |  |  |  |  |  | unshift @{$forward->{$param}}, $v; | 
|  | 0 |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  |  | 
| 71 | 0 |  |  |  |  |  | my $seentags = {}; | 
| 72 | 0 |  |  |  |  |  | for my $i (@INC) { | 
| 73 | 0 | 0 |  |  |  |  | if(!$reverse->{$i}) { | 
| 74 | 0 |  |  |  |  |  | next; | 
| 75 |  |  |  |  |  |  | } | 
| 76 |  |  |  |  |  |  |  | 
| 77 | 0 |  |  |  |  |  | my ($tag) = $reverse->{$i} =~ /([[:lower:]]+)/smx; | 
| 78 | 0 |  |  |  |  |  | $seentags->{$tag} = $reverse->{$i}; | 
| 79 |  |  |  |  |  |  | } | 
| 80 |  |  |  |  |  |  |  | 
| 81 | 0 |  |  |  |  |  | for my $a (@args) { | 
| 82 | 0 | 0 |  |  |  |  | if(!$forward->{$a}) { | 
| 83 | 0 |  |  |  |  |  | carp qq[Use of unknown tag "$a"]; | 
| 84 |  |  |  |  |  |  | } | 
| 85 | 0 |  |  |  |  |  | for my $i (@{$forward->{$a}}) { | 
|  | 0 |  |  |  |  |  |  | 
| 86 | 0 |  |  |  |  |  | my ($tag) = $reverse->{$i} =~ /([[:lower:]]+)/smx; | 
| 87 | 0 | 0 | 0 |  |  |  | if($seentags->{$tag} && ($seentags->{$tag} ne $reverse->{$i})) { | 
| 88 | 0 |  |  |  |  |  | carp qq[Import of tag "$a" may clash with tag "$seentags->{$tag}"]; | 
| 89 |  |  |  |  |  |  | } | 
| 90 | 0 |  |  |  |  |  | unshift @INC, $i; | 
| 91 |  |  |  |  |  |  | } | 
| 92 |  |  |  |  |  |  | } | 
| 93 | 0 |  |  |  |  |  | return; | 
| 94 |  |  |  |  |  |  | } | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | sub _import_ldlibs { | 
| 97 | 0 |  |  | 0 |  |  | my ($pkg, $config, @args) = @_; | 
| 98 | 0 |  |  |  |  |  | my $forward      = {}; | 
| 99 | 0 |  |  |  |  |  | my $reverse      = {}; | 
| 100 | 0 |  | 0 |  |  |  | my @LDLIBS       = split /:/smx, $ENV{LD_LIBRARY_PATH}||q[]; | 
| 101 |  |  |  |  |  |  |  | 
| 102 | 0 |  |  |  |  |  | for my $param ($config->Parameters('ldlibs')) { | 
| 103 | 0 |  | 0 |  |  |  | for my $v (split /[,\s;:]+/smx, $config->val('ldlibs', $param) || q[]) { | 
| 104 | 0 |  |  |  |  |  | $reverse->{$v} = $param; | 
| 105 | 0 |  |  |  |  |  | unshift @{$forward->{$param}}, $v; | 
|  | 0 |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | } | 
| 107 |  |  |  |  |  |  | } | 
| 108 |  |  |  |  |  |  |  | 
| 109 | 0 |  |  |  |  |  | my $seentags = {}; | 
| 110 | 0 |  |  |  |  |  | for my $i (@LDLIBS) { | 
| 111 | 0 | 0 |  |  |  |  | if(!$reverse->{$i}) { | 
| 112 | 0 |  |  |  |  |  | next; | 
| 113 |  |  |  |  |  |  | } | 
| 114 | 0 |  |  |  |  |  | my ($tag) = $reverse->{$i} =~ /([[:lower:]]+)/smx; | 
| 115 | 0 |  |  |  |  |  | $seentags->{$tag} = $reverse->{$i}; | 
| 116 |  |  |  |  |  |  | } | 
| 117 |  |  |  |  |  |  |  | 
| 118 | 0 |  |  |  |  |  | for my $a (@args) { | 
| 119 | 0 | 0 |  |  |  |  | if(!$forward->{$a}) { | 
| 120 | 0 |  |  |  |  |  | next; | 
| 121 |  |  |  |  |  |  | } | 
| 122 |  |  |  |  |  |  |  | 
| 123 | 0 |  |  |  |  |  | for my $i (@{$forward->{$a}}) { | 
|  | 0 |  |  |  |  |  |  | 
| 124 | 0 |  |  |  |  |  | my ($tag) = $reverse->{$i} =~ /([[:lower:]]+)/smx; | 
| 125 | 0 | 0 | 0 |  |  |  | if($seentags->{$tag} && ($seentags->{$tag} ne $reverse->{$i})) { | 
| 126 | 0 |  |  |  |  |  | carp qq[Import of tag "$a" may clash with tag "$seentags->{$tag}"]; | 
| 127 |  |  |  |  |  |  | } | 
| 128 | 0 |  |  |  |  |  | unshift @LDLIBS, $i; | 
| 129 |  |  |  |  |  |  | } | 
| 130 |  |  |  |  |  |  | } | 
| 131 |  |  |  |  |  |  |  | 
| 132 | 0 |  |  |  |  |  | $ENV{'LD_LIBRARY_PATH'} = join q[:], @LDLIBS; ## no critic (RequireLocalizedPunctuationVars) | 
| 133 | 0 |  |  |  |  |  | return; | 
| 134 |  |  |  |  |  |  | } | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | sub dump { ## no critic (Homonyms) | 
| 137 | 0 |  |  | 0 | 1 |  | my $config = config(); | 
| 138 | 0 |  |  |  |  |  | for my $l (qw(Libs LDlibs)) { | 
| 139 | 0 | 0 |  |  |  |  | print $l, "\n" or croak $ERRNO; | 
| 140 | 0 |  |  |  |  |  | for my $s (sort $config->Parameters(lc $l)) { | 
| 141 | 0 |  |  |  |  |  | printf qq[%-12s %s\n], $s, $config->val(lc $l, $s); | 
| 142 |  |  |  |  |  |  | } | 
| 143 | 0 | 0 |  |  |  |  | print "\n\n" or croak $ERRNO; | 
| 144 |  |  |  |  |  |  | } | 
| 145 |  |  |  |  |  |  |  | 
| 146 | 0 |  |  |  |  |  | return; | 
| 147 |  |  |  |  |  |  | } | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | 1; | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | __END__ |