in reply to Re^3: Designing an OO program with multiple inherited classes
in thread Designing an OO program with multiple inherited classes
Many thanks for your hand-holding. I am almost there, but consider the following variation on your suggestion (my relevant edits, the ones that don't work for me, marked with ## desired ##)
#!/usr/local/bin/perl -w use strict; use DBI qw(:sql_types); use Obj; use constant ( PI => 3.1425 ); { my %CACHE = (); sub cache { my ($self, $cid, $val) = @_; if (defined $val) { %CACHE = (); # empty the cache $CACHE{$cid} = $val; # stuff cache with new val return $val; } else { return $CACHE{$cid} if exists $CACHE{$cid}; } } } my $dbh = DBI->connect( "dbi:SQLite:dbname=db.sqlite","","", {RaiseError => 1, AutoCommit => 0} ); my $obj = Obj->new(dbh => $dbh, uid => 1, cid => 1); print "\$obj->foo called Foo::foo\n"; ##################################### package Obj; use strict; use Foo; sub new { my ($class, %args) = @_; my $self = bless( {dbh => $args{dbh}, uid => $args{uid}, cid => $args{cid},}, $class ); $self->{foo} = Foo->new; return $self; } sub foo { my $self = shift; print "In Obj::foo and about to pass the buck.\n"; return $self->{foo}->foo(@_); } sub dbh { my $self = shift; return $self->{dbh}; } sub cid { my $self = shift; return $self->{cid}; } sub uid { my $self = shift; return $self->{uid}; } 1; ##################################### package Foo; use strict; sub new { return bless { }; } sub foo { print "In Foo::foo, where the work gets done.\n"; ## desired: be able to call Obj::instance_methods like so ## print "Cell id is: " . $self->cid() . "\n"; ## ## desired: be able to access Obj::class_methods, ## class vars and class constants like so ## my $dbh = $self->dbh; my $sth = $dbh->prepare("SELECT * FROM cells WHERE cid = ?"); $sth->execute; my @res = $sth->fetchrow_array; $self->cache($self->cid, \@res); # class method print "Have a pi: " . Obj::PI . "\n"; # class constant ## return 1; } 1;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Designing an OO program with multiple inherited classes
by dsheroh (Monsignor) on Dec 11, 2009 at 08:16 UTC |