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; # $[]
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.
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.
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;
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With