Skip to content

Commit 343fca6

Browse files
committed
Proc::Async basics
1 parent d1c43b7 commit 343fca6

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

lib/Language/concurrency.pod

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,68 @@ each time it is called. This could be used, for instance, to fan-out a
447447
L<Supply> to one or more L<Channel>s to provide for different interfaces
448448
in a program.
449449
450+
=head2 Proc::Async
451+
452+
L<Proc::Async> builds on the facilities described to run and interact with
453+
an external program asynchronously:
454+
455+
my $proc = Proc::Async.new('echo', 'foo', 'bar');
456+
457+
$proc.stdout.tap(-> $v { print "Output: $v" });
458+
$proc.stderr.tap(-> $v { print "Error: $v" });
459+
460+
say "Starting...";
461+
my $promise = $proc.start;
462+
463+
await $promise;
464+
say "Done.";
465+
466+
# Output:
467+
# Starting...
468+
# Output: foo bar
469+
# Done.
470+
471+
The path to the command and any arguments are supplied to
472+
the constructor but the program will not be started until
473+
L<start|/type/Proc::Async#method_start> is called, which will return
474+
a L<Promise> that will be kept when the program exits. The standard
475+
output and standard error of the program are available as L<Supply>
476+
objects from the methods L<stdout|/type/Proc::Async#method_stdout>
477+
and L<stderr|/type/Proc::Async#method_stderr> respectively which can be
478+
tapped as required.
479+
480+
If you want to write to the standard input of the program
481+
you can supply the C<:w> adverb to the constructor and use
482+
the methods L<write|/type/Proc::Async#method_write>,
483+
L<print|/type/Proc::Async#method_print> or
484+
L<say|/type/Proc::Async#method_say> to write to the opened pipe once
485+
the program has been started:
486+
487+
my $proc = Proc::Async.new(:w, 'grep', 'foo');
488+
489+
$proc.stdout.tap(-> $v { print "Output: $v" });
490+
491+
say "Starting...";
492+
my $promise = $proc.start;
493+
494+
$proc.say("this line has foo");
495+
$proc.say("this one doesn't");
496+
497+
$proc.close-stdin;
498+
await $promise;
499+
say "Done.";
500+
501+
# Output:
502+
# Starting...
503+
# Output: this line has foo
504+
# Done.
505+
506+
Some programs (such as C<grep> without a file argument in this
507+
example, ) won't exit until their standard input is closed so
508+
L<close-stdin|/type/Proc::Async#method_close-stdin> can be called when
509+
you are finished writing to allow the L<Promise> returned by C<start>
510+
to be kept.
511+
450512
=head1 Low-level APIs
451513
452514
=head2 Threads

0 commit comments

Comments
 (0)