Re: What is the pipe symbol doing to my array?
[email protected] (Sean McAfee) Mon, 3 Nov 2025 22:02:35 +0800
| Newsgroups | perl.perl6.users |
|---|---|
| Message-ID | <CANan03Yz7Cma9h_8ZQT481BJu=_T-NktKHmN3KTSWxAgxjOoNw@mail.gmail.com> |
On Mon, Nov 3, 2025 at 1:31 PM ToddAndMargo via perl6-users < [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.