Why are links interlaced inside Perl lists?

Placing a precompiled regular expression inside two different hashes listed:

my @list = ();

my $regex = qr/ABC/;

push @list, { 'one' => $regex };
push @list, { 'two' => $regex };

use Data::Dumper;
print Dumper(\@list);

I would expect:

$VAR1 = [
      {
        'one' => qr/(?-xism:ABC)/
      },
      {
        'two' => qr/(?-xism:ABC)/
      }
    ];

But instead, we get a circular link:

$VAR1 = [
      {
        'one' => qr/(?-xism:ABC)/
      },
      {
        'two' => $VAR1->[0]{'one'}
      }
    ];

This will happen with undefined nested hash links and shallow copied ones $regex.

My guess is that the main reason is that precompiled regular expressions are actually links, and links within the same list structure are compacted as optimizations (\ $ scalar behaves the same way). I do not quite understand the usefulness of this (presumably the link to the link has the same amount of memory), but maybe there is a reason based on the internal representation

? ? , , , GC , . , , , MongoDB segfault (. https://rt.cpan.org/Public/Bug/Display.html?id=58500)

+5
3

.

; , . Data:: Dumper Perl- , , $list[0]->{one} $list[1]->{two} .

Perl , , , .

+9

.

  • .
  • Data:: Dumper .
  • :: , , , , Perl, , $list[0]{one}, $list[0]{two}.
  • , .
  • .
+6

, , (\ $scalar ). (, ), , , ,

, , - (). , . , , - , , , .

, Data:: Dumper , , , .

For such a scalar, this is probably not required for this, but it probably happens because Data :: Dumper checks to see if it has seen the link before checking the type. It also gives an advantage, which shows that it is a reference to the same data, rather than a copy of it, which is possibly useful information that would be lost if it simply printed the value.

+1
source

All Articles