amnesty_puppy has asked for the wisdom of the Perl Monks concerning the following question:

I'm quite new to Perl but I'm doing my A2 (UK) computing project in it, using a hash tie to access a database. I have one pm file that changes for each table when you tie it to a hash (table is defined by an extra argument). When I tie another hash to the .pm file it seems to get confused and the data is retained from the original tie, although I'm sure I've seen have a mix between the two while debugging. What am I doing wrong? Thanks, Dom.

Replies are listed 'Best First'.
Re: Hash ties
by davido (Cardinal) on Jan 17, 2005 at 16:36 UTC

    You may be using package globals to store your data instead of properly scoped object containers such as a blessed lexical hashref. But this response is about as reliable as if you had taken me to a room full of people I don't know, put a blindfold on me, spun me around six times, and asked me to point to Kevin. We need to see code posted to be able to disect what you've done wrong. I recommend boiling the problem down to no more than 20 or so lines of code that replicates the problem and posting it here.


    Dave

      amnesty_puppy: Can't get to my code right now but that sounds like the right problem, I just declared it with my $hashref = {};. How should it be defined properly? I'll look it up in the camel when I get a minute. Thanks.
Re: Hash ties
by JediWizard (Deacon) on Jan 17, 2005 at 15:08 UTC

    Can you post some code? It sounds like you are using a package variable where you should be using an object property.

    Update: davido is correct, this is a wild guess. Its just my first instinct.

    May the Force be with you
Re: Hash ties
by amnesty_puppy (Novice) on Jan 19, 2005 at 21:13 UTC
    I've had a bit of a think but not sure what to do. I've got a Database.pm which is for tying:
    #!/usr/bin/perl package Database; my $database = {}; sub TIEHASH { my ($class, $key, $value, @options) = @_; # my $db = DBI->connect(...); # ... more database stuff $database->{"$key"} = $value; return bless $database; } sub FETCH { return $database->{"One"}; }


    And the script:
    #!/usr/bin/perl use Database; my %one, %two; tie (%one, Database, "One", "hard"); print $one{"One"}; tie (%two, Database, "One", "soft"); print $two{"One"};
    Seems a bit overkill but it shows what I mean, the hash value get's overwritten by the second tie, and I don't want it to. I thought of doing it with the anonymous hash for object properties but then what would I return in TIEHASH? Thanks.