> -----Original Message-----
> From: Ronald J Kimball [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, January 15, 2002 3:33 PM
> To: Tomasi, Chuck
> Cc: '[EMAIL PROTECTED]'
> Subject: Re: Maintaining a Cache of Hash References
> 
> 
> On Tue, Jan 15, 2002 at 03:25:54PM -0600, Tomasi, Chuck wrote:
> > @UserCache; # Place to store data already seen
> > sub GetUser
> > {
> >     my ($id, $user)=@_;     # record number and hash 
> reference to populate
>         ^^^^^^^^^^^^^^^
> > 
> >     if (defined($UserCache[$id])) {
> > 
> >         $user = $UserCache[$id];                            
>                 
> >         return(1);  
> 
> $user is lexically scoped to the body of the subroutine; this 
> assignment
> right before a return is pointless.
> 
> I guess you also have a global variable named $user, which is what you
> meant to set.  Global variables should generally be avoided; 
> have GetUser()
> return the value instead.
> 
> BTW, I don't see where the hashes come in.  @UserCache is an array.

$user is a hash reference (sort of like a pointer in C).  I call GetUser in
this fashion...

&inc::db::GetUser($val, \%urec);

When the database is read I have full access to the contents because I
passed urec by reference.  For example $urec{'FirstName'} would contain
"Chuck".  I'm using @UserCache in inc::db which is used from within many of
programs.  When a program is run, I may hit user ID 1041 several times.  The
first time, I want to get it from the database.  Subsequent requests to
&inc::db::GetUser() with the first arg 1041 should recognize that 1041
exists and get the contents from a saved area (@UserCache) which is an array
of hashes.  I'm just asking to retrieve the 1041'st one in this example.

Like I said earlier, GetUser works fine over and over if I don't involve
UserCache, hence passing and using hashes by reference isn't the problem.
The problem is trying to save and restore data from @UserCache.

Reply via email to