Difference between () and [] in Perl 6

For those who may be confused by this answer, the question is about Perl 6, and none of this applies to Perl 5.

The statement

my @s = [2443, 5, 33, 90, -9, 2, 764]

creates an itemised array and assigns it to @s[0], so @s has only a single element and sorting it is pointless.

However you can say

@s[0].sort.say

which has the effect you expected


I'm going to go out on a limb and refer to some of CPAN's Perl6 documentation where this could be viewed as a list vs. array thing - i.e. a sequence of values versus a sequence of itemized values (see doc.perl6.org).

Certainly perl6 is different enough that it warrants its own tag but it is still perl so it's not surprising that () creates a list and [] creates an anonymous array.

> say [2443, 5, 33, 90, -9, 2, 764].WHAT
(Array)
> say (2443, 5, 33, 90, -9, 2, 764).WHAT
(List)

Since this question was first asked and answered the behavior has changed:

> my @s = [2443, 5, 33, 90, -9, 2, 764]
> @s.sort.say
(-9 2 5 33 90 764 2443)

Note that the output when sorted is a List but otherwise @s is an Array:

> @s.sort.WHAT.say
(List)
> @s.WHAT.say
(Array)

Tags:

Raku