Gossamer Forum
Quote Reply
%rec
I am having a problem with forcing to add a record if no record exists and have boiled it down to the following:

Some of this may not make any sense, but I tried a lot over the last 2 1/2 hours and was desperate for anything!

I printed out %rec to see what would happen. I found out that if a record existed then it would return a value of 18/32. If a record did not exist it would return a value of 1/8. I tested this with 3 id's that had records and 3 that didn't and it worked exactly like this every time.

So, why would it return a value if the record did not exist?

Here is the code I have. The code in blue I tested seperatly and it does what it is supposed to. The code in red is the problem.

In my sub html_home

if ($per_add) {


%rec = &get_record($db_userid);


if (!%rec) {
print "Location: $db_script_url?db=default&uid=$db_uid&add_form=1\n\n";
return;
}
}



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


So going with what I am finding as the value of %rec I changed the sub html_home to:

if ($per_add) {
%rec = &get_record($db_userid);
if (%rec == "1/8") {
print "Location: $db_script_url?db=default&uid=$db_uid&add_form=1\n\n";
return;
}
}


And what this does is forces to add if there is no record and doesn't if there is a record.

Seems like a strange workaround but it's working so far. However, I'm a little hesitant if it will work every time.

Any words of wisdom?

Thanks!
Adam

Quote Reply
Re: %rec In reply to
I have my thoughts on this one but I have a feeling I may be wrong so I'll tread with caution.

I'd like to know what happens if you forced get_record to return 'undef' as soon as it is called and then tested for (!%rec).

George E.D. Burville
Ed: www.appbe.com
Quote Reply
Re: %rec In reply to
Instead of having it print out %rec, have it print out $rec{$db_key}.

I would use

Code:

if ($per_add) {
%rec = &get_record($db_userid);
unless ($rec{$db_key}) {
print "Location: $db_script_url?db=default&uid=$db_uid&add_form=1\n\n";
return;
}
}
JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: %rec In reply to
That works too! Any idea why the original didn't work?

Thanks,
Adam

Quote Reply
Re: %rec In reply to
Hashes are strange animals. There's a lot of stuff going on "behind the scenes" that I haven't bothered to worry about. I just know that the best way to test for the existence of a hash is to check for one element that you know must be there.

JPD
http://www.jpdeni.com/dbman/
Quote Reply
Re: %rec In reply to
JPD... My curiosity got the better of me and I spent over an hour using Perl Builder with a test program to check what Adam has said and looking for answers. I must admit, your comment hit the nail on the head.

Adam... I can't get your values to print but certainly the "1/8" value to show that a record is undefined actually works. The 'if (!%rec)' statement doesn't work at all.

Both... I don't have an answer as to why the (!%rec) will not work but I have found at least one instance of it being used in Links 2.0 code. Where do we go from here?

George E.D. Burville
Ed: www.appbe.com