Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find the version and authority of a Perl 6 module?

Tags:

module

raku

In Bar.pm, I declare a class with an authority (author) and a version:

class Bar:auth<Camelia>:ver<4.8.12> {
    }

If I use it in a program, how do I see which version of a module I'm using, who wrote it, and how the module loader found it? As always, links to documentation are important.

This question was also asked on perl6-users but died before a satisfactory answer (or links to docs) appeared.

Another wrinkle in this problem is that many people aren't adding that information to their class or module definitions. It shows up in the META.json file but not the code.

like image 335
brian d foy Avatar asked Jul 03 '17 10:07

brian d foy


1 Answers

(Probably not a satisfying answer, because the facts of the matter are not very satisfying, especially regarding the state of the documentation, but here it goes...)


If the module or class was versioned directly in the source code à la class Bar:auth<Camelia>:ver<4.8.12>, then any code that imports it can introspect it:

use Bar;

say Bar.^ver;   # v4.8.12
say Bar.^auth;  # Camelia

# ...which is short for:
say Bar.HOW.ver(Bar);   # v4.8.12
say Bar.HOW.auth(Bar);  # Camelia

The ver and auth methods are provided by:

  • Metamodel::ClassHOW (although that documentation page doesn't mention them yet)
  • Metamodel::ModuleHOW (although that documentation page doesn't exist at all yet)

Unfortunately, I don't think the meta-object currently provides a way to get at the source path of the module/class.
By manually going through the steps that use and require take to load compilation units, you can at least get at the prefix path (i.e. which location from $PERL6LIB or use lib or -I etc. it was loaded from):

my $comp-spec = CompUnit::DependencySpecification.new: short-name => 'Bar';
my $comp-unit = $*REPO.resolve: $comp-spec;
my $comp-repo = $comp-unit.repo;
say $comp-repo.path-spec;  # file#/home/smls/dev/lib
say $comp-repo.prefix;     # "/home/smls/dev/lib".IO

$comp-unit is an object of type CompUnit.
$comp-repo is a CompUnit::Repository::FileSystem.
Both documentations pages don't exist yet, and $*REPO is only briefly mentioned in the list of dynamic variables.


If the module is part of a properly set-up distribution, you can get at the meta-info defined in its META6.json (as posted by Lloyd Fournier in the mailing list thread you mentioned):

if try $comp-unit.distribution.meta -> %dist-meta {
    say %dist-meta<ver>;
    say %dist-meta<auth>;
    say %dist-meta<license>;
}
like image 184
smls Avatar answered Nov 13 '22 07:11

smls