Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tie::Hash experiment 1

Status
Not open for further replies.

133tcamel

Programmer
Sep 22, 2002
137
IN
Hi,
I'm trying to use Hash references with Tie::Hash but I'm a little confused as to how this is working out so far. Can somebody please explain to me the output and the correct way to implement this? Thanks!

Regards, San

Code:
package Tie_Test;

use Tie::Hash;
use Data::Dumper;
use vars qw(@ISA);
@ISA = qw(Tie::StdHash);

sub STORE
{
    my ( $self, $key, $value ) = @_;
    print "STORE: [$key, $value ]", Dumper($key, $value), "\n";
}

sub FETCH
{
    my ( $self, $key ) = @_;
    print "FETCH: ", Dumper($key), "\n";
    #return $h;
}


tie my %config, 'Tie_Test';

$config{'dsn'}->{'test'} = 44;

1;

Output:
Code:
FETCH: $VAR1 = 'dsn'; #Fetch called first?

If I uncomment the "return $h;" there is the following output:
Code:
FETCH: $VAR1 = 'dsn';

STORE: [dsn, HASH(0x18d34d0) ]$VAR1 = 'dsn';
$VAR2 = {};


---
cheers!
san
smoking3vc.gif


print length "answer to life the universe and everything";
 
I don't know what you have in mind, and what is it that you want to do.
If you tell us what exactly is it that you want to do maybe we can be of more help.


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
Hi,
I'm just trying to experiment with Tie::Hash to learn more. The above code (in my first post) works great when I call $config{'san'} = 1, with STORE called first and everything checks out.

But when I change it to $config{'san'}->{'test'} = 1 (where %config is a Tie Hash) everything fails and instead FETCH is called first instead of STORE. I'm just trying to understand, what happens and why it happens, when I write something like $config{'san'}->{'test'} = 1

---
cheers!
san
smoking3vc.gif


print length "answer to life the universe and everything
 
Why you call it like this?
Do you want to create hash of hashes?
If you do then you have to change your 'print' cause this way its just print a value, which is the refernce to a hash (the one you a trying to create).


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
Hi,
yes, i'm trying to create hash of hashes. But as you can see when I do that with a TIED hash, when storing a value, FETCH is called first before STORE (which i don't understand). Also I see no way to access the hash of hashes using the TIED variable.

The print statement is just to debug. Its a reference to a hash (that's why I used the Dumper module too), but note that the hash to which it is referring is empty (another confusing thing)


---
cheers!
san
smoking3vc.gif


print length "answer to life the universe and everything
 
Ok, I tried to give others a chance to answer but it looks like I'm gonna have to give it a go.

You are trying to create a hash of hashes and using a tied hash to manage it.

You need to understand that perl does not actually directly support such a structure but magically creates it from a hash of [red]references[/red] to hashes. This is critical to understand what is going on.

When you assign to the top level (a hash element), it can use the tied "store" functionality as you would expect.

However, when you try to store something in a subhash (hash within hash element), perl first has to get the address of the hash that it's going to write to. That address is the reference that I mentioned earlier and is stored in one of the top level hash elements. Consequently, to get this address or reference, it needs to [red]read[/red] it from your top level hash. Hence the read where you were expecting a write.

Also, since only the top level hash is tied, when perl has obtained the reference to the subhash, that is not a tied hash and consequently any read to that will happen without calling any of your tied code.

Hopefully that explains your problem.

How you wish to solve it is completely another matter and something for you to consider.

Good Luck


Trojan
 
thanks Trojan.. that indeed was helpful! it looks like perl does a even more work for us behind our back without telling, than I previously thought ;)

---
cheers!
san
smoking3vc.gif


print length "The answer to life, universe & everything!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top