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

I'm stuck on how to create a data file that can be read and written to. If I used File::ShareDir, I find that I don't have permission to write to the data file (it's a json file with oauth info that has to be periodically udpated). If I stick the module in a directory within the module, the path to the data file cannot be found by both my test scripts and the module when it is actually installed . I suppose I could get the absolute location of the module on the system and tack that on the front of the file name but I'm wondering if there is a better recommendation for handling this problem. Thanks!

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re: Reading/writing to a module's data file
by haj (Vicar) on Aug 05, 2018 at 10:10 UTC
    Quoting File::ShareDir (emphasis mine):
    Quite often you want or need your Perl module (CPAN or otherwise) to have access to a large amount of read-only data that is stored on the file-system at run-time.

    If you want ordinary users to create their own data files, then File::HomeDir is for you. In particular, look at the functions my_dist_data and my_dist_config.

    If you want a shared file with r/w access in your module's directory, then the absolute path can be identified by loading the module and inspecting its entry in %INC. Since files in the lib section are usually installed read-only, you'd still need to modify the permissions during installation of your module. From a security perspective, a globally-writeable file will probably cause some nasty issues because it's not only your application which can write the file.

Re: Reading/writing to a module's data file
by kevbot (Vicar) on Aug 05, 2018 at 06:22 UTC
    Here are a couple modules that might help you. FindBin can help with determining the location of a data file, and Test::File::ShareDir will allow your tests to access the files in your share dir even if you have not installed the module yet.

    This article may also give you some ideas.

    Writing to a data file that is in the install location of your module seems a bit strange to me. Could you address your issue by changing your module(s) such that it uses a config file? The config file could contain the oauth info that needs to be updated. There are many modules that can read/write config information, e.g. Config::JSON.

Re: Reading/writing to a module's data file
by Anonymous Monk on Aug 05, 2018 at 10:15 UTC

      Thanks, I'll probably go this direction. I was thinking of writing a similar module myself so this is a great find.

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest";
      $nysus = $PM . ' ' . $MCF;
      Click here if you love Perl Monks