読者です 読者をやめる 読者になる 読者になる

Perl deleteの挙動を確認

Perlのdelete関数の挙動を確認してみた。
想定通りですた。

#!/usr/bin/Perl

use strict;
use warnings;
use utf8;

use Data::Dumper;

my $test = +{
    a => 1,
    b => 'b',
    c => +{
        d => 2,
        e => 'e',
        f => +{
            g => 3,
            h => 'h',
        },
    },
};

my $c = $test->{c};
my $f = $test->{c}->{f};

print '# $test hash ############' . "\n";
print Dumper $test;
print "\n";


print '# $c hash ###############' . "\n";
print Dumper $c;
print "\n";


print '# $f hash ###############' . "\n";
print Dumper $f;
print "\n";

print '# delete $test->{c}->{f}->{h}; #' . "\n";
delete $test->{c}->{f}->{h};
print "\n";

print '# $test hash ############' . "\n";
print Dumper $test;
print "\n";

print '# $c hash ###############' . "\n";
print Dumper $c;
print "\n";

print '# $f hash ###############' . "\n";
print Dumper $f;
print "\n";

print '# print Dumper $test->{c}->{f}->{h}; #' . "\n";
print Dumper $test->{c}->{f}->{h};
print "\n";

print '# delete $test->{c}; #' . "\n";
delete $test->{c};
print "\n";

print '# $test hash ############' . "\n";
print Dumper $test;
print "\n";

print '# $c hash ###############' . "\n";
print Dumper $c;
print "\n";

print '# $f hash ###############' . "\n";
print Dumper $f;
print "\n";

exit;
# $test hash ############
$VAR1 = {
          'c' => {
                   'e' => 'e',
                   'd' => 2,
                   'f' => {
                            'h' => 'h',
                            'g' => 3
                          }
                 },
          'a' => 1,
          'b' => 'b'
        };

# $c hash ###############
$VAR1 = {
          'e' => 'e',
          'd' => 2,
          'f' => {
                   'h' => 'h',
                   'g' => 3
                 }
        };

# $f hash ###############
$VAR1 = {
          'h' => 'h',
          'g' => 3
        };

# delete $test->{c}->{f}->{h}; #

# $test hash ############
$VAR1 = {
          'c' => {
                   'e' => 'e',
                   'd' => 2,
                   'f' => {
                            'g' => 3
                          }
                 },
          'a' => 1,
          'b' => 'b'
        };

# $c hash ###############
$VAR1 = {
          'e' => 'e',
          'd' => 2,
          'f' => {
                   'g' => 3
                 }
        };

# $f hash ###############
$VAR1 = {
          'g' => 3
        };

# print Dumper $test->{c}->{f}->{h}; #
$VAR1 = undef;

# delete $test->{c}; #

# $test hash ############
$VAR1 = {
          'a' => 1,
          'b' => 'b'
        };

# $c hash ###############
$VAR1 = {
          'e' => 'e',
          'd' => 2,
          'f' => {
                   'g' => 3
                 }
        };

# $f hash ###############
$VAR1 = {
          'g' => 3
        };

参照先は残ってる。
perlのガベージコレクションは参照カウントだから、
参照はずれたオブジェクトはなんかのタイミングで消されるのでしょう。