Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the path in file of a different file of the module?

How do I get the path of another file of the module?

I tried it this way but I don't know where to get the $here and I don't know if it would work if I had the $here?

use v6;
unit class Aaa::Bbb::Ccc;

my $here = '.../lib/Aaa/Bbb/Ccc.pm6'.IO;
my $bin = $here.parent.parent.parent.add( 'bin' ).add( 'app' );

use Pod::Load;
my $pod = load( $bin );
# use the $pod

When I try this, $pod is empty:

use Pod::Load;
my $pod = load( $*PROGRAM );
say $pod.perl;  # $[]
like image 501
sid_com Avatar asked Apr 26 '19 09:04

sid_com


People also ask

What is __ path __ in Python?

If you change __path__ , you can force the interpreter to look in a different directory for modules belonging to that package. This would allow you to, e.g., load different versions of the same module based on runtime conditions.

How do you call a module from another folder?

We can use sys. path to add the path of the new different folder (the folder from where we want to import the modules) to the system path so that Python can also look for the module in that directory if it doesn't find the module in its current directory.


2 Answers

There is no such thing as a "another file of the module". A module would represent only a single file; a distribution represent multiple modules and thus multiple files. Additionally a module cannot assume it is being loaded from a file system -- it might be loaded into memory out of a tar file or socket, and if a module were to try to access e.g. "../MyOtherModule.pm6".IO it would obviously fail.

What you can safely do is get at the content of any module in the same distribution:

unit class Aaa::Bbb::Ccc;

my $bin-code = $?DISTRIBUTION.content("bin/app").open.slurp;
like image 96
ugexe Avatar answered Sep 28 '22 20:09

ugexe


Update: As @ugexe pointed out, if you intend on installing your module this will not work.


$?DISTRIBUTION seems like a good solution.

Otherwise an alternative is to use the $?FILE variable. You would need to export this from your module if want to use it somewhere else in your program:

unit module MyModule;

our sub myDIR() {
    $?FILE.IO.dirname;
}
use MyModule;
myDIR()~"/relative/path/to/somefile
like image 45
drclaw Avatar answered Sep 28 '22 18:09

drclaw