问题描述
我正在尝试将写入速度非常慢的哈希写入数据文件,但我不确定 Perl6 与 Perl5 相比如何做到这一点.这是一个类似的问题 将中间数据存储在 Perl 6 中的文件中 但我不知道如何使用那里写的任何东西,特别是消息包.
I am attempting to write a hash, which is written very slowly, into a data file, but am unsure about how Perl6 does this in comparison to Perl5. This is a similar question Storing intermediate data in a file in Perl 6 but I don't see how I can use anything written there, specifically messagepack.
我希望看到与 Perl6 等效的
I'd like to see the Perl6 equivalent of
my %hash = ( 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4);
use Storable;
store \%hash, 'hash.pldata';
然后阅读
my $hashref = retrieve('hash.pldata');
my %hash = %{ $hashref };
这是 Perl5 内置的,超级简单,我不需要安装任何模块(我喜欢它!),但是我如何在 Perl6 中做到这一点?我在手册中没有看到它.他们似乎在用 STORE
https://docs.perl6 谈论其他事情.org/routine/STORE
This is built in to Perl5, it's super easy, I don't need to install any modules (I love it!), but how can I do this in Perl6? I don't see it in the manual. They appear to be talking about something else with STORE
https://docs.perl6.org/routine/STORE
推荐答案
这个怎么样?好吧,不如 Storable
有效,但它似乎有效....
How about this? OK, not as efficient as Storable
but it seems to work....
#!/usr/bin/perl6
my $hash_ref = {
array => [1, 2, 3],
hash => { a => 1, b => 2 },
scalar => 1,
};
# store
my $fh = open('dummy.txt', :w)
or die "$!\n";
$fh.print( $hash_ref.perl );
close($fh)
or die "$!\n";
# retrieve
$fh = open('dummy.txt', :r)
or die "$!\n";
my $line = $fh.get;
close($fh)
or die "$!\n";
my $new_hash_ref;
{
use MONKEY-SEE-NO-EVAL;
$new_hash_ref = EVAL($line)
or die "$!\n";
}
say "OLD: $hash_ref";
say "NEW: $new_hash_ref";
exit 0;
我明白了
$ perl6 dummy.pl
OLD: array 1 2 3
hash a 1
b 2
scalar 1
NEW: array 1 2 3
hash a 1
b 2
scalar 1
这篇关于Perl6 相当于 Perl 的“存储"或“使用可存储"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!