Home > Raku > Mimicing quotes

Mimicing quotes

My quest to shorten code is coming along nicely. One thing that is still quite wordy is object creation. To fill a pipe with life we need instances of Proc::Async.

my $find = Proc::Async.new: </usr/bin/find /tmp>;

As I showed earlier operators are deboilerplaters. We will need that argument list that is currently going to .new. It would be nice to reduce anything else to just two letters. Let’s define a sub prefix to do so.

sub prefix:<px>(List:D \l) is looser(&infix:<,>) { say ‚Proc::Async.new: <‘, l.Str, ‚>‘; };

px</usr/bin/find /tmp>; # OUTPUT Proc::Async.new: </usr/bin/find /tmp>
px ‚/usr/bin/find‘, ‚/tmp‘; # OUTPUT Proc::Async.new: </usr/bin/find /tmp>

By using is looser(&infix:<,>) we tell the compiler to create a List and then call the sub called px with a funny syntax. By doing so we mimic a quote construct that is used to implement qx and are able to leave a space out. There is a catch though.

px«/usr/bin/find /tmp»;

# OUTPUT:
# ===SORRY!=== Error while compiling /home/dex/projects /raku/lib/raku-shell-piping/EVAL_0
# Two terms in a row
# at /home/dex/projects/raku/lib/raku-shell-piping/EVAL_0:1
# ------> px«/usr/⏏bin/find /tmp;
#     expecting any of:
#         infix
#         infix stopper
#         statement end
#         statement modifier
#         statement modifier loop

The grammar seams to be confused here. Since there is an alternative to list quotes with interpolation, I shall bugrepot and soldier on.

My nagging via R#3799 has been fruitful and provided a solution for this case.

class C {
    has $.state is rw;
}

my \px = C.new;

multi postcircumfix:<{ }>(C:D, $s, Bool :$adverb = False) {
    Proc::Async.new: $s;
}

multi postcircumfix:<{ }>(C:D, @a, Bool :$adverb = False) {
    Proc::Async.new: @a;
}

multi infix:<|»>(Proc::Async:D $l, Proc::Async:D $r, :$different-adverb = "non-given") {
    dd $l;
    dd $r;
}

px<ls>;
px«ls»;
my $a = 42;
px<ls 1 2 3 $a>;
px«ls $a»;
px«ls $a»:adverb;

px{'foo' ~ 41.succ};

px«ls $a»:adverb |» (px«sort»:adverb) :different-adverb(42);

This is providing all I need to mimic qx in most of it various forms. The instance of C is used as a placeholder for the compiler to hold onto. It can have state that is global to all calls to postcircumfix:<{ }>(C:D, ...). This might came in handy later.

Shell::Piping has caused 3 bug reports so far. I do feel like I’m the first to tread this swamp. If I don’t make it, please hug my friends and delete my browser history.

Categories: Raku
  1. p6steve
    July 25, 2020 at 12:52

    This is awesome. We just need to get some more bodies in the swamp to walk over!

  1. July 27, 2020 at 22:47

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: