how do i tie a reference to a hash

P

pillbug

Hi,

I'm trying to tie a hash that is returned from a DBI
method called fetchrow_hashref. All I want to do is
override the FETCH method to validate my key names.

But fetchrow_hashref returns a reference to a hash, not a hash, and tie
seems to want a hash. Or maybe I want to tie it as a scalar?

How can I do this? I'm sort of new to perl.
 
J

Jim Gibson

pillbug said:
Hi,

I'm trying to tie a hash that is returned from a DBI
method called fetchrow_hashref. All I want to do is
override the FETCH method to validate my key names.

But fetchrow_hashref returns a reference to a hash, not a hash, and tie
seems to want a hash. Or maybe I want to tie it as a scalar?

How can I do this? I'm sort of new to perl.

Why don't you make a hash copy:

my $hash_ref = fetchrow_hashref(...);
my %hash = %{$hash_ref}

and tie %hash ?

If you are new to perl, why are you trying to tie hash references? I
programmed in Perl for 10 years and have yet to use Tied variables.

FYI: this newsgroup is defunct. Try comp.lang.perl.misc in the future.
 
T

thumb_42

pillbug said:
Hi,

I'm trying to tie a hash that is returned from a DBI
method called fetchrow_hashref. All I want to do is
override the FETCH method to validate my key names.

But fetchrow_hashref returns a reference to a hash, not a hash, and tie
seems to want a hash. Or maybe I want to tie it as a scalar?

How can I do this? I'm sort of new to perl.

There are probably several ways to do this...

One is to pass the reference as a parameter to TIEHASH, then use the
underlying reference to store/fetch things.

The other, (if this is what you want) is to dereference the hash prior
to tie():

tie %{$my_hash_ref} ...

Jamie
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top