mwb613 has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

Sorry for the long title, I'm having trouble articulating just the issue I'm having

Basically I'm wondering what is the best way, without using absolute paths, to set up my files so that modules AND non-modules (such as YAML files) can be referenced easily in a script. I often reference a module using "use lib()" and those modules often reference yaml files using absolute paths. I've come to realize that if I move those files to other computers I cannot easily set up the paths so everything is included properly (see below for more info). Even if I start keeping the modules in the same DIR as the script (or one level below in a modules DIR for example) I cannot use relative paths in the case that the script is not run from the directory that it resides (if Cron were running it for example).

What is the best practice to ensure portability for modules and other files a script might need to reference?

Here's why I'm asking:

Up until this point I have been working with Perl with only two different Linux machines where. I set up the directory structures identically so when I wanted to move a script from one machine to another I just needed to copy it and any "use lib" includes I wanted would work fine.

I now have a few more machines, VMs, etc and thought it would be a good idea to set up source control for additional users, etc. So I created a few GIT repositories and realized, as I was trying to add files into the repo so they could be replicated to other machines, I could no longer use a hierarchy where I put all PM files in a single dir and all YAML files in another and still break the repo into smaller projects rather than a big monolithic repo (there would be issues even with that since the placement of the cloned repo is not assured.

  • Comment on Help with Best Practices for Module Paths when scripting across multiple machines

Replies are listed 'Best First'.
Re: Help with Best Practices for Module Paths when scripting across multiple machines
by boftx (Deacon) on Nov 07, 2014 at 23:41 UTC

    This the basic approach I use to find a known location for a module subdir named for itself:

    # used to find the location of THIS module. assumes that all support # dirs will be under a directory named after this module (without # the '.pm') # NOTE! this is a class method that doesn't check the 'cached' value. # YOU WILL BE SURPRISED if there has been an intervening chdir operati +on! # see the public 'basename_dir' method for normal use. sub _basename_dir { my $package = __PACKAGE__; $package =~ s/::/\//g; my $packpath = $INC{ join( '.', $package, 'pm' ) }; $packpath =~ s/\.pm$//; my $realpath = Cwd::realpath($packpath); return $realpath; }
    You will probably need to play with it a bit, but the general approach might helpful.

    You must always remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.
Re: Help with Best Practices for Module Paths when scripting across multiple machines
by RonW (Parson) on Nov 07, 2014 at 22:30 UTC

    Perl uses @INC to find modules, so a module should be able to use @INC to find itself.

    For a program to find itself, examining the value of $0 in the context of (a) the current working directory and (b)the value of $ENV{PATH}, might locate the running program.

    Update: Added sample code.

    use warnings; use strict; package My::Mod; use File::Spec; use Carp; my $module = File::Spec->catfile(split('::', __PACKAGE__)); my $modPath; for (@INC) { my $t = File::Spec->catfile($_, $module); if ( -e $t ) { $modPath = $t; last; } } my $configPath = $modPath; if ($modPath) { $configPath =~ s/\.pm$/.yml/; } else { croak("Can't find config file."); }

      I'm not sure I'm answering my original question regarding best practices but using your advice I found the following works to load YAML files that are in a folder "next" to my modules.

      Within my module:

      use File::Basename; my $current_package = __PACKAGE__; my $path_to_module = dirname($INC{"$current_package.pm"}); my $path_to_yaml = $path_to_module; $path_to_yaml =~ s{modules}{yaml}; my $yaml_config = LoadFile($path_to_yaml.'/file_i_want_to_load.yaml');
      Thanks very much for your help.

        I had forgotten about %INC

        Glad that you figured it out from my idea. Your solution is much better.

Re: Help with Best Practices for Module Paths when scripting across multiple machines
by GotToBTru (Prior) on Nov 07, 2014 at 23:06 UTC

    Install the YAML files in the same directory as the module. Then, within your program, search @INC for them.

    1 Peter 4:10
Re: Help with Best Practices for Module Paths when scripting across multiple machines
by Anonymous Monk on Nov 07, 2014 at 23:16 UTC