开发者

Tie variable multiple times

Can I tie a variable multiple times? I'd try it myself, but I'm not sure of the syntax. I want to tie a hash to Cache::Memcached::Ti开发者_如何学Ce and IPC::Shareable.


No. Confirming bvr's guess, a variable can only have a single "tied" magic. When you call tie on an already-tied variable, the existing tie-magic (and the associated tied object) is discarded before the new tie is created.

Toy example:

package Foo;
sub TIESCALAR { return bless [] }
sub DESTROY { print "Destroying Foo\n" }

package Bar;
sub TIESCALAR { return bless [] }
sub DESTROY { print "Destroying Bar\n" }

package main;
tie my $var, "Foo";
print "Tied to ", ref tied $var, "\n";
tie $var, "Bar";
print "Tied to ", ref tied $var, "\n";

Output:

Tied to Foo
Destroying Foo
Tied to Bar
Destroying Bar


Not only is this not possible, but it's not sensible either. What is a fetch supposed to mean in this context? How to deal with them returning two different values?

What I suspect you want is a multilevel caching system, you may want to look into CHI for that.


I don't think it is possible to tie two classes to single item. Running tie second time just remove original link and replace with new one.

It should be possible to write some kind of multi-dispatch, though. I mean writing a class that accept multiple other classes and call their respective methods like FETCH or STORE.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜