Skip to content

Commit 56086e0

Browse files
committed
Expands filehandle explanation closes #2111
1 parent e9a0579 commit 56086e0

File tree

2 files changed

+47
-25
lines changed

2 files changed

+47
-25
lines changed

doc/Type/IO.pod6

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,9 @@ spurt 'file-that-already-exists', 'new text', :createonly;
352352
353353
sub run(*@args ($, *@) --> Proc)
354354
355-
Runs an external command without involving a shell and returns a L<Proc> object.
355+
Runs an external command I<without involving a shell> and returns a
356+
L<Proc> object. By default, the external command will print to standard
357+
output and error, and read from standard input.
356358
357359
run 'touch', '>foo.txt'; # Create a file named >foo.txt
358360
@@ -376,19 +378,33 @@ Note that C<--> is required for many programs to disambiguate between
376378
command-line arguments and
377379
L<filenames that begin with hyphens|http://mywiki.wooledge.org/BashPitfalls#Filenames_with_leading_dashes>.
378380
379-
A sunk L<Proc> object for a process that L<exited|/routine/exitcode> unsuccessfully
380-
will throw. If you wish to ignore such failures, simply use L<run> in non-sink context:
381+
A sunk L<Proc> object for a process that L<exited|/routine/exitcode>
382+
unsuccessfully will throw. If you wish to ignore such failures, simply
383+
use L<run> in non-sink context:
381384
382385
run 'false'; # SUNK! Will throw
383386
run('false').so; # OK. Evaluates Proc in Bool context; no sinking
384387
385-
To capture output or error you can use the C<:out> or C<:err> arguments respectively:
388+
If you want to capture standard output or error instead of having it
389+
printed directly you can use the C<:out> or C<:err> arguments
390+
respectively, which will make them available using the
391+
L<C<Proc.out>|/type/Proc> method:
386392
387393
my $proc = run 'echo', 'Perl 6 is Great!', :out, :err;
388394
$proc.out.slurp(:close).say; # OUTPUT: «Perl 6 is Great!␤»
389395
$proc.err.slurp(:close).say; # OUTPUT: «␤»
390396
391-
See L<Proc|/type/Proc> and L<Proc::Async|/type/Proc::Async> for more details.
397+
You can use these arguments to redirect them to a filehandle, thus
398+
creating a kind of I<pipe>:
399+
400+
my $ls-alt-handle = open :w, '/tmp/cur-dir-ls-alt.txt';
401+
my $proc = run "ls", "-alt", :out($ls-alt-handle);
402+
# (The file will contain the output of the ls -alt command)
403+
404+
405+
These argument are quite flexible and admit, for instance, handles to
406+
redirect them. See L<Proc|/type/Proc> and
407+
L<Proc::Async|/type/Proc::Async> for more details.
392408
393409
=head2 sub shell
394410

doc/Type/Proc/Async.pod6

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
class Proc::Async {}
99
=end code
1010
11-
B<Note:> only the MoarVM backend of Rakudo implements C<Proc::Async> at the
12-
moment.
11+
B<Note:> only the MoarVM backend of Rakudo implements C<Proc::Async> at
12+
the moment.
1313
14-
C<Proc::Async> allows you to run external commands asynchronously, capturing
15-
standard output and error handles, and optionally write to its standard input.
14+
C<Proc::Async> allows you to run external commands asynchronously,
15+
capturing standard output and error handles, and optionally write to its
16+
standard input.
1617
1718
=begin code
1819
my $file = ‘foo’.IO;
@@ -127,18 +128,20 @@ An example of piping several commands like C<echo "Hello, world" | cat -n>:
127128
multi method new(*@ ($path, *@args), :$w, :$enc, :$translate-nl --> Proc::Async:D)
128129
multi method new( :$path, :@args, :$w, :$enc, :$translate-nl --> Proc::Async:D)
129130
130-
Creates a new C<Proc::Async> object with external program name or path C<$path>
131-
and the command line arguments C<@args>.
131+
Creates a new C<Proc::Async> object with external program name or path
132+
C<$path> and the command line arguments C<@args>.
132133
133134
If C<:w> is passed to C<new>, then a pipe to the external program's standard
134135
input stream (stdin) is opened, to which you can write with C<write> and
135136
C<say>.
136137
137-
The C<:enc> specifies L<the encoding|/type/IO::Handle#method_encoding> for streams
138-
(can still be overridden in individual methods) and defaults to C<utf8>.
138+
The C<:enc> specifies L<the encoding|/type/IO::Handle#method_encoding>
139+
for streams (can still be overridden in individual methods) and defaults
140+
to C<utf8>.
139141
140-
If C<:translate-nl> is set to C<True> (default value), OS-specific newline
141-
terminators (e.g. C<\r\n> on Windows) will be automatically translated to C<\n>.
142+
If C<:translate-nl> is set to C<True> (default value), OS-specific
143+
newline terminators (e.g. C<\r\n> on Windows) will be automatically
144+
translated to C<\n>.
142145
143146
=head2 method stdout
144147
@@ -186,8 +189,9 @@ along as L<Str|/type/Str>.
186189
await $promise;
187190
188191
You must call C<stderr> before you call L<#method start>. Otherwise an
189-
exception of class L<X::Proc::Async::TapBeforeSpawn|/type/X::Proc::Async::TapBeforeSpawn> is
190-
thrown.
192+
exception of class
193+
L<X::Proc::Async::TapBeforeSpawn|/type/X::Proc::Async::TapBeforeSpawn>
194+
is thrown.
191195
192196
If C<stderr> is not called, the external program's standard error stream is not
193197
captured at all.
@@ -203,8 +207,8 @@ Use L«C<.Supply>|/type/Proc::Async#method_Supply» for merged STDOUT and STDERR
203207
multi method bind-stdin(IO::Handle:D $handle)
204208
multi method bind-stdin(Proc::Async::Pipe:D $pipe)
205209
206-
Sets a handle (which must be opened) or a C<Pipe> as a source of C<STDIN>. The C<STDIN> of
207-
the target process must be writable or
210+
Sets a handle (which must be opened) or a C<Pipe> as a source of
211+
C<STDIN>. The C<STDIN> of the target process must be writable or
208212
C<X::Proc::Async::BindOrUse> will be thrown.
209213
210214
my $p = Proc::Async.new("cat", :in);
@@ -223,16 +227,18 @@ and will print the content of C</etc/profile> to standard output.
223227
224228
method bind-stdout(IO::Handle:D $handle)
225229
226-
Redirects STDOUT of the target process to a handle (which must be opened).
227-
If STDOUT is closed L<X::Proc::Async::BindOrUse|/type/x/proc/async/BindOrUse>
228-
will be thrown.
230+
Redirects STDOUT of the target process to a handle (which must be
231+
opened). If STDOUT is closed
232+
L<X::Proc::Async::BindOrUse|/type/x/proc/async/BindOrUse> will be
233+
thrown.
229234
230235
my $p = Proc::Async.new("ls", :out);
231236
my $h = "ls.out".IO.open(:w);
232237
$p.bind-stdout($h);
233238
$p.start;
234239
235-
This program will pipe the output of the C<ls> shell command to a file called C<ls.out>, which we are opened for reading.
240+
This program will pipe the output of the C<ls> shell command to a file
241+
called C<ls.out>, which we are opened for reading.
236242
237243
=head2 method bind-stderr
238244
@@ -416,8 +422,8 @@ The C<Proc::Async> object must be created for writing (with
416422
C<Proc::Async.new(:w, $path, @args)>). Otherwise an
417423
L<X::Proc::Async::OpenForWriting> exception will the thrown.
418424
419-
C<start> must have been called before calling method close-stdin, otherwise an
420-
L<X::Proc::Async::MustBeStarted> exception is thrown.
425+
C<start> must have been called before calling method close-stdin,
426+
otherwise an L<X::Proc::Async::MustBeStarted> exception is thrown.
421427
422428
=head2 method kill
423429

0 commit comments

Comments
 (0)