为什么不“使用过载"?使用“使用命名空间:自动清理"? [英] Why doesn't "use overload" work with "use namespace:autoclean"?

查看:96
本文介绍了为什么不“使用过载"?使用“使用命名空间:自动清理"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,只是为了健全性检查,过载似乎对我没有用.我不知道它是我拥有的perl版本,还是overload.pm版本,还是我实现它的方式有问题,但是此代码对我不起作用.

Ok just to sanity check overload doesnt seem to be working for me. I don't know if it's the version of perl I have, or the version of overload.pm, or something wrong with how I've implemented it, but this code doesnt work for me.

perl版本

This is perl, v5.10.1 (*) built for x86_64-linux-gnu-thread-multi

重载版本

perl -Moverload -e 'print "$overload::VERSION\n";'
1.07

Token.pm

package Token;
use namespace::autoclean;
use Data::Dumper;


use Moose;
use Moose::Util::TypeConstraints; 

use overload '+' => \&_overload_add, fallback => 1;

  has 'secretvalue' => ( is => 'rw', isa => 'Int');  

  sub _overload_add{
    my ( $one, $two ) = @_;   
    my $value = $one->secretvalue() + $two->secretvalue();
    return ($value);
  }

主要

use strict;
use warnings;
use Token;
my $t = Token->new( secretvalue => 17, key => 'x' );
my $t2 = Token->new( secretvalue => 12, key => 'y' );

my $x = $t + $t2;

print $x;

打印

 $VAR1 = 12900840;

最糟糕的是,我在日志中没有得到任何警告或错误.

The worst part is that I'm not getting any kind of warning or errors in the log.

更新

感谢Freido发现问题.我已经更新了这个问题,以防万一其他人偶然发现这个问题.

Thanks to Freido for finding the problem. I've updated the question just in case anyone else stumbles on this.

Perl/Moose社区通常不使用重载吗?

Does the Perl/Moose community generally not use overload?

推荐答案

namespace::autoclean迷上了overload为处理操作员而添加的魔力.您可以预期以下工作:

namespace::autoclean is futzing with the magic that overload adds to handle your operator. The following works as you would expect:

package Token;

use Moose;
use Moose::Util::TypeConstraints;

use overload '+' => '_overload_add';

has 'secretvalue' => ( is => 'rw', isa => 'Int');

sub _overload_add{
    my ( $one, $two ) = @_;
    my $value = $one->secretvalue() + $two->secretvalue();
    return ($value);
}

1;

随便一眼都不会在namespace::autoclean文档中发现解决此问题的任何内容,因此我想这是一个出乎意料的功能.一些搜索显示,已经报告了一个错误.

A casual glance does not reveal anything in the namespace::autoclean docs that addresses this, so I guess it's an unanticipated feature. Some searching reveals that a bug has been reported.

我想它归结为overload添加到您的包装中的特殊符号.重载+将添加符号表条目()(+OVERLOAD.我猜测namespace::autoclean将其中的一些或全部清理干净,从而消除了重载.

I guess it comes down the special symbols that overload adds to your package. Overloading + adds the symbol table entries (), (+, and OVERLOAD. I'm guessing some or all of these are vacuumed up by namespace::autoclean, thus undoing your overloading.

如果您喜欢冒险,请此处是namespace::autoclean的补丁,用于过滤掉重载符号

If you're feeling adventurous, here's a patch for namespace::autoclean that filters out overload symbols.

这篇关于为什么不“使用过载"?使用“使用命名空间:自动清理"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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