문제

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::Tie and IPC::Shareable.

도움이 되었습니까?

해결책

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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top