Quick actions

cmd+k|ctrl+k

Navigation

Languages

Rosetta-IPC-via-Named-Pipes

Snippet info

Language

Raku

Visibility

public

Author

221a86612c7785cb6094f25b034cba9994736288

Created

2025-10-15T16:20:50.984117Z

Updated

2025-10-16T20:35:12.504085Z

# see https://rosettacode.org/wiki/IPC_via_named_pipe#Raku

# Issues: 
# - need to touch "in" before strings can be echoed into "in", 
# - need to cat "out" before new strings can be echoed into "in"

# use mkfifo directly from libc
use NativeCall;
sub mkfifo(Str $pathname, int32 $mode --> int32) is native('libc.so.6') is symbol('mkfifo') {*}

# create filenames and named pipes in TMP
my ($in, $out) = <in out>.map: -> $name { $*TMPDIR.add("$name-$*PID") };
   ($in, $out).map:            -> $name { mkfifo(Str($name), 0o666)   };

('In :: Out', "$in :: $out")>>.say;

my atomicint $char-count = 0;

my Supply $in-changed = $in.dirname.IO.watch.grep({ .path eq $in and
                                                    .event ~~ FileChanged });

react { whenever $in-changed {
               # calculate new char-count
               given $in.open(:r) {
                    .lock: :shared;
                    say $char-count ⚛+= .readchars.chomp.codes;
                    .close;
                }                

                # write new char-count to $out
                given $out.open(:w) {
                    .lock;
                    .spurt: $char-count;
                    .close;
                }
        }
}
INFO