Re: sub default question
[email protected] (ToddAndMargo via perl6-users) Wed, 19 Nov 2025 23:15:37 -0800
| Newsgroups | perl.perl6.users |
|---|---|
| Message-ID | <[email protected]> |
On 11/19/25 2:33 AM, Peter Pentchev wrote:
> On Wed, Nov 19, 2025 at 02:19:43AM -0800, ToddAndMargo via perl6-users wrote:
>> On 11/19/25 1:38 AM, Peter Pentchev wrote:
>>> On Wed, Nov 19, 2025 at 01:13:23AM -0800, ToddAndMargo via perl6-users wrote:
>>>> Hi All,
>>>>
>>>> https://github.com/rakudo/rakudo/blob/main/src/core.c/Proc.rakumod
>>>> 232: multi sub run(*@args where .so, :$in = '-', :$out = '-', :$err = '-',
>>>>
>>>> Here is my confusion. The above sets
>>>> :$in = '-', :$out = '-', :$err = '-',
>>>> to "-" as the default.
>>>>
>>>> But if you leave
>>>> :$out and :$err
>>>> off the run line, STDOUT and STDERR do not
>>>> get captured and are sent to the console.
>>>
>>> ...that's exactly what "-" means.
>>>
>>> G'luck,
>>> Peter
>>>
>>
>>
>> If :out and "err are left off the run line, they are
>> not used and the STDOUT and STDERR go to the console.
>
>>
>> If :out and :err are included, the STDOUT and STDERR
>> are captured and your Proc variable are populated
>> with their file handles.
>>
>> So why leaving them out, does not the default shown
>> on line 232 apply?
>
> If :out and :err are left off the run line, then Raku invokes
> the run function just as if you specified the default values,
> which in this case are "-" for both. The part you refer to
> in your other message about `:out` being equivalent to
> `:out(True)` does not apply in this case, since you do NOT
> specify `:out` in the run invocation, you leave `:out` out
> completely, so Raku uses the default value, which is "-".
>
> Some more clarification may be in order here. As a Unix convention,
> tools such as `cat`, `sort`, `wc` that process text files may
> accept the special value "-" (a single minus sign) as an argument
> that means "now read from the standard input stream and process that".
> This allows you to do stuff like:
>
> #!/bin/sh
>
> cat /usr/share/wrap/prologue.txt - /usr/share/wrap/epilogue.txt
>
> ...and when you invoke that script, it will "surround" whatever you pass
> on its standard input stream with the contents of those two files.
> It also allows you to do funny stuff like:
>
> echo 'roam:x:1000:1000:/home/roam:My account' | sort -k 3,3n -k 4,4n /etc/passwd -
>
> ...to produce something like a passwd file that contains an additional line.
>
> Similarly, "-" used where the path to an output file should be specified
> also traditionally means "just use the standard output stream, do not
> write to a file":
>
> tar cf - myprog-0.1.0/ | gzip -n9 > myprog-0.1.0.tar.gz
>
> ...will make `tar` write the archive contents to its standard output stream,
> which is then piped to `gzip`.
>
> This is why the special value "-" (a string that consists of a single
> "ASCII minus" character) may be passed to run for the :in, :out, and :err
> parameters. If :in is "-", the new process will read from your process's standard
> input stream. If :out is "-", the new process will write stuff to your process's
> standard output stream. If :err is "-", the new process will report errors to
> your process's standard error stream. If none of these have been redirected,
> then yes, everything will go "to the console", as you said.
>
> And "-" is also the default for those parameters, so if you do:
>
> run("date")
>
> ...then the `date` program will output stuff on the console, or rather, on
> your own process's standard output stream - that's because Raku sees that
> you did not specify a value for :out, so it uses the default value "-", and
> run treats "-" as "go to the standard output stream".
>
Hi Peter,
I understand now. Thank you!
I thought
:out("-")
meant to capture STDOUT.
:out(True)
is the one that captures STDOUT.
Boy did I get that mixed up!
-T
p.s. You are a talented technical writer.