in reply to Re^3: Conditional array initialisation?
in thread Conditional array initialisation?
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Conditional array initialisation? (scalar, !defined)
by tye (Sage) on Jul 13, 2013 at 02:16 UTC | |
by Athanasius (Archbishop) on Jul 13, 2013 at 03:49 UTC | |
by tye (Sage) on Jul 13, 2013 at 05:12 UTC | |
by BrowserUk (Patriarch) on Jul 13, 2013 at 03:28 UTC | |
by tye (Sage) on Jul 13, 2013 at 05:23 UTC |