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

I am currently working on a perl module Im returning a hash %dbReturn to the caller. This is the final snippet of the code for the return
my $row_hash; $x=0; while($row_hash = $st->fetchrow_hashref) { if($wildCard==1) { foreach(@tables) { my $col; foreach $col(@$_) { $dbReturn{$x}{$col} = $row_has +h->{$col}; $x++; } } } else { foreach(@fieldNames) { $dbReturn{$x}{$_} = $row_hash->{$_}; #print "$dbReturn{$x}{$_}\n"; } } } return $dbReturn;
The caller is doing
my $dbData = $dbFunctions->query($queryString) my $something = $dbData->{}{};
I have tried putting known data into the {}{} and it does not work. I know the data in $dbReturn is present because if I call it from the module itself everything works fine. Everything is using strict and warnings. If I try to return the data as
return %dbReturn;
I get the following error apon execution Can't use string ("1/8") as a HASH ref while "strict refs" in use at test.cgi line 14. Here is a data demo datatable for $dbReturn
$dbReturn{0}
$dbReturn{0}{'ID'} $dbReturn{0}{'Firstname'} $dbReturn{0}{'Lastname'}
0 John Doe
$dbReturn{1}
$dbReturn{1}{'ID'} $dbReturn{1}{'Firstname'} $dbReturn{1}{'Lastname'}
1 Jane Doe
So My Question is, Im probably returning the hash wrong. How should I be returning it? Thanks in advance

Replies are listed 'Best First'.
Re: Returning Hashes
by cmeyer (Pilgrim) on Jun 03, 2005 at 23:05 UTC
    You are storing stuff in the hash %dbReturn, but returning the scalar $dbReturn. Perhaps you want return \%dbReturn?
Re: Returning Hashes
by duff (Parson) on Jun 03, 2005 at 23:10 UTC

    It looks like you really want to return a reference to a hash rather than the hash itself, so a return \%dbReturn; at the end of your subroutine should do it.

    Also keep in mind that $dbReturn bears no relation to %dbReturn. You seem to be conflating the two.

Re: Returning Hashes
by monarch (Priest) on Jun 04, 2005 at 00:36 UTC
    Even the most experienced programmer can make this mistake.. on more occasions than I'd care to admit I've accidently specified a $ instead of a \% etc.

    For this reason there are two tools available to help protect ourselves.. because odds are you'll do this again (through no intention on your part). They are:

    - never, ever give both a scalar and a hash the same name! It's possible and valid to do this but fraught with danger..!

    - try using the following bit of code whenever you're not seeing what you expect (I do this all the time):

    { use Data::Dumper; print( Dumper( $dbReturn ) . "\n"; }

    The second tip is what I'll recommend the most, along with pulling out the debugger. I asked my first manager in a software engineering role what his best tip for coding was - and he said "learn to use the debugger". Now that I'm a bit older I know what he meant! Anyway, enough rambling..

    PS I think your question was laid out very nicely.. thanks for going to the effort to present a well structured question.

Re: Returning Hashes
by Anonymous Monk on Jun 03, 2005 at 23:15 UTC
    Both of you were right I forgot my \ before the %dbReturn at the end of the routine. Everything works as it should now. Have yourselves a great weekend!