10

I have this module which needs a specific file to work. You can pass the path of the file you want to use or not. If you don't, then a default file is taken. That default file is located at the resources folder, so I typed the path as: "resources/data/type-graph.txt". The problem is that does not work because it takes my CWD as root directory.

Do you know how to make the path relative to the module dir?

Any suggestion is appreciated :).

jjmerelo
  • 22,578
  • 8
  • 40
  • 86
Antonio Gamiz Delgado
  • 1,871
  • 1
  • 12
  • 33
  • I'd take a look at Zef and how it handles config files. I think you should be able to have your module install a default config somewhere that it can access but off the top of my head I'm not sure where. – Scimon Proctor Jul 02 '19 at 09:17

1 Answers1

12

You should take a look at the Modules documentation page. There this example is given to access a file placed in the resources folder:

my $template-text = %?RESOURCES<templates/default-template.mustache>.slurp;

You also need to list the file in META6.json so the file will be accessible once the module is installed.

{
    ...,
    "resources": [ "templates/default-template.mustache"]
}

As guifa noted in a comment %?RESOURCES works with individual files, not directory structures. It makes no guarantees of how the files are actually stored. So %?RESOURCES<templates>.dir will not work.

Patrick Böker
  • 3,173
  • 1
  • 18
  • 24
  • 2
    It might be worthwhile to note that that %?RESOURCES requires full foreknowledge of the file names. You can't, for instance, do `%?RESOURCES.dir` and get usable results. – user0721090601 Jul 03 '19 at 04:22
  • 3
    @guifa note that anything using `%?RESOURCES` has the means to know the file names: `say %?RESOURCES{$_} for $*DISTRIBUTION.meta` – ugexe Jul 03 '19 at 14:53
  • @ugexe eah, I should have been clearer, you need to pass it an exact filename key. I never thought about peaking into `$*DISTRIBUTION` —  I'm going to update my localization module to use that, thanks for the idea! – user0721090601 Jul 03 '19 at 15:26