问题
For example, I've installed the Cro module, when I run my simple code:
my %headers = {Authorization => OAuth realm="", oauth_consumer_key="xxxxxxxxxxxxxxxx", oauth_nonce="29515362", oauth_signature="KojMlteEAHlYjMcLc6LFiOwRnJ8%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1525913154", oauth_token="xxxx-xxxxxxxxxxxxxxxxxx", oauth_version="1.0", User-Agent => Cro};
my $resp = await Cro::HTTP::Client.get: 'http://api.fanfou.com/statuses/home_timeline.json',
headers => [
user-agent => 'Cro',
content-type => 'application/json;charset=UTF-8',
|%headers
];
say $resp.header('content-type'); # Output: application/json; charset=utf-8;
my Str $text = await $resp.body-text();
And it says 'Could not parse media type application/json; charset=utf-8;
Died with the exception:
Could not parse media type 'application/json; charset=utf-8;'
in method parse at /Users/ohmycloud/.perl6/sources/5B710DB8DF7799BC8B40647E4F9945BCB8745B69 (Cro::MediaType) line 74
in method content-type at /Users/ohmycloud/.perl6/sources/427E29691A1F7367C23E3F4FE63E7BDB1C5D7F63 (Cro::HTTP::Message) line 74
in method body-text-encoding at /Users/ohmycloud/.perl6/sources/427E29691A1F7367C23E3F4FE63E7BDB1C5D7F63 (Cro::HTTP::Message) line 83
in block at /Users/ohmycloud/.perl6/sources/F870148C579AB45DEB39F02722B617776C3D6D5F (Cro::MessageWithBody) line 49
It seems that application/json; charset=utf8;
is not a valid content-type
, so I add a test:
use Cro::MediaType;
use Test;
sub parses($media-type, $desc, &checks) {
my $parsed;
lives-ok { $parsed = Cro::MediaType.parse($media-type) }, $desc;
checks($parsed) if $parsed;
}
parses 'application/json; charset=utf-8;', 'application/json media type with charset', {
is .type, 'application', 'Correct type';
is .subtype, 'json', 'Correct subtype';
is .subtype-name, 'json', 'Correct subtype name';
is .tree, '', 'No tree';
is .suffix, '', 'No suffix';
is .Str, 'application/json; charset=utf-8;', 'Stringifies correctly';
};
done-testing;
And the output is:
not ok 1 - application/json media type with charset
# Failed test 'application/json media type with charset'
# at cro_media.pl6 line 6
# Could not parse media type 'application/json; charset=utf-8;'
1..1
# Looks like you failed 1 test of 1
the source code seems locate in the /Users/ohmycloud/.perl6/sources/5B710DB8DF7799BC8B40647E4F9945BCB8745B69
file, and I add ';'?
after the TOP
token:
token TOP { <media-type> ';'? }
save, and run my code again, but the error is the same. So how to make the change work? In Perl 5, I can just edit my .pm
module, but in Perl 6, I dont't know what to do.
回答1:
In this answer in zef's issues, they state that "installations are immutable". It's probably a better option if you download Cro from its source, patch it and install again so that your application picks up the new version.
It might also happen that 'application/json' does not admit that charset declaration, or that there should be no space behind the ;. But the main issue here is that you shouldn't edit modules once installed.
回答2:
As jjmerelo mentioned, installations are immutable, one solution is to download source code(include META6.json file), edit the code you want, then:
zef install . --/test
For simple test, it's ok for me.
As for application/json; chartset=utf-8;
can't parse, I add a ;
in MediaType.pm6
's token
token, make it possible to include ;
(maybe it's a bug, I don't know):
token token { <[A..Za..z0..9;!#$%&'*+^_`{|}~-]>+ }
installed locally, and it parse ok now.
来源:https://stackoverflow.com/questions/50264214/how-to-edit-the-source-code-in-module-after-installed-it-using-zef