icanhezperl has asked for the wisdom of the Perl Monks concerning the following question:
I'm seeing an odd behavior wehere the "Can't use an undefined value as an ARRAY reference at ..." is generated when expected but not when map() and grep() are involved.
Here's the code. In the first case with map(), perl runs without an error (ditto for grep). But in the second case, without any map or grep, the expected error is thrown becaus the value being dereferenced is undefined.
use strict; use warnings; my $this_key = "NOT_IN_HASH"; my $hash_ref = {}; my @items; if (0) { # this gives no error @items = map { $_->{value} } @{$hash_ref->{$this_key}}; } else { # but this generates to the expected error. @items = @{$hash_ref->{$this_key}}; }
Am I missing something obvious? Is this behavior intentional?
Thank you.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: map() and grep() suppress "Can't use an undefined value as an ARRAY reference at ..." errors
by stevieb (Canon) on Mar 15, 2018 at 20:47 UTC | |
|
Re: map() and grep() suppress "Can't use an undefined value as an ARRAY reference at ..." errors
by haukex (Archbishop) on Mar 15, 2018 at 20:48 UTC | |
by stevieb (Canon) on Mar 15, 2018 at 21:12 UTC | |
|
Re: map() and grep() suppress "Can't use an undefined value as an ARRAY reference at ..." errors
by Marshall (Canon) on Mar 15, 2018 at 21:16 UTC | |
|
Re: map() and grep() suppress "Can't use an undefined value as an ARRAY reference at ..." errors
by icanhezperl (Novice) on Mar 15, 2018 at 21:06 UTC |