10

I've started a module-starter (with --builder=Module::Build). I want to use SQL::Library to collect my SQL into an .ini file... but in order to find the file during run time, I need to know its exact path. How do I get the path of the "data directory" of a module at run time?

Until now, I've been using FindBin (like "$FindBin::Bin/../../path/to/module/datafiles/foo.ini", but I found this is not very robust (For example, it seems to break when there are two programs with the same name in two different dirs in PATH).

cjm
  • 61,471
  • 9
  • 126
  • 175
user1481
  • 849
  • 1
  • 5
  • 15

1 Answers1

16

This is what File::ShareDir is for. Since you're using Module::Build, you'll need to set the share_dir parameter (and require Module::Build 0.36) in order to have your data files installed along with your module. Then, in your code, you'll use File::ShareDir to calculate the path of foo.ini (e.g. dist_file('My-Dist', 'foo.ini'))

cjm
  • 61,471
  • 9
  • 126
  • 175
  • Can I get `File::ShareDir` to find files when the module is not yet installed, from scripts in bin/ or test files? – user1481 Feb 15 '12 at 00:50
  • @user1481, Scripts run through `Build test` will find the file because the module is installed (in `blib/`) when those tests are run. – ikegami Feb 15 '12 at 00:57
  • 1
    @user1481, For other scripts, either do `perl Build.PL && ./Build && perl -Mblib script.pl` or add code to fallback to `"$FindBin::Bin/../share/foo.ini"` if the file returned by File::ShareDir doesn't exist. – ikegami Feb 15 '12 at 01:02
  • File::Share works exactly as File::ShareDir, but can see the ./share directory during development without needing to install. – preaction May 17 '12 at 03:56