有没有办法从 Perl6 模块代码中的 META6.json 获取版本? [英] Is there a way to get the version from META6.json in Perl6 module code?
问题描述
我想做这样的事情:
die "Fatal error: application version $MY-APPLICATION-VERSION"
由于 Perl6 模块不能假设它打包的文件的相对位置(例如安装的模块都放在一个平面目录结构中)我不能只做 $?FILE.IO.add("../META6.json").slurp
.有没有办法在可能已安装或可能未安装的 Perl6 应用程序/模块中获取 META6.json 中声明的版本?
Since a Perl6 module cannot assume the relative locations of files it was packaged with (for instance installed modules are all put into a flat directory structure) I can't just do $?FILE.IO.add("../META6.json").slurp
. Is there a way to get the version declared in META6.json in a Perl6 app/module that may or may not be installed?
推荐答案
自 rakudo v2019.03
模块可以访问 Distribution 对象用于通过 $?DISTRIBUTION.这允许以下内容:
As of rakudo v2019.03
modules may access the Distribution object used to load it via $?DISTRIBUTION. This allows the following:
unit module My::Module;
BEGIN my $VERSION = $?DISTRIBUTION.meta<version>;
use My::SubModule:ver($VERSION); # ensure we only ever use the version included in this distribution
die "Fatal error: application version $VERSION"
这篇关于有没有办法从 Perl6 模块代码中的 META6.json 获取版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!