A
Andrew Hamm
Hi Folks,
This is interesting phenomenom I've originally posted to comp.lang.perl.tk,
but I think it merits a wider canvassing* of opinion.
* That's a Perl/Tk joke.
I've often wondered if you can chain ties, and the apparent tying used by
the -textvariable option in Tk has led me to experiment with it since I want
to bind tied variables to Tk::Entry fields. The following is very
interesting:
#!/usr/bin/perl
use strict;
use warnings;
package Up;
sub TIESCALAR {
my $class = shift;
my $thing = shift;
return bless \$thing, $class;
}
sub FETCH {
my $self = shift;
return "up:$$self";
}
sub STORE {
my $self = shift;
my $value = shift;
warn "Up::STORE $value\n";
return $$self = $value;
}
package Down;
sub TIESCALAR {
my $class = shift;
my $thing = shift;
tie $thing, 'Up';
return bless \$thing, $class;
}
sub FETCH {
my $self = shift;
return "down:$$self";
}
sub STORE {
my $self = shift;
my $value = shift;
warn "Down::STORE $value\n";
return $$self = $value;
}
package main;
tie my $kangaroo, 'Down'; # sport
$kangaroo = 1;
print "kangaroo = $kangaroo\n";
__END__
Which means that you can indeed chain ties, although the 2nd tie needs to
cooperate - I'll work on a trick for that next...
So, who has any interesting thoughts on this subject? Has it been used in
other software? Are there any known caveats in this technique?
Thanks in advance for any edifying discussion.
This is interesting phenomenom I've originally posted to comp.lang.perl.tk,
but I think it merits a wider canvassing* of opinion.
* That's a Perl/Tk joke.
I've often wondered if you can chain ties, and the apparent tying used by
the -textvariable option in Tk has led me to experiment with it since I want
to bind tied variables to Tk::Entry fields. The following is very
interesting:
#!/usr/bin/perl
use strict;
use warnings;
package Up;
sub TIESCALAR {
my $class = shift;
my $thing = shift;
return bless \$thing, $class;
}
sub FETCH {
my $self = shift;
return "up:$$self";
}
sub STORE {
my $self = shift;
my $value = shift;
warn "Up::STORE $value\n";
return $$self = $value;
}
package Down;
sub TIESCALAR {
my $class = shift;
my $thing = shift;
tie $thing, 'Up';
return bless \$thing, $class;
}
sub FETCH {
my $self = shift;
return "down:$$self";
}
sub STORE {
my $self = shift;
my $value = shift;
warn "Down::STORE $value\n";
return $$self = $value;
}
package main;
tie my $kangaroo, 'Down'; # sport
$kangaroo = 1;
print "kangaroo = $kangaroo\n";
__END__
Which means that you can indeed chain ties, although the 2nd tie needs to
cooperate - I'll work on a trick for that next...
So, who has any interesting thoughts on this subject? Has it been used in
other software? Are there any known caveats in this technique?
Thanks in advance for any edifying discussion.