Writing a persistent perl script
bash, linux, perl, unix
Solution
If %my_hash in your example have moderate size in its final initialized state, you can simply use one of serialization modules like Storable, JSON::XS or Data::Dumper to keep your data in pre-assembled form between runs. Generate a new file when it is absent and just reload ready content from there when it is present.
Also, you've mentioned that you would call this script in loops. A good strategy would be to not call script right away inside the loop, but build a queue of arguments instead and then pass all of them to script after the loop in single execution. Script would set up its environment and then loop over arguments doing its easy work without need to redo setup steps for each of them.
Problem
I am trying to write a persistent/cached script. The code would look something like this: ``` ... Memoize('process_fille'); print process_file($ARGV[0]); ... sub process_file{ my $filename = shift; my ($a, $b, $c) = extract_values_from_file($filename); if (exists $my_hash{$a}{$b}{$c}){ return $my_hash{$a}{$b}{$c}; } return $default; } ``` Which would be called from a shell script in a loop as follows ``` value=`perl my_script.pl`; ``` Is there a way I could call this script in such a way that it will keep its state. from call to call. Lets assume that both initializing '%my_hash' and calling extract_values_from_file is an expensive operation. Thanks