I'm doing some work in Perl and I ran across an odd result using the conditional operator.
The code in question:
($foo eq "blah") ? @x = @somearray : @y = ("another","array");
Trying to compile this code results in the error "Assignment to both a list and a scalar at XXX line YY, near ');'
". In trying to pinpoint the source of the error I've written this using a couple different ways of representing an array in Perl and they all return with the same error. Now at first I thought it was just some dumb obvious mistake with the assignment statements, but just to satisfy my curiosity I rewrote the statement in a more verbose way:
if($foo eq "blah") {
@x = @somearray;
} else {
@y = ("another","array");
}
That version of the code compiled perfectly fine.
Is there some fine distinction between how the conditional operator works and a basic if-else statement works that I'm missing here? I always understood the conditional operator to be just a short-hand version of the second statement. If there isn't a functional difference between the two, why would Perl object to the first statement, but not the second?