in reply to Help with Best Practices for Module Paths when scripting across multiple machines

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."); }

Replies are listed 'Best First'.
Re^2: Help with Best Practices for Module Paths when scripting across multiple machines
by mwb613 (Beadle) on Nov 07, 2014 at 23:33 UTC

    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.