Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine the absolute path name of the compilation unit of the caller?

Tags:

raku

According to p6doc 5to6-perlfunc:

FILE

Replaced by $?FILE which is slightly different from __FILE__ in that it is always an absolute path, rather than a relative one in the Perl 5 case.

and according to p6doc CallFrame:

With no arguments the callframe will give you frame information for the line calling callframe. The file and line annotations will be identical to those in $?FILE and $?LINE.

But when I tested this, the file attribute gives a relative path name:

./p.p6:

use v6;
use Data::Dump::Tree;

use lib '.';
use MyModule;

say 'Main script: package path name     : ', $?FILE;
say 'Main script: $*PROGRAM-NAME        : ', $*PROGRAM-NAME;
MyModule::func();

./MyModule.pm6:

unit module MyModule;

our sub func() {
    say "func: This package path name     : ", $?FILE;
    my $frame = callframe 1;
    say "func: Calling package's file name: ", $frame.file;
}

Output:

Main script: package path name     : /home/hakon/test/perl6/./p.p6
Main script: $*PROGRAM-NAME        : ./p.p6
func: This package path name     : /home/hakon/test/perl6/MyModule.pm6 (MyModule)
func: Calling package's file name: ./p.p6

How can I get the absolute path name of the compilation unit of the caller?

like image 273
Håkon Hægland Avatar asked Apr 17 '19 19:04

Håkon Hægland


1 Answers

Have you looked at the absolute method from IO::Path?

say "func: Calling package's file name: ", $frame.file.IO.absolute;

like image 200
LuVa Avatar answered Nov 17 '22 12:11

LuVa