我想使用Class生成一个json行,我会覆盖gist方法,以便它打印出我想要的内容:
@H_502_28@解决方法
my $ak = '7111ac873c9dcd5fc335ded47630d050'; my $st = '1523451601875'; my $ifo = 'true'; my $uu = "15778787898988090"; class Stay { has $.ak is rw = '7111ac873c9dcd5fc335ded47630d050'; has $.uu is rw; has $.ifo is rw; has $.st is rw; method gist() { #return qqww/{"ev":"app","ak":"$!ak","uu":"$!uu","ifo":"$!ifo","st":"$!st"}/; return qqww:to「EOF」; {"ev":"app","st":"$!st"} EOF } } say Stay.new(uu => $uu,ifo => $ifo,st => $st);
但失败了:
===SORRY!=== Error while compiling /Users/ohmycloud/Desktop/stay.pl6 Confused at /Users/ohmycloud/Desktop/stay.pl6:18 ------> {"ev":⏏"app","ifo":"$!i expecting any of: colon pair
我想要的是:
{"ev":"app","ak":"7111ac873c9dcd5fc335ded47630d050","uu":"15778787898988090","ifo":"true","st":"1523451601875"}
为什么我不能在qqww或qq中使用冒号对:结构?
你可以使用
unquoting:
method gist() { return '{"ev":"app","ak":"\qq[$!ak]","uu":"\qq[$!uu]","ifo":"\qq[$!ifo]","st":"\qq[$!st]"}': }
将返回
{"ev":"app","st":"1523451601875"}
如果你使用qqww或类似的话,冒号会妨碍你.