Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Executing a program from within a Perl Module in a non-standard path

by GoldElite (Beadle)
on Mar 20, 2013 at 12:14 UTC ( #1024506=note: print w/replies, xml ) Need Help??

in reply to Executing a program from within a Perl Module in a non-standard path

Alright, I found a solution that isn't perfect, but suits my needs thus far. Since the Utilities project includes a logger package that is used by practically every other script (and which already has tenuously appropriate functions ;) ), I managed to put a "get_script_dir" function into it that other scripts can use to figure out where they are (it is exported along with multiple other things for convenience):

sub get_script_dir { #Get the calling script's path my $sc_path = (caller)[1]; #Parse the (possibly relative) directory out of the filename $sc_path =~ m/^(.*[\/\\]+)[^\/\\]*$/; $sc_path = $1 || "."; #Append a trailing slash if there isn't one, for consistency $sc_path .= "/" unless m/[\/\\]$/; return $sc_path; }


Then, in every script that isn't a Package/Module, I start with the following:

#!/usr/bin/perl use strict; use warnings; use FindBin; use lib "$FindBin::Bin"; use lib "$FindBin::Bin/Utilities"; ...etc...


This way, scripts will call packages from their working directories regardless of the cwd (any script executed via 'system', etc - rather than just used - will inherit its own, relative, FindBin value). Any Package/Module/Script that feels the need to execute something in its own path can then call something like system(get_script_dir() . "some_executable"); which should™ always work.

It's not amazing, and probably slightly hacky, but it's the most efficient method to overcome Perl's trouble with script-relative paths that I could produce so far. I'm still open to any better solutions, but at this point I'm not expecting any :)


As an adendum, something that I completely failed to mention in the orignal post, is that most of the servers should be expected to be only running core versions of Perl 5.8.8 on RedHat. Not that's reeaally changed anything, but still, I'm noting this now, for reference.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1024506]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2018-06-22 21:34 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (124 votes). Check out past polls.