Re: What is the pipe symbol doing to my array?

[email protected] (ToddAndMargo via perl6-users) Mon, 3 Nov 2025 10:07:05 -0800
Newsgroups perl.perl6.users
Message-ID <[email protected]>
On 11/3/25 6:02 AM, Sean McAfee wrote:
> On Mon, Nov 3, 2025 at 1:31 PM ToddAndMargo via perl6-users <perl6- 
> [email protected] <mailto:[email protected]>> wrote:
> 
>     I guess I am asking what the flattening does.
> 
> 
> It passes the elements of an array as separate parameters to a function, 
> rather than passing the array itself as a single parameter.
> 
> my @nums = 3, 4, 5;
> sub show($x, $y, $z) { say "Got $x, $y, and $z" }
> show(1, 2, @nums);  # Got 1, 2, and 3 4 5
> show(|@nums); # Got 3, 4, and 5
> 
> But a function that takes a slurpy array parameter flattens its 
> arguments automatically, making it pointless to explicitly flatten with 
> a pipe:
> 
> sub show2(*@args) { say "Got @args.join(', ')" }
> show2(1, 2, @nums); # Got 1, 2, 3, 4, 5
> show2(@nums); # Got 3, 4, 5
> show2(|@nums); # Got 3, 4, 5
> 
> This is why in my other recent response to you, I was able to say this:
> 
> my $p = run <ls -la>, dir(test => *.ends-with('.raku'));
> 
> And I didn't need to say this, even though it also works:
> 
> my $p = run |<ls -la>, |dir(test => *.ends-with('.raku'));
> 
> It's because run takes a slurpy list of arguments.
> 
>     Why is this proper
>           my $proc = run(@x, :err, :out)
> 
>     and this is not
>           my $proc = run(@x, :err, :out)
> 
> 
> Well, I mean...those are identical.  But assuming you meant to write |@x 
> in the second one, it's not "proper" for the same reason this is improper:
> 
> my $y = (+$x) ** (+2) - (+5) * (+$x) + (+1);
> 
> ...compared to:
> 
> my $y = $x ** 2 - 5 * $x + 1;
> 
> While functionally equivalent, the first is riddled with pointless 
> complications that will leave a human reader scratching their head.
> 


Hi Sean,

Thank you!

My big worry with flattening is when a parameter passed to run has a 
space in it.

     run "a", "b c", "d", ...

that flattening will send it as

     run "a", "b", "c", "d", ...

-T