When you do
print sort @letters . "\n";
what you are really doing is evaluating @letters in a scalar context and adding a new row and then sorting this array. Since the array is 5 in length, you get number 5.
Try the following:
my @letters = ('b', 'c', 'a', 'e', 'd'); print sort(@letters . "\ntest");
And you will output:
5 test
sort undefined behavior in scalar context. Thus, you cannot :
my @letters = ('b', 'c', 'a', 'e', 'd'); print sort(@letters) . "\n";
What you probably want to do is something like this:
my @letters = ('b', 'c', 'a', 'e', 'd'); print join(",", sort(@letters)) . "\n";
Output:
a,b,c,d,e
In the first scenario, you add \n to the list of letters, and then sort it. So it ends at the beginning. Here is an example
my @letters = ('b', 'c', 'a', 'e', 'd'); print sort @letters , "\n", 1, 2, 3;
Outputs:
In general, it is useful to use parentheses to understand what behavior you want to get.
my @letters = ('b', 'c', 'a', 'e', 'd'); print sort(@letters), "\ntest";
Outputs:
abcde test