On Thu, Aug 23, 2001 at 07:52:04PM +0200, Birgit Kellner wrote:
> my ($db_key, %rec);
> # %orderhash contains numeric keys and values, like ('43' => '5', '20' => 
> '17'); I want to call the sub get_record for each of these keys.
> # NOTE: this works perfectly, with the same syntax, in other parts of the 
> same file.


> # might the problem be that I'm embedding the call to the sub in a foreach 
> loop here?

No.

 
> foreach (keys %orderhash) {
>               $db_key = $_;
>               print "Database key: $db_key"; #this works fine, so $db_key HAS a 
>numeric 
> value
>               %rec = &get_record($db_key); # here's the problem: %rec doesn't get 
> returned

What does get returned?


>               print "Number: $rec{'number'}"; # ouch, no value.

Is this what you're using to verify %rec has no keys?  Have you tried doing
a dump of %rec to see what it does have?

For example:

    use Data::Dumper;
    print Data::Dumper->([\%rec])->Terse(1)->Useqq(1)->Dump;


>               }
> 
> # this is the called sub, contained in another file that we're requiring.
> # other file also runs under use strict - no errors reported. all variables 
> not declared in the sub are declared globally.
> # I won't give all explanations about what this does just yet - hoping that 
> this is a simple logic error which I'm too blind to see ...
> 
> sub get_record {
> # --------------------------------------------------------
> # Given an ID as input, get_record returns a hash of the
> # requested record or undefined if not found.
> 
>       my ($key, $found, $line, @data, $field, $restricted, $i, %rec);
>       $key = $_[0];   
>       $found = 0;
>       ($restricted = 1) if ($auth_modify_own and !$per_admin);
> 
>       open (DB, "<$db_file_name") or &cgierr("error in get_records. unable to 
> open db file: $db_file_name.\nReason: $!");
>       if ($db_use_flock) { flock(DB, 1); }    
>       LINE: while (<DB>) {
>               (/^#/)      and next LINE;
>               (/^\s*$/)   and next LINE;
>               $line = $_;     chomp ($line);          
>               @data = &split_decode($line);
>               next LINE if ($restricted and ($db_userid ne $data[$auth_user_field]));
>               if ($data[$db_key_pos] eq $key) {
>                       $found = 1;
>                       for ($i = 0; $i <= $#db_cols; $i++) {  # Map the array columns 
>to a hash.
>                               $rec{$db_cols[$i]} = $data[$i];
>                       }
>                       last LINE;
>               }
>       }
>       close DB;       

>       $found ?
>               (return %rec) :
>               (return undef);

This can be simplified to simply: return %rec

If %rec has data, you'll get key-value pairs; if not, you get an empty list,
and thus an empty hash.  As it is, if a record couldn't be found, and you use
get_record() like you do above, e.g. %rec = get_record(), %rec will have one
key, undef, with a value of undef, and you'll get a warning.

I doubt if this has any bearing on your problem, I'm just suggesting it as a
simplification.


> }


> I've also tried calling the sub without "&", but it doesn't make a 
> difference.

It won't.  In your case, the '&' has no practical purpose.


Michael
--
Administrator                      www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to