Arrays in Perl 6

Yeah, and the fact that the author keeps repeating this mistake over and over is pretty disheartening.

Not to shit on author's efforts to popularize Perl 6, but here's some of the errata for the article:

We can use the == operator to check if two arrays have the same content

That checks the number of elements. Use eqv to check for equality, but note that lazy arrays cannot be compared.

a variable that starts with a $ sign that for now I'll call generic variable

Never heard of that term used with the language.

the generic variable (starting with $) sees the data as a single item, while the array variable (staring with @) sees the values as separate elements.

It don't see nothing. You have a containerized array in one case and one that isn't. You can easily not opt-in for the free Scalar container and get perfectly iterable Array in a $-sigiled variable:

my $array := [1, 2, 3];
say "[$_]" for $array;  #OUTPUT: «[1]␤[2]␤[3]␤»

The other way is to use the | flattening operator.

It's called a slip. And it flattens the Iterable to the outer container, but it doesn't flatten its arguments as its name in the article suggests.

we cannot have a space between the @ and the $ in @$s.

Sure you can. Just use parens. And the splat is called a coercer not a prefix.

my $x = [<a b c>];
say @(  $x  ) #OUTPUT: «[a b c]␤»

"How to get the array (@) back from the generic ($) ?"

This entire section is made up guesswork. It was talking about "dereferencing" when I read it in the morning, but it seems the author corrected that part now.

So we need to turn the generic variable into an array in some way

We just need to decontainerize ("decont") a containerized Array.

We could also use the | flattening operator. That will also create a copy of the content. Just like @$s did.

Neither creates a copy. The @ calls .cache, which for Arrays ends up returning self. The slip ends up calling .Slip which doesn't copy. It's assignment to a @var that would clone the original list.

But as mentioned above, the goal is to simply decont the Array, which can be done with empty angle brackets: $foo<>. While it doesn't matter much for Lists, this is crucial for Seqs, since $foo<> decont will not cache it, while @$s will, so this will quickly consume all of your RAM...

my $lines = '500TB-file.txt'.IO.lines;
.say for @$lines;

....while this won't:

my $lines = '500TB-file.txt'.IO.lines;
.say for $lines<>;

Trying to use :=, the binding operator here just confuses me.

That's because the entire section assumes some "generic variables" and "array variables" and "conversion" and "dereferencing" between them when we're just dealing with containers.

To Perl 6 beginners reading this, I'd suggest to forget about all this masturbatory prose about binding. It's not that important. Learn this difference between List objects, Array objects (mutable List), Slip objects (List that flattens into outer Iterable), and Seq objects (non-Positional objects that can be iterated just once, unless you cache them_method_cache) which in many cases happens automatically). Then read about Containers and what Reification is and then you'll be all set.

The @ vs $ isn't the important bit. To drive the point home... Look, ma! No @ sigils!

my \array               = [1, 2, 3];
my $array             := [4, 5, 6];
my %array is Array = [7, 8, 9];
say "[$_]" for array;
say "[$_]" for $array;
say "[$_]" for %array;
# OUTPUT: «[4]␤[5]␤[6]␤[1]␤[2]␤[3]␤[7]␤[8]␤[9]␤»
/r/perl Thread Parent Link - perl6maven.com