http://qs1969.pair.com?node_id=11118780

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

I have been documenting my Random modules in a readme for GitHub when I came across some that have external data. Two of them have text files that I open, chomp, and make into an array; nothing special. However, the data for those two are in my general data directory. If I people want to use these modules (and if I ever get them properly written), I will need to put the data closer to them. Now, I just don't know where to put them. (The other modules with external data need hashes made, and that is a bigger problem.)

You might be wondering why I did not put the data directly into the module. It is because two of the lists are over 100 items long and the other two are over 10 with longer strings.

I am also looking to move these Random modules away from using any of my Util modules. My Util modules are messes and for my personal use. (So many PODs to update with their dependencies, hopefully without using any of those Util modules one day.)

I know it may be a matter of taste, but where you would put data files relative to the module that uses them?

My OS is Debian 10 (Buster); my perl versions are 5.28.1 local and 5.8.8 on web host.

No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
Lady Aleena

Replies are listed 'Best First'.
Re: Where to put data that goes with a module?
by swl (Parson) on Jul 02, 2020 at 04:36 UTC
      I have been planning on asking a similar question. In my case, I have an old .pl script which I still occasionally find useful. It requires a large number of sound (.wav) files which it plays by passing their path to "Windows Media Player" with exec. (Because of changes to the Player, it only works reliably on Windows XP. I hope to fix that.) Your suggestion to use build tools to manage the sound files seems like overkill. I have never even attempted to use Makefile.PL and have had only limited success with Build.PL.
      Bill

        Yes, it might well be overkill for many applications. If you have a standalone script that is not going onto CPAN then it's just as easy to add an adjacent data directory to an archive.

        Overkill?

      Hi. I know it has been almost two months since I asked this, but I got frustrated then, so decided to let this sit for a while. So now I have calmed down a bit, I can dig in.

      So, today I decided to give File::ShareDir a go on the command line, and I got the answer I was looking for initially. The main directory for external files a module needs is lib/auto. So now I know where to put the external data files and can use File::ShareDir to hopefully get them. I have not tried this yet but will shortly. Here is what I think the top of my module will look like, in this case I am using one of my favorites, Random::Color.

      package Random::Color; use v5.10.0; use strict; use warnings; use Exporter qw(import); use File::ShareDir qw(module_dir); use Fancy::Rand qw(fancy_rand); use Fancy::Open qw(fancy_open); our $VERSION = '1.000'; our @EXPORT_OK = qw(random_color); my $directory = module_dir('Random::Color'); my @Crayola_crayons = fancy_open("$directory/Crayola_crayon_colors.txt +");; my @MandMs = fancy_open("$directory/MandMs_colors.txt");

      Also, I am assuming when I go to package Random::Color the file list inside the package will be something like this.

      • Random-Color
        • Changes
        • ignore.txt
        • Makefile.PL
        • MANIFEST
        • README
        • lib
          • auto
            • Random
              • Color
                • Crayola_crayon_colors.txt
                • MandMs_colors.txt
          • Random
            • Color.pm
            • Color.pod
        • t
          • 00-load.t
          • manifest.t
          • pod-coverage.t
          • pod.t
        • xt
          • boilerplate.t

      Am I on the right track here? I got the base of this structure from Discipulus's tutorial.

      As an aside, I finally started using git because of that tutorial. How to use git was explained very well there. I am so very grateful for it!

      Update: I tried it, it works as expected, I'm happy! Thank you for the recommendation.

      My OS is Debian 10 (Buster); my perl versions are 5.28.1 local and 5.16.3 or 5.30.0 on web host depending on the shebang.

      No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
      Lady Aleena

        Good to hear it works.

Re: Where to put data that goes with a module?
by tobyink (Canon) on Jul 02, 2020 at 09:08 UTC

    Either use File::ShareDir as swl suggests, or if there's a one-to-one correspondence between modules and data (i.e. your module only needs one data file, and the data file will only be used by that one module) use a __DATA__ section.

      Another option to allow essentially multiple DATA segments is Inline::Files
Re: Where to put data that goes with a module?
by perlfan (Vicar) on Jul 02, 2020 at 05:17 UTC