Hi,
I must confess, that I don't fully understand the
difference between an array and a list
perldoc -q 'list and an array'
Basically, a list is a constant sequence of scalar values. An array is a
Perl datatype which contains a list. The array may be altered. A list
cannot. It is roughly analogous to a string 'Hello' and a variable $foo
which contains 'Hello'. You can say $foo .= ' World'. But you cannot
say 'Hello' .= ' World'.
and suspect
that the cause for my problem lies somewhere there...
Could someone please explain, why does the code below warns
"Useless use of sort in scalar context at ./test_case.pl"?
#!/usr/bin/perl -w
use constant DEFAULT => qw(ARMI ARM4 THUMB WINS WINSCW);
@platforms = (sort keys %prj_platforms) || (DEFAULT);
List vs Array is not really the problem, no. The || operator forces
scalar context on its arguments. An array in scalar context returns the
size of that array. A list used scalar context evaluates each of its
members, from left to right, and discards all but the last value, which it
returns. sort() returns a list. But even if it returned an array, your
code would still not do what you want.
Your problem is not list vs. array, but rather list context vs. array
context. The || operator is forcing scalar context, where you need list
context - so that one of your two lists can be assigned to @platforms.
(@platforms = sort keys %prj_platforms) || (@platforms = DEFAULT);
Now you have an expression on the left which is done in list context -
because of the assignment to @platforms. Once that assignment is
completed, @platforms is evaluated in scalar context, as the argument to
the || operator. Thus, it returns its new size. If that size is 0, the
short-circuit nature of || causes its second argument to be evaluated,
causing DEFAULT to be assigned to @platforms instead.
Hope this helps,
Paul Lalli