Chains of ties

Discussion in 'Perl Misc' started by Andrew Hamm, Jul 9, 2004.

  1. Andrew Hamm

    Andrew Hamm Guest

    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.
     
    Andrew Hamm, Jul 9, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian J. Sayatovic

    SSL trust chains

    Brian J. Sayatovic, Oct 16, 2003, in forum: Java
    Replies:
    0
    Views:
    514
    Brian J. Sayatovic
    Oct 16, 2003
  2. Andrew Thompson

    Code that ties up the compiler

    Andrew Thompson, Nov 26, 2008, in forum: Java
    Replies:
    1
    Views:
    292
    Roedy Green
    Nov 27, 2008
  3. Michael Angelo Ravera

    Ranking an array (with ties and tollerances)

    Michael Angelo Ravera, Aug 17, 2011, in forum: C Programming
    Replies:
    13
    Views:
    566
    Michael Angelo Ravera
    Aug 21, 2011
  4. Axel Etzold

    Finding ties in sorting?

    Axel Etzold, Sep 14, 2007, in forum: Ruby
    Replies:
    4
    Views:
    128
    Dan Zwell
    Sep 14, 2007
  5. kj
    Replies:
    8
    Views:
    145
    phaylon
    Mar 14, 2005
Loading...

Share This Page