Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top