in reply to Autovivification not working?
2;0 juerd@ouranos:~$ rm karmatest 2;0 juerd@ouranos:~$ perl test.pl Use of uninitialized value in addition (+) at test.pl line 31. 2;0 juerd@ouranos:~$ cat test.pl #!/usr/bin/perl -w package Karma; use GDBM_File; use strict; sub new($$){ my ($prot, $file) = @_; my $self = { hash => {} }; $self->{object} = tie %{ $self->{hash} }, 'GDBM_File', $file, GDBM +_WRCREAT, 0640; bless $self, $prot; return $self; } sub resolve($$){ my ($self, $subject) = @_; $subject = lc $subject; my $recur = 0; if (defined $self->{hash}{$subject}){ while ($self->{hash}{$subject} =~ /^S:(.*)/){ $subject = 'ERECURSIE', last if ++$recur > 10; $subject = lc $1; } } # The next line was put in to work around the warning. # $self->{hash}{$subject} = 0 unless defined $self->{hash}{$subject} +; return \ $self->{hash}{$subject}; } sub incr($$;$){ my ($self, $subject, $d) = @_; #line 31 return ${ $self->resolve($subject) } += defined($d) ? $d : 1; } sub decr($$;$){ my ($self, $subject, $d) = @_; return ${ $self->resolve($subject) } -= defined($d) ? $d : 1; } sub get($$){ my ($self, $subject) = @_; my $foo = ${ $self->resolve($subject) }; print "BLAAT: $foo\n"; return $foo; } sub set($$$){ my ($self, $subject, $new) = @_; return ${ $self->resolve($subject) } = $new; } sub reorganize($){ $_[0]->{object}->reorganize(); } 1; package main; my $karma = Karma->new('karmatest'); $karma->incr('foo'); # foo must not exist
I'm sorry to have disappointed you by leaving out a very important part.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Autovivification not working?
by jepri (Parson) on Dec 20, 2001 at 19:27 UTC | |
by Juerd (Abbot) on Dec 20, 2001 at 19:41 UTC | |
by jepri (Parson) on Dec 20, 2001 at 19:55 UTC | |
by Juerd (Abbot) on Dec 20, 2001 at 20:04 UTC |