develooper Front page | perl.perl6.language | Postings from April 2005

How do I... tie hashes/arrays?

Thread Next
From:
Ingo Blechschmidt
Date:
April 19, 2005 07:11
Subject:
How do I... tie hashes/arrays?
Message ID:
loom.20050419T154701-133@post.gmane.org
Hi,  
  
quoting an old post from Luke (http://xrl.us/ftet): 
> C<tie>ing is going to work quite differently, from what I hear.  
> So it might be possible to overload any function to call a  
> special version when your type of array is used.  You just have  
> to write all the special versions.  
  
So is the following valid?  
  class TiedArray is Array {  
    method push(*@things) {  
      ...;  
    }  
  }  
  
  my TiedArray @array;  
  push @array:    ...; # calls our push  
  unshift @array: ...; # calls Array::unshift  
  # Will push @array, ... DWIM?  
  
How do I define the method/sub responsible for @array.[index]/  
%hash.{key}?  
  class TiedArray is Array {  
    # Possibility #1  
    method *postcircumfix:<'[', ']'>(TiedArray $self, $index) {  
      return new Proxy:  
        FETCH => {...},  
        STORE => {...};  
    }  
  
    # Possibility #2  
    multi sub *postcircumfix:<'[', ']'>(TiedArray $self, $index) {  
      # Body as above  
    }  
  
    # Possibility #3  
    method FETCH($index)       {...}  
    method STORE($index, $val) {...}  
  
    # Possibility #4  
    method GET($index) is rw {  
      # Body as in possibility #1  
    }  
  }  
  
FWIW, I like possibility #4 best.  
  
  
--Ingo  
  
--   
Linux, the choice of a GNU | self-reference, n. - See self-reference    
generation on a dual AMD   |   
Athlon!                    |   


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About