perl - 我怎样才能持久化一个大的 Perl 对象以便在运行之间重复使用?

标签 perl serialization persistence

我有一个大型 XML 文件,用 XML::Simple 解析需要 40 多秒.

我希望能够缓存生成的解析对象,以便在下次运行时我可以只检索解析的对象而不是重新解析整个文件。

我看过使用 Data::Dumper但是文档有点缺乏如何从磁盘文件存储和检索其输出。我看过的其他类(例如 Cache::Cache 似乎是为存储许多小对象而设计的,而不是一个大对象。

谁能推荐一个为此设计的模块?

编辑 . XML 文件是 ftp://ftp.rfc-editor.org/in-notes/rfc-index.xml ,我选择了Storable用于加快后续运行。更改 XML 解析器将需要非常重要的代码更改。

在我的 Mac Pro 上使用 XML::Simple 读取整个文件的基准数据与 Storable是:

      s/iter  test1  test2
test1   47.8     --  -100%
test2  0.148 32185%     --

最佳答案

Data::Dumper 实际上非常简单。如果你的对象是一个 hashref $HashRef :

# Write
open(FILE, ">your_filename") || die "Can not open: $!";
print FILE Data::Dumper->Dump([$HashRef],["HashRef"]);
close(FILE) || die "Error closing file: $!";

# Read
my $HashRef;
$HashRef = eval { do "your_filename" };
   # Might need "no strict;" before and "use strict;" after "do"
die "Error reading: $@" if $@;
# Now $HashRef is what it was before writing

另一个不错的选择是使用 Storable .从 POD:
use Storable;
store \%table, 'file';
$hashref = retrieve('file');

有关各种选项的非常好的指南(以及 Data::Dumper 使用的更好示例),请参阅 Chapter 14 "Persistence" of brian d foy's "Mastering Perl" book

关于perl - 我怎样才能持久化一个大的 Perl 对象以便在运行之间重复使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2903919/

相关文章:

c# - CosmosClient : Custom json converter works out of the box with NewtonSoft, 不包含 System.Text.Json

hibernate - 如何在jsp页面中从表(持久性数据库表)中获取列到 'select'的 'form'列表中

java - sonarQube 报告错误 - 使字段 transient 或可序列化

html - 如何从 Controller 返回文件以供查看?

linux - xsubpp 编译器不存在

regex - 帮助 perl 正则表达式规则

c# - 需要从非托管 C++ 代码调用托管代码

java - 简单 id 到 java 值映射表的高效持久存储

java - JPA AccessType.Property 无法从嵌套类中检索值

perl - 不能在新版本的 Perl 中使用 "my $_"