in reply to Passing Hashes

This didnt seem to work. Here is an exact copy of the code in both files

file: index.cgi
#!/usr/bin/perl use fetcher; use strict; print "Content-type: text/html\n\n"; my $dat; if($ENV{'CONTENT_LENGTH'} !=0) { $dat = fetcher->gather(); foreach(keys %dat) { print "$_ = $dat{$_}<br>\n"; } } print <<endofhtml; <form action="index.cgi" method="post"> <input type="text" name="b1"><input type="text" name="b2"> <input type="submit"> </form> endofhtml

file: fetcher.pm
package fetcher; sub gather { my %tmp; $tmp{'name'} = "ironcom"; $tmp{'pwd'} = "pwd"; return \%tmp; } 1;

Now, if I do that using strict It tells me that I need to declare %dat. so then I use my %dat alogn with my $dat and it returns nothing.
If I do %dat = fetcher->gather(); it prints
HASH(0x8230e2c) = 

Any other suggestions

Replies are listed 'Best First'.
Re^2: Passing Hashes
by Joost (Canon) on Oct 24, 2004 at 00:47 UTC
Re^2: Passing Hashes
by Fletch (Bishop) on Oct 24, 2004 at 00:40 UTC

    It doesn't work because the scalar $dat you've declared has nothing to do with the hash %dat. Hashes and scalars (and arrays for that matter) have completely different namespaces. $a, @a, %a all can exist simultaneously and are completely disjoint from one another. You need to (re-)read perldoc perldata.

Re^2: Passing Hashes
by TheEnigma (Pilgrim) on Oct 24, 2004 at 00:51 UTC
    $dat contains a reference to the hash called %tmp, because that's what you returned from gather when you (correctly) said return \%tmp;. So you need to dereference it when you use it. To dereference $dat as a hash, you would say foreach(keys %$dat).

    TheEnigma