use tahash;
my %hash = (
"Jan" => 1,
"Feb" => 2,
"Mar" => 3,
);
my $val = tie(%hash,"tahash") or die "Can't tie : $!";
####
package tahash;
sub TIEHASH {
print "<@_ \n>";
my $class = shift;
my %hash = @_;
print "In TIEHASH .... \n";
return bless(\%hash);
}
####
In TIEHASH ....
The object is destroyed...
####
#!/usr/bin/perl
use strict;
use warnings;
use tahash;
use Data::Dumper (); # just for debugging, remove from production code
my %hash = (
"Jan" => 1,
"Feb" => 2,
"Mar" => 3,
);
my $val = tie(%hash,"tahash") or die "Can't tie : $!";
print Data::Dumper->new([$val],['val'])->Dump(),"\n";
print Data::Dumper->new([\%hash],['*hash'])->Dump(),"\n";
####
package tahash;
use strict;
use warnings;
use Data::Dumper (); # just for debugging, remove from production code
sub TIEHASH {
print Data::Dumper->new([\@_],['*_'])->Dump(),"\n";
my $class = shift;
my %hash = @_;
print "In TIEHASH .... \n";
return bless(\%hash);
}
sub DESTROY {
print "The object is destroyed...\n";
}
1;
####
X:\>perl 1221948.pl
@_ = (
'tahash'
);
In TIEHASH ....
$val = bless( {}, 'tahash' );
Can't locate object method "FIRSTKEY" via package "tahash" at C:/strawberry/perl
/lib/Data/Dumper.pm line 190.
The object is destroyed...
X:\>
####
package tahash;
use strict;
use warnings;
use Data::Dumper (); # just for debugging, remove from production code
# For historic reasons, the Tie::StdHash class is hidden in Tie::Hash.
# So you have to load Tie::Hash instead of Tie::StdHash and set @INC manually.
# Otherwise, you could simply do this:
#use parent 'Tie::StdHash';
use Tie::Hash ();
our @ISA=qw(Tie::StdHash);
sub FIRSTKEY {
my $self=shift;
print "Hey, someone scans my keys!\n";
return $self->SUPER::FIRSTKEY(@_);
}
sub DESTROY {
my $self=shift;
print "The object is destroyed...\n";
$self->SUPER::DESTROY();
}
1;
####
X:\>perl 1221949.pl
$val = bless( {}, 'tahash' );
Hey, someone scans my keys!
%hash = ();
The object is destroyed...
X:\>