We have some (about 50-100) table that contain things like eg zip-code, department addresses, typecodes, and so on ad nauseam. Some are small, some are big, others huge - it varies.
In the frontend code (on apache) we eg. need to create selectboxes, that let the user choose between different options, based on the content of the constant tables.
A possibility would be to fetch the data everytime it is needed:
or something like that.my $zip = $zip_server->get_zip_codes(); print "selectbox-header"; for (@$zip) { print "selectbox line"; } print "selectbox-end";
This will take quite a while and soon you discover the need for caching the data. So you try something like:
...in common initialisation code... our $zip; $zip=$zip_server->get_zip_codes(); ...where the zip-code is needed... print "selectbox-header"; for (@$zip) { print "selectbox line"; } print "selectbox-end"
This is fast, but it takes more and more memory as the number of constants rise. So the next version could be something like:
That is fast, easy and does not comsume unnessacry amounts of memory. the downside is that you have to remember to call the zip_init before you use $zip....in the common initialisation code... our $zip; sub zip_init { $zip = $zip_server->get_zip_codes() unless $zip; } ...at every use... zip_init(); print "selectbox-header"; for (@$zip) { print "selectbox line"; } print "selectbox-end";
So I would like something like:
Note no zip_init, fetch calls. Just the plain ordinary access to a variable....in initialisation section... our $zip; tie $zip .... # magic here sub ZIP::TIE::FETCH { # smoke and mirrors here $zip = $zip_server->get_zip_codes(); untie $zip; # and leave the data in $zip } ..and where we use it... print "selectbox-header"; for (@$zip) { print "selectbox line"; } print "selectbox-end";
At the first reference to the variable the tie magic clicks in and retrieves the data, and removes the magic, leaving the 'naked' variable.
Giving
Did that clarify what I need?
In reply to Re: Re: Using tie to initialize large datastructures
by htoug
in thread Using tie to initialize large datastructures
by htoug
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |