Nevertheless undef @a is allowed, adding even more confusion:
I don't see any confusion nor potential for it.
If I have a scalar and at some point in my code I wish to check if it currently has a value, and if it does not, set it, I might write:
my $x; ... $x = getValue() unless defined $x;
More recently, I'd write that as: $x //= getValue();. Clear concise semantics.
If have an array, and at some point in my code I want to check if it contains anything, and populate it if it does not, I might write:
my @a; ... @a = getValues() unless @a;
Thus using the value of the array in a scalar context to decide if the array is empty or not.
It seems a natural, semantically clear, easily implemented extension of the scalar case above to write that as:
@a //= getValues();
Test the scalar context value of the array, and if it is false, assign the list on the right hand side to the array on the left.
Indeed, it seemed so logically analogous, and semantically clear, that I wrote it and expected it to work, and I was taken by considerable surprise that it didn't.
Hence my OP.
In reply to Re^4: Conditional array initialisation? (Question expanded)
by BrowserUk
in thread Conditional array initialisation?
by BrowserUk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |