in reply to Using || operator with arrays
As has been explained, the lefthand side of the || operator is evaluated for truth, which inherently means it was evaluated in scalar context, and then the context of the far left trickles to the right, so it is possibly evaluated in scalar context. That makes things a little funky, as you've discovered.
But I felt this discussion wouldn't be complete with another alternative that does work.
print +(@array) ? @array : @array1;
This evaluates @array for truth (meaning it exists with at least one element). If @array evaluates to truth, it as a list is passed through the ternary oprator. If @array evaluates to false (ie, it has zero elements or is otherwise undefined), then @array1 passes through the ternary operator. In both cases, the list context presented via 'print' trickles down correctly.
Dave
|
|---|