Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tie variable multiple times

Tags:

perl

tie

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.

like image 511
Glen Solsberry Avatar asked Dec 22 '22 09:12

Glen Solsberry


2 Answers

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
like image 66
hobbs Avatar answered Jan 01 '23 08:01

hobbs


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.

like image 38
Leon Timmermans Avatar answered Jan 01 '23 07:01

Leon Timmermans