sudeep has asked for the wisdom of the Perl Monks concerning the following question:

hi perlmonks, I am trying to write a perl module which has to use CPAN module tie::hash this is the first time am using this module and am not understanding how to tie hash and how to print the stored key and its values. Here is my code
#!/vol/perl/5.8/bin/perl use lib "/vol/perl/5.8/DBD/oracle9.2/lib/site_perl/5.8.1/sun4-solaris" +; $key="num"; $val=12; package NewHash; require Tie::Hash; use Carp; use vars qw(@ISA); @ISA = qw(Tie::StdHash); sub TIEHASH { my $class = shift; print $class; my %hash; @hash{@_} = (undef) x @_; return bless \%hash, $class; } sub STORE { my ($self, $key, $val) = @_; print $key,$val; if (exists $self->{$key}) { print $key,$val; $self->{$key} = $val; # croak "invalid key [$key] in hash\n"; return; } else{ print hhh; $self->{$key} = $val;} return; } sub FETCH { my $self = shift; my $key = shift; my $is_re = (ref $key eq 'Regexp'); return $self->{$key} if !$is_re && exists $self->{$key}; $key = qr/$key/ unless $is_re; /$key/ and return $self->{$_} for keys %$self; return; } package main; { my $b=new; $b->TIEHASH(NewHash); $a=NewHash->STORE('num',12); print "Package Type: ", ref($a), "\n"; print "$self->{$key}"; }
so far i have done this but when i run it its not showing any results. And also I wanted to see those key and its values and use those values for some other purpose. Guys could any one please help me that would be really great. Thanks.

Replies are listed 'Best First'.
Re: using Tie::Hash CPAN module
by Fletch (Bishop) on Feb 25, 2008 at 14:33 UTC

    That's not the way to use a tied hash at all.

    tie my %tied, 'NewHash'; $tied{ num } = 12; print $tied{ num }, "\n";

    You need to read the perltie docs.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: using Tie::Hash CPAN module
by jeanluca (Deacon) on Feb 25, 2008 at 14:34 UTC
    Try this example, you're almost there:
    #! /usr/bin/perl use strict; use warnings ; package Remember; use strict; use warnings; use IO::File; sub TIESCALAR { my $class = shift; my $filename = shift; my $handle = new IO::File "> $filename" or die "Cannot open $filename: $!\n"; print $handle "The Start\n"; bless {FH => $handle, Value => 0}, $class; } sub FETCH { my $self = shift; return $self->{Value}; } sub STORE { my $self = shift; my $value = shift; my $handle = $self->{FH}; print $handle "$value\n"; $self->{Value} = $value; } sub DESTROY { my $self = shift; my $handle = $self->{FH}; print $handle "The End\n"; close $handle; } package main; my $fred; tie $fred, 'Remember', 'myfile.txt'; $fred = 1; $fred = 4; $fred = 5; untie $fred; system "cat myfile.txt";

    Cheers LuCa
      hi, thanks for ur reply, I almost got it but here in this example which one is the hash and key and its corresponding values can we do without using IO::file and FH because here i have some values which are stored in an array and have to store those values to a key and also the important thing is am getting values dynamically at run time so for this first I have to chech whether the key exists or not if exists replace those values with the existed values. For this thing, here am not understanding how to do this.  here we have sub functions store, delete, fetch how to pass a key and values to those functions so that it stores the  values to that key and use that key for some other purpose. I have tried this am not getting it. Thanks.
        You should really read Tie::Hash
        Anyway, to give you a start here is an example:
        #! /usr/bin/perl -l use strict; use warnings ; package Remember; use strict; use warnings; sub TIEHASH { my $class = shift ; return bless {}, $class; } sub STORE { my ( $self, $key, $value) = @_ ; $self->{$key}= $value ; } sub FETCH { my ( $self, $key) = @_ ; return $self->{$key} ; } package main; my %fred; tie %fred, 'Remember' ; $fred{'abc'} = 'xyz' ; print "$fred{'abc'}";