一个 perl6 模块可以有条件地“使用"另一个 perl6 模块吗? [英] Can one perl6 module conditionally 'use' another perl6 module?

查看:54
本文介绍了一个 perl6 模块可以有条件地“使用"另一个 perl6 模块吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种合理的方法可以让一个 perl6 模块检查另一个 perl6 模块是否存在并在且仅当它安装时使用"它?

Is there a sensible way to have one perl6 module check for the presence of another perl6 module and to 'use' it if and only if it is installed?

这样的东西...

module Polygons;

if $available {
    use Measure;                #only if Measure is installed
}

class Rectangle is export {
    has $.width;
    has $.height;

    method area {
        $!width * $!height;     #provides operator overload for Measure * Measure
    }
}
#====================

module Measure;

class Measure is export {
    has $.value;
    has $.unit;

    method Real {
        $!value;
    }
    method Str {
        "$!value $!unit";
    }
    method multiply( $argument ) {
        my $result = $.;
        $result.value = $!value * $argument;
        $result.unit  = "$!unit2";
        return $result;
    }
}

multi infix:<*> ( Measure:D $left, Measure:D $right ) is export {
    return $result.multiply( $argument );
}

#====================

#main.p6

use Polygons;
use Measure;

my $x = Measure.new( value => 10, unit => 'm' );
my $y = Measure.new( value => 20, unit => 'm' );

my $rect = Rectangle.new( width => $x, height => y );
say $rect.area;        #'200 m2'

这个想法是传播运算符重载(在本例中为中缀:<*>)备份类继承,以便在属性中存储更复杂的对象.

The idea is to propagate the operator overload (infix:<*> in this case) back up the class inheritance so that one store more elaborate objects in the attributes.

(请不要撕掉排水管 - 因为我怀疑总有办法!)

(Without tearing up the drains please - since I suspect there is always a way!)

推荐答案

所以这个答案的第一个版本基本上没用.

So the first version of this answer was essentially useless.

这是我想出的第一个新东西,它适用于我理解的您的问题.我还没有在 repo 上试过.

Here's the first new thing I've come up with that works with what I understand your problem to be. I haven't tried it on the repo yet.

在文件 a-module.pm6 中:

unit module a-module;
our sub infix:<*> ($l,$r) { $l + $r } }

our 意味着如果我们可以要求它,我们将能够看到这个例程,尽管它只能通过它的完全限定名称可见&a-module::infix:<*>.

The our means we'll be able to see this routine if we can require it, though it'll only be visible via its fully qualified name &a-module::infix:<*>.

然后在一个使用文件中:

Then in a using file:

use lib '.';
try require a-module;
my &infix:<*> = &a-module::infix:<*> // &OUTER::infix:<*>;
say 1 * 2 # 2 or 3 depending on whether `a-module.pm6` is found

如果模块丢失,使用的默认例程可以是来自 OUTER(如图所示)或来自 CALLER 或任何其他 你喜欢的伪包.

The default routine used if the module is missing can be the one from OUTER (as shown) or from CALLER or whatever other pseudo package you prefer.

这个问题/解决方案看起来很基本,我怀疑它一定是在 SO 上或文档中的某个地方.我会发布我所知道的,然后明天再探索.

This problem/solution seems so basic I suspect it must be on SO or in the doc somewhere. I'll publish what I've got then explore more tomorrow.

这篇关于一个 perl6 模块可以有条件地“使用"另一个 perl6 模块吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆