I do not believe that you need the "&" before the function call.

-----Original Message-----
From: Birgit Kellner [mailto:[EMAIL PROTECTED]]
Sent: Thursday, August 23, 2001 12:52 PM
To: [EMAIL PROTECTED]
Subject: is this sub called correctly? am I missing something?


I have a sub called and it doesn't return what it's supposed to return - am
I blind or is there something I am missing out on?
The script runs with "use strict", and no errors are reported.

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?

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
                print "Number: $rec{'number'}"; # ouch, no value.
                }

# 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);
}

I'd be really grateful for any hints as to what might be wrong - thanks in
advance.
I've also tried calling the sub without "&", but it doesn't make a
difference.

Birgit Kellner

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



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

Reply via email to