سؤال

Why does dereferencing a hash reference into hash & assigning it to a scalar gives an irrelevant value (at least to me)?

Code:

my $hash = {
        1 => 9,
        2 => 10,
        3 => 11,
        4 => 12,
        5 => 13,
        6 => 14,
        7 => 15,
        8 => 16,
       };

my $dereferenced = %$hash;

print $dereferenced;

Perl version : 5.12.4

OS : Windows 7

The value printed is

7/16

Whether it has something to do with Perl internals?

هل كانت مفيدة؟

المحلول

Scalars cannot contain hashes or arrays, only references to them.

But even if they could, when you use a hash on the right side of a scalar assignment, it is in scalar context, and a hash in scalar context produces false if the hash is empty, or a string describing the bucket usage within the hash if not (e.g. "7/32").

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top