Cannot use a colon pair in `qqww` or` qqww: to` struct

I want to generate a json string using Class, I am overwriting a method gistso that it prints what I want:

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","ak":"$!ak","uu":"$!uu","ifo":"$!ifo","st":"$!st"}
    EOF
  }
}

say Stay.new(uu => $uu, ifo => $ifo, st => $st); 

but with an error:

===SORRY!=== Error while compiling /Users/ohmycloud/Desktop/stay.pl6
Confused
at /Users/ohmycloud/Desktop/stay.pl6:18
------>     {"ev":⏏"app","ak":"$!ak","uu":"$!uu","ifo":"$!i
    expecting any of:
        colon pair

I want to:

{"ev":"app","ak":"7111ac873c9dcd5fc335ded47630d050","uu":"15778787898988090","ifo":"true","st":"1523451601875"}

Why can't I use a colon pair in qqwwor qq:tostruct?

+4
source share
2 answers

You can use unquoting :

method gist() {
    return '{"ev":"app","ak":"\qq[$!ak]","uu":"\qq[$!uu]","ifo":"\qq[$!ifo]","st":"\qq[$!st]"}':
}

which will return

{"ev":"app","ak":"7111ac873c9dcd5fc335ded47630d050","uu":"15778787898988090","ifo":"true","st":"1523451601875"}

Colonies will interfere if you use qqwwor similar.

+4
source

{}have special meaning in double quotes. They should avoid

qq:to「EOF」;
\{"ev":"app","ak":"$!ak","uu":"$!uu","ifo":"$!ifo","st":"$!st"\}
EOF

you need qq, not the ones qqwwthat make up the list.

you can use fmt

(:ev<app>, :$!ak, :$!uu, :$!ifo, :$!st).fmt('"%s":"%s"', ',').fmt('{%s}')

or JSON::Fast

require JSON::Fast <&to-json>;
{:ev<app>, :$!ak, :$!uu, :$!ifo, :$!st}.&to-json

or even

require JSON::Fast <&to-json>;
self.^attributes.map( {.name => .get_value(self)} ).Hash.&to-json
+6
source

Source: https://habr.com/ru/post/1696071/


All Articles