#12 Patch to alleviate BIN_DIR, SCRIPT_DIR, etc

v3.xx
open
nobody
5
2010-08-27
2010-08-27
P Fudd
No

I'm setting up MUMmer 3.0, and I copied the binaries into /usr/local/bin,
where they promptly broke, because the install path was hardcoded into
the perl scripts.

Here is the solution:

##################################################
my $BIN_DIR;
my $SCRIPT_DIR;
my $AUX_BIN_DIR;

BEGIN {
use Cwd;
my $linkcount=50;
(my $file=$0)=~s/.*\///; #name of program - $0
($BIN_DIR=$0)=~s/[^\/]*$//; $BIN_DIR||=getcwd()."/"; $BIN_DIR=~s/[\r\n]//g;
while (defined(my $l=readlink($BIN_DIR.$file))) { #looks for symbolic link
if ($linkcount--<0) {die("$0: symlink loop detected, dying\n");}
($file=$l)=~s/.*\///;
if (substr($l,0,1) eq "/") {($BIN_DIR=$l)=~s/[^\/]*$//;next;}
(my $npwd=$l)=~s/[^\/]*$//; $BIN_DIR.=$npwd;
};
$SCRIPT_DIR="$BIN_DIR/scripts";
$AUX_BIN_DIR="$BIN_DIR/aux_bin";
}

use lib $SCRIPT_DIR;

#################################################

This code fragment takes the name of the currently running program ($0), traces any symlinks, and sticks the appropriate directory names into $BIN_DIR, $SCRIPT_DIR and $AUX_BIN_DIR, and adds $SCRIPT_DIR to the perl library path. This should alleviate any hard-coded directory silliness, at least on the perl side of things.

Discussion