List Info

Thread: Life of a variable in a module




Life of a variable in a module
user name
2006-01-30 18:43:37
I have a module I've written, 'Tools.pm'. One of the
function in
Tools.pm will look up an identifier in a table in a database
and return
the value for the identifier. Since the table is rather
short and to
keep from having to access the disk all the time, I have the
function
store the values in a hash on the first invocation:

Code:

my %values; sub table_value($) { my $identifier = shift; if
(not
defined(%values)) { get the values from table and store in
%values } if
(not defined($values{$identifier})) { INSERT (NULL, $value)
INTO table;
$identifier = SELECT Last_Insert_ID(); $values{$identifier}
= $value; }
return($values{$identifier}); }


This function works fine, but, I notice that if I call the
function and
call several other functions from Tools.pm then call
'table_value()'
again, it seems to have lost all the values in %values and
will reload
the values into %values again.

Is this because the scope of %values is declared with my and
the
variable is going out of scope when I leave the function?
How can I keep
the values in the %values hash so the program doesn't
constantly access
the information from the database?



Carl.




_______________________________________________
Perl-Unix-Users mailing list
Perl-Unix-Userslistserv.ActiveState.com
To unsubscribe: http:/
/listserv.ActiveState.com/mailman/mysubs
[1]

about | contact  Other archives ( Real Estate discussion Medical topics )