C
Clint Olsen
Hi:
I have a script included in the post that behaves strangely. Originally I
had a plain hash that had a few scalars inside of it that were tied.
However, for reasons I won't bore you with here, I needed to also tie the
containing hash. However, once I did this, I broke the tied scalars.
After I tie the scalar, it nearly gets immediately culled by the Perl
garbage collector (DESTROY is called), and I'm a bit puzzled why it's
firing. I'm sure some of you Perl blackbelts have seen something like this
before and can recite chapter/line/verse in the bible why this isn't
kosher.
Thanks,
-Clint
#!/usr/bin/perl
use strict;
use warnings;
package Foo;
my $pos;
my @keys;
sub TIEHASH {
my ($class,@args) = @_;
print "TIEHASH($class)\n"; bless {}
}
# Fetch a value from a tied hash
#
sub FETCH {
my $self = $_[0];
my $key = $_[1];
print "FETCH(", ref $self, ") ", "$key => ", ${$self}{$key} ? ${$self}{$key} : "undef", "\n"; ${$self}{$key};
}
# Get the first key from a tied hash
#
sub FIRSTKEY {
my $self = $_[0];
$pos = 0;
@keys = keys %{$self};
$keys[$pos++];
}
# Get the next key from a tied hash
#
sub NEXTKEY {
print "NEXTKEY => ", $keys[$pos] ? $keys[$pos] : "undef", "\n";
$keys[$pos++]
}
# check if a key exists in a tied hash
#
sub EXISTS {
my ($self,$key) = @_;
print "EXISTS $key => ", exists ${$self}{$key}, "\n";
exists ${$self}{$key};
}
#
# Place the value into the hash
#
sub STORE {
my ($self,$key,$val) = @_;
print "STORE(", ref $self, ") $key => $val\n"; ${$self}{$key} = $val;
}
sub DELETE {
my ($self,$key) = @_;
print "DELETE(", ref $self, ") $key\n";
delete ${$self}{$key};
}
sub CLEAR {
my ($self) = @_;
print "CLEAR\n";
%{$self} = ();
}
package Bar;
sub TIESCALAR {
my $class = $_[0];
my $tool = $_[1];
print "TIESCALAR($class) => $tool\n";
return bless \$tool, $class;
}
sub FETCH {
my $self = $_[0];
print "FETCH(", ref $self, ") fetching $$self\n";
return $$self;
}
sub STORE {
my ($self,$val) = @_;
print ref $self, ": STORE => $val\n";
}
sub DESTROY {
my $self = $_[0];
print "DESTROY(", ref $self, ") => $$self\n";
}
package main;
my %hash;
tie %hash, 'Foo';
tie $hash{bar}, 'Bar', 'blah';
$hash{booga} = 'baz';
print "Tied scalar value is $hash{bar}\n";
I have a script included in the post that behaves strangely. Originally I
had a plain hash that had a few scalars inside of it that were tied.
However, for reasons I won't bore you with here, I needed to also tie the
containing hash. However, once I did this, I broke the tied scalars.
After I tie the scalar, it nearly gets immediately culled by the Perl
garbage collector (DESTROY is called), and I'm a bit puzzled why it's
firing. I'm sure some of you Perl blackbelts have seen something like this
before and can recite chapter/line/verse in the bible why this isn't
kosher.
Thanks,
-Clint
#!/usr/bin/perl
use strict;
use warnings;
package Foo;
my $pos;
my @keys;
sub TIEHASH {
my ($class,@args) = @_;
print "TIEHASH($class)\n"; bless {}
}
# Fetch a value from a tied hash
#
sub FETCH {
my $self = $_[0];
my $key = $_[1];
print "FETCH(", ref $self, ") ", "$key => ", ${$self}{$key} ? ${$self}{$key} : "undef", "\n"; ${$self}{$key};
}
# Get the first key from a tied hash
#
sub FIRSTKEY {
my $self = $_[0];
$pos = 0;
@keys = keys %{$self};
$keys[$pos++];
}
# Get the next key from a tied hash
#
sub NEXTKEY {
print "NEXTKEY => ", $keys[$pos] ? $keys[$pos] : "undef", "\n";
$keys[$pos++]
}
# check if a key exists in a tied hash
#
sub EXISTS {
my ($self,$key) = @_;
print "EXISTS $key => ", exists ${$self}{$key}, "\n";
exists ${$self}{$key};
}
#
# Place the value into the hash
#
sub STORE {
my ($self,$key,$val) = @_;
print "STORE(", ref $self, ") $key => $val\n"; ${$self}{$key} = $val;
}
sub DELETE {
my ($self,$key) = @_;
print "DELETE(", ref $self, ") $key\n";
delete ${$self}{$key};
}
sub CLEAR {
my ($self) = @_;
print "CLEAR\n";
%{$self} = ();
}
package Bar;
sub TIESCALAR {
my $class = $_[0];
my $tool = $_[1];
print "TIESCALAR($class) => $tool\n";
return bless \$tool, $class;
}
sub FETCH {
my $self = $_[0];
print "FETCH(", ref $self, ") fetching $$self\n";
return $$self;
}
sub STORE {
my ($self,$val) = @_;
print ref $self, ": STORE => $val\n";
}
sub DESTROY {
my $self = $_[0];
print "DESTROY(", ref $self, ") => $$self\n";
}
package main;
my %hash;
tie %hash, 'Foo';
tie $hash{bar}, 'Bar', 'blah';
$hash{booga} = 'baz';
print "Tied scalar value is $hash{bar}\n";