编写一个持久的Perl脚本 [英] Writing a persistent perl script

查看:155
本文介绍了编写一个持久的Perl脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个持久/缓存的脚本。在code会是这个样子:

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

这将在一个shell脚本在一个循环中调用如下:

Which would be called from a shell script in a loop as follows

value=`perl my_script.pl`;

有没有一种方法我可以打电话给这个脚本以这样一种方式,它会保持其状态。从电话呼叫。让我们假定这两个初始化'%my_hash,并呼吁extract_values​​_from_file是昂贵的操作。

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.

感谢

推荐答案

如果在你的榜样%my_hash在其最后的初始化状态大小适中,您可以简单地使用像的耐储藏,的 JSON :: XS 或的数据::自卸车以保证您的数据在运行之间pre-组装形式。生成一个新文件,当它不存在,只是重新从那里准备内容时,它是present。

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.

这篇关于编写一个持久的Perl脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆