因为在Perl中简单地重载’=’并不像人们期望的那样行事,这样做的正确方法是什么?
引自overload perldoc:
Simple assignment is not overloadable (the
'='
key is used for the Copy Constructor). Perl does have a way to make assignments to an object do whatever you want,but this involves usingtie()
,notoverload
– see 07001 and the 07002 examples below.
我已经阅读了COOKBOOK和tie的文档,但我很难弄清楚如何以这种方式使用它.
我希望能够像这样创建一个对象:my $object = Object-> new()然后,当我将它分配给某些东西时,我希望它做一些特殊的处理.
例如:$object = 3会在内部执行类似$object-> set_value(3)的操作;
我知道这不一定是好习惯.这更像是一个教育问题.我只是想知道如何做到这一点.不是否应该这样做.