Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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

by GoldElite (Acolyte)
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.


Comment on Re: Executing a program from within a Perl Module in a non-standard path
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1024506]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2014-09-22 09:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (186 votes), past polls