in reply to Re^2: Array dereference in foreach()
in thread Array dereference in foreach()
I think that the OPs problem is that an array-type variable, @a, is being assigned an array reference, $href.
Sorry, but that's not the right way to look at it. @a is not being assigned a reference, instead, the @{...} dereferences the array reference, and it can be used in place of any normal array (perlref), so in effect my @x = @{...} is like assigning an array to an array.
In your third one-liner example, $a plausibly could be undefined.
I'm not sure what you mean, $a is initially undefined in every example. Autovivification is what is causing both $a to go from being uninitialized to holding a hashref (because it's being dereferenced as such with ->{...}) and $a->{list} to come into existence as a hash entry with a value of [] (empty array ref). The third example does not work because in my @x = @{$a->{list}};, the @{$a->{list}} is not in lvalue context. Personally, I think in this case the autovivification behavior is fairly consistent with DWIM:
Edit: Just moved a sentence so the order makes more sense.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Array dereference in foreach()
by pme (Monsignor) on Nov 16, 2017 at 09:54 UTC | |
by choroba (Cardinal) on Nov 16, 2017 at 10:20 UTC | |
by pme (Monsignor) on Nov 16, 2017 at 12:17 UTC | |
by choroba (Cardinal) on Nov 16, 2017 at 12:23 UTC | |
by pme (Monsignor) on Nov 16, 2017 at 12:55 UTC | |
| |
by haukex (Archbishop) on Nov 16, 2017 at 12:43 UTC | |
by 1nickt (Canon) on Nov 16, 2017 at 12:19 UTC |