问题
I am working with documents compiled by Rakudo Perl and the documents can get updated.
I store the documents in a CompUnit::PrecompilationStore::File
How do I change an older version for a newer one?
The following program produces the same output, as if the newer version is not stored in CompUnit. What am I doing wrong?
use v6.c;
use nqp;
'cache'.IO.unlink if 'cache'.IO ~~ e;
my $precomp-store = CompUnit::PrecompilationStore::File.new(prefix=>'cache'.IO);
my $precomp = CompUnit::PrecompilationRepository::Default.new(store=> $precomp-store );
my $key = nqp::sha1('test.pod6');
'test.pod6'.IO.spurt(q:to/--END--/);
=begin pod
=TITLE More and more
Some text
=end pod
--END--
$precomp.precompile('test.pod6'.IO, $key, :force);
my $handle = $precomp.load( $key )[0];
my $resurrected = nqp::atkey($handle.unit,'$=pod')[0];
say $resurrected.contents[1].contents[0];
'test.pod6'.IO.spurt(q:to/--END--/);
=begin pod
=TITLE More and more
Some more text added
=end pod
--END--
# $precomp-store.unlock;
# fails with:
# Attempt to unlock mutex by thread not holding it
# in block <unit> at comp-test.p6 line 30
$precomp.precompile('test.pod6'.IO, $key, :force);
my $new-handle = $precomp.load($key)[0];
my $new-resurrected = nqp::atkey($new-handle.unit,'$=pod')[0];
say $new-resurrected.contents[1].contents[0];
The output is always:
Some text
Some text
Update: My original question had '$handle' not '$new-handle' where '$new-resurrected' is defined. There is no change to the output.
回答1:
I think the answer might be in the answer to other, similar question of yours here In general, CompUnits are intended to be immutable. If the object changes, the target needs to change too. As @ugexe says there,
$key
is intended to represent an immutable name, such that it will always point at the same content.
So you might be actually be looking for a precomp-like behavior, but you might not want to use the actual CompUnits to do that.
回答2:
As mentioned previously load
methods cache, not the method call to precomp
. You are expecting the parameter :force
to method precompile
to affect a later call to method load
-- this is incorrect. We can easily prove that :force
is working as expected for precompiling by skipping the first call to load
and seeing if the final call to load
shows the updated results:
use v6.c;
use nqp;
'cache'.IO.unlink if 'cache'.IO ~~ e;
my $precomp-store = CompUnit::PrecompilationStore::File.new(prefix=>'cache'.IO);
my $precomp = CompUnit::PrecompilationRepository::Default.new(store=> $precomp-store );
my $key = nqp::sha1('test.pod6');
'test.pod6'.IO.spurt(q:to/--END--/);
=begin pod
=TITLE More and more
Some text
=end pod
--END--
$precomp.precompile('test.pod6'.IO, $key, :force);
'test.pod6'.IO.spurt(q:to/--END--/);
=begin pod
=TITLE More and more
Some more text added
=end pod
--END--
# $precomp-store.unlock;
# fails with:
# Attempt to unlock mutex by thread not holding it
# in block <unit> at comp-test.p6 line 30
$precomp.precompile('test.pod6'.IO, $key, :force);
my $new-handle = $precomp.load($key)[0];
my $new-resurrected = nqp::atkey($new-handle.unit,'$=pod')[0];
say $new-resurrected.contents[1].contents[0];
which gives: Some more text added
来源:https://stackoverflow.com/questions/53736139/updating-a-program-in-a-compunitprecompilationstore