Perl6相当于Perl的'商店'或'使用Storable'

con*_*con 9 perl6 raku

我试图将写得非常慢的哈希写入数据文件,但我不确定Perl6与Perl5相比如何做到这一点.这是一个类似的问题在Perl 6中的文件中存储中间数据,但我不知道如何使用那里写的任何东西,特别是messagepack.

我想看看Perl6相当于

my %hash = ( 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4);
use Storable;
store \%hash, 'hash.pldata';
Run Code Online (Sandbox Code Playgroud)

然后阅读

my $hashref = retrieve('hash.pldata');
my %hash = %{ $hashref };
Run Code Online (Sandbox Code Playgroud)

这是内置于Perl5,它非常简单,我不需要安装任何模块(我喜欢它!),但我怎么能在Perl6中做到这一点?我在手册中没有看到它.他们似乎在用https://docs.perl6.org/routine/STORE谈论其他事情STORE

Ste*_*ker 4

这个怎么样?好吧,虽然效率不高,Storable但似乎有效......

#!/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;
Run Code Online (Sandbox Code Playgroud)

我明白了

$ 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
Run Code Online (Sandbox Code Playgroud)