in reply to Dereferencing hashrefs

The two methods which you describe above for dereferencing a hashref structure are correct as per perlref and should work.

A couple of other debugging points which you may want to consider:

 

perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'

Replies are listed 'Best First'.
Re: Re: Dereferencing hashrefs
by schnarff (Acolyte) on Feb 11, 2002 at 12:35 UTC
    Thank you all for your responses -- they're much more than I expected.

    Since this reply seemed to have the most solid ways for checking that my fetchrow_hashref() call was working, I tried the suggestions therein. Sadly, I found that I was getting a valid hashref returned, as:
    print "Errors are: $sth->err\n"; print "I got out: ref($rec_ref)\n";
    Gives me:
    Errors are: DBI::st=HASH(0xe2478)->err I got out: ref(HASH(0xf9d14))
    both of which seem to indicate that I do, indeed, have a valid hashref to work with.

    To expand on what's going wrong, let me pull out the following bit of code from my program:
    if ($rec_ref->{'company'} ne "") { %ref = %$rec_ref; $found = 1; } $dbh->disconnect(); if ($found) { return %rec; } else { return undef; }
    I know from the database that there will *always* be a company value returned; thus, I was using that as a sanity-check that my hashref was good. From there, I set a flag on how to return from the subroutine I'm in; if it's good, I return the hash that should be filled by dereference from my hashref, otherwise undef.

    What's actuall happening here is that I *am* getting a value for $rec_ref->{'company'} (I've checked that with a print statement), and I know I'm returning a hash -- but the subroutine which uses that hash is acting as if it's empty. That subroutine is known good from other activities within my script, so I won't paste it here.

    Further comment upon this expanded code would be greatly appreciated. Again, thanks for all of your help.

    Alex Kirk
      Method calls aren't interpolated... What is the output of:
      print "Errors are: " . $sth->err . "\n";

      -Blake

      1. I notice you call your hash %ref, but return %rec - maybe this is your problem?

      2. You know $rec_ref is OK, and you know your subroutine's hash is not OK, and you know that %ref = %$rec_ref is OK (because we've told you ;-) ). So work backwards to find out where the bug is. Use perl -d and run through this subroutine, checking your hash using the "x" command, e.g. x %ref.

      dave hj~

      I'm not sure why you want to return a hash instead of a reference, but there's no need to create %ref or %rec.

      $dbh->disconnect(); return %$rec_ref unless $rec_ref->{company} eq ''; return undef;



      HTH,
      Charles K. Clarkson
      Clarkson Energy Homes, Inc.