2

For instance:

use strict;
use warnings;
use Devel::Peek;

Dump({});

Would print the following:

SV = IV(0x170fc98) at 0x170fca8
  REFCNT = 1
  FLAGS = (TEMP,ROK)
  RV = 0x170fa68
  SV = PVHV(0x1715970) at 0x170fa68
    REFCNT = 1
    FLAGS = (SHAREKEYS)
    ARRAY = 0x0
    KEYS = 0
    FILL = 0
    MAX = 7
    RITER = -1
    EITER = 0x0

it seems like SV = IV(0x170fc98) at 0x170fca8 (a numerical, IV, scalar) references the PVHV (hash) SV = PVHV(0x1715970) at 0x170fa68

I was expecting something like:

  SV = PVHV(0x1715970) at 0x170fa68
    REFCNT = 1
    FLAGS = (SHAREKEYS)
    ARRAY = 0x0
    KEYS = 0
    FILL = 0
    MAX = 7
    RITER = -1
    EITER = 0x0
nixer
  • 115
  • 5

2 Answers2

3

Yes, references are always scalars.

The construct { ... } creates an empty hash, then returns a reference to it, which is a scalar.

It's similar to do { my %anon = ( ... ); \%anon }.

ikegami
  • 367,544
  • 15
  • 269
  • 518
tobyink
  • 13,478
  • 1
  • 23
  • 35
3

Each data type in Perl has a common “header” of data that supplies reference counting, etc. The flags in this header determine what type it actually is (e.g. a PVHV). So internally a %hash is a PVHV. The header is followed by other hash-specific fields.

Now what is a $reference = \%hash? A reference is a scalar containing a pointer to another data type. This is implemented as the integer field in an IV, but the ROK flag is set to show that the data isn't an integer but a pointer.

You can only pass scalars to a Perl subroutine, therefore you can only pass a hash reference, not a hash itself to Dump. As Dump prints out whatever scalar you provide, the enclosing reference is also shown.

To understand internal data types in Perl better, I recommend the Illustrated Perl Guts.

amon
  • 57,091
  • 2
  • 89
  • 149
  • $reference = \%hash which assign the ref of %hash to the $reference scalar seems ok to me to have the IV header, but my example which is using only {}, I was actually expecting to see only PVHV. – nixer Jan 01 '14 at 20:53
  • @naabu That's because `{...}` doesn't create a hash, it constructs a hash reference. It was designed that way because of back-compatibility: collections like `@arrays` or `%hashes` can only contain scalar values, and having `{}` create a reference allows us to write nested literals: `[{foo => 2}, 3, [1, 2, {3 => ["a", "b"]}]]` (`[...]` creates an array reference). – amon Jan 01 '14 at 21:12
  • 1
    things were maybe a bit clearer when there were separate IV and RV (reference value) types, but when things were reorganized (for speed and less memory, in 5.10?) they were united. – ysth Jan 01 '14 at 21:47
  • @naabu, The above comment should read: That's because `{...}` doesn't **just** create a hash, it **also** constructs a reference to it. – ikegami Jan 02 '14 at 05:36