how do i tie a reference to a hash

Discussion in 'Perl' started by pillbug, Feb 7, 2004.

  1. pillbug

    pillbug Guest

    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.
     
    pillbug, Feb 7, 2004
    #1
    1. Advertising

  2. pillbug

    Jim Gibson Guest

    In article <LgXUb.9525$>, pillbug
    <> wrote:

    > 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.
     
    Jim Gibson, Feb 12, 2004
    #2
    1. Advertising

  3. pillbug

    Guest

    pillbug <> wrote:
    > 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
     
    , Feb 16, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SpecialK

    Tie a file to a hash

    SpecialK, Feb 19, 2004, in forum: Perl
    Replies:
    1
    Views:
    480
    Ben Morrow
    Feb 19, 2004
  2. SpecialK

    Tie a large file to a Hash

    SpecialK, Feb 19, 2004, in forum: Perl
    Replies:
    0
    Views:
    463
    SpecialK
    Feb 19, 2004
  3. rp
    Replies:
    1
    Views:
    562
    red floyd
    Nov 10, 2011
  4. Darius
    Replies:
    10
    Views:
    712
    Darius
    Feb 12, 2004
  5. botfood

    tie() with DB_File not tie()ing ?

    botfood, Apr 24, 2006, in forum: Perl Misc
    Replies:
    23
    Views:
    476
    botfood
    Apr 26, 2006
Loading...

Share This Page