@@ -365,6 +365,80 @@ spurt 'file-that-already-exists', 'new text', :createonly;
365
365
# OUTPUT: «Failed to open file /home/camelia/file-that-already-exists: file already exists …»
366
366
= end code
367
367
368
+ = head2 sub run
369
+
370
+ Defined as:
371
+
372
+ = for code :method
373
+ sub run(
374
+ *@args ($, *@),
375
+ :$in = '-',
376
+ :$out = '-',
377
+ :$err = '-',
378
+ Bool :$bin = False,
379
+ Bool :$chomp = True,
380
+ Bool :$merge = False,
381
+ Str:D :$enc = 'UTF-8',
382
+ Str:D :$nl = "\n",
383
+ :$cwd = $*CWD,
384
+ Hash() :$env = %*ENV
385
+ --> Proc:D)
386
+
387
+ Runs an external command I < without involving a shell > and returns a
388
+ L < Proc|/type/Proc > object. By default, the external command will print to
389
+ standard output and error, and read from standard input.
390
+
391
+ run 'touch', '>foo.txt'; # Create a file named >foo.txt
392
+
393
+ run <<rm >foo.txt>>; # Another way to use run, using word quoting for the
394
+ # arguments
395
+
396
+ If you want to pass some variables you can still use C « < > » , but try
397
+ to avoid using C < « » > as it will do word splitting if you forget to
398
+ quote variables:
399
+
400
+ my $file = ‘--my arbitrary filename’;
401
+ run ‘touch’, ‘--’, $file; # RIGHT
402
+ run <touch -->, $file; # RIGHT
403
+
404
+ run «touch -- "$file"»; # RIGHT but WRONG if you forget quotes
405
+ run «touch -- $file»; # WRONG; touches ‘--my’, ‘arbitrary’ and ‘filename’
406
+ run ‘touch’, $file; # WRONG; error from `touch`
407
+ run «touch "$file"»; # WRONG; error from `touch`
408
+
409
+ Note that C < -- > is required for many programs to disambiguate between
410
+ command-line arguments and
411
+ L < filenames that begin with hyphens|https://mywiki.wooledge.org/BashPitfalls#Filenames_with_leading_dashes > .
412
+
413
+ A sunk L < Proc|/type/Proc > object for a process that L < exited|/routine/exitcode >
414
+ unsuccessfully will throw. If you wish to ignore such failures, simply use
415
+ L < run|/routine/run > in non-sink context:
416
+
417
+ run 'false'; # SUNK! Will throw
418
+ run('false').so; # OK. Evaluates Proc in Bool context; no sinking
419
+
420
+ If you want to capture standard output or error instead of having it
421
+ printed directly you can use the C < :out > or C < :err > arguments
422
+ respectively, which will make them available using the
423
+ L < C < Proc.out > |/type/Proc> method:
424
+
425
+ my $proc = run 'echo', 'Perl 6 is Great!', :out, :err;
426
+ $proc.out.slurp(:close).say; # OUTPUT: «Perl 6 is Great!»
427
+ $proc.err.slurp(:close).say; # OUTPUT: «»
428
+
429
+ You can use these arguments to redirect them to a filehandle, thus
430
+ creating a kind of I < pipe > :
431
+
432
+ my $ls-alt-handle = open :w, '/tmp/cur-dir-ls-alt.txt';
433
+ my $proc = run "ls", "-alt", :out($ls-alt-handle);
434
+ # (The file will contain the output of the ls -alt command)
435
+
436
+ These argument are quite flexible and admit, for instance, handles to
437
+ redirect them. See L < Proc|/type/Proc > and
438
+ L < Proc::Async|/type/Proc::Async > for more details.
439
+
440
+ See also L < C < new > |/type/Proc#method_new> for more examples.
441
+
368
442
= head2 sub shell
369
443
370
444
sub shell($cmd --> Proc)
0 commit comments