I have an XS modules which defines a couple of attributes, so for better
safety I thought I should make it a tied hash, and check that only
known attributes are fetched and stored, and for some attributes also
a check of the value.

I read "Understanding the Magic of Tied Hashes and Arrays" on perlguts, but
I can't say I understood it well. I didn't try the code on that page. 
Instead I figured I could do it from Perl. After some trial and error, I 
got the below working. Almost. When the test script terminates, there is
an access violation, and a debug-print last in my DESTROY prints, so it
appears to be in Perl itself where things go bad. 

In the new() routine I call an XS routine called news (temporary name during
test), which fills in various attributes in the hash. (I've tried setting
them from Perl too, but it still crashes.

It might be that I have done something really stupid. But I figured I should 
check whether I'm running in the right direction at all.

   package MSSQL::OlleDB;
   ...
   use Exporter;
   use DynaLoader;
   use Tie::Hash;
   ..
   @ISA = qw(Exporter DynaLoader Tie::StdHash);
   ...
   @EXPORT = qw(new);
   ...
   #------------------------  FETCH and STORE ------------------------------
   # My own FETCH routine, chckes that retrieval is of a known attribute.
   sub FETCH {
      my ($self, $key) = @_;
      if (not grep($key eq $_, ALL_ATTRIBUTES)) {
          croak("Attempt to fetch undefined attribute '$key'");
      }
      return $self->{$key};
   }
   
   # My own STORE routine, barfs if attribute is non-existent.
   sub STORE {
      my ($self, $key, $value) = @_;
      if (not grep($key eq $_, ALL_ATTRIBUTES)) {
          croak("Attempt to store value for undefined attribute '$key'");
      }
      $self->{$key} = $value;
   }
      
   bootstrap MSSQL::OlleDB;
   
   sub new {
      my ($self) = @_;
   
      my %olle;
      my $X = tie %olle, $self;
      news($X);    
      bless \%olle, $self;
   }
   




-- 
Erland Sommarskog, Stockholm, [EMAIL PROTECTED]

Reply via email to