Missing error for undefined value as a hash link

I have two pieces of code that, it would seem, should give the same result, but the latter leads to an error.

1

my $href = undef; my @values = values %{ $href }; # OK - @values is empty 

2:

 my $href = undef; my %hash = %{ $href }; # <-- Error here my @values = values %hash; # ERROR: Can't use an undefined value as a HASH reference 

Why does having values on one line let it work? I would prefer that both of them give an error, since using the undefined value as a hash link is clearly an error. I do not have more recent perl versions for testing, but this was reproduced in 5.8.8 and 5.10.1.

+6
source share
1 answer

Values ​​is a function, and it takes a hash, array, or general expression as an argument (as indicated here ). If you pass undef as an argument to "values", it simply detects it as empty and does not return a value. On the other hand, if you try to explicitly convert undef to a hash, it will not work because it lacks a key-value pair.

EDIT : just to be exact. This error occurs with a "strict" directive (which you should always use, one way or another).

-1
source

Source: https://habr.com/ru/post/978052/


All Articles