[prev in list] [next in list] [prev in thread] [next in thread] 

List:       apache-modperl
Subject:    Re: OOP and mod_perl question
From:       "Sean D. Cook" <sdc () edutest ! com>
Date:       2000-09-29 11:44:43
[Download RAW message or body]



> Andreas Grupp wrote:
> > 
> > Hello
> > 
> > I am trying to develop for the first time a perl module. It should work on a
> > server with mod_perl. The objects are not using mod_perl ($r) and are just
> > solving some of my work in a nicer way. Since I'm new in OOP on perl (I only
> > know C++) I would hear from some experts that the following is allowed in Perl
> > OO modules and does not conflict with mod_perl.
> > 
> > The question belongs to the constructor. I have $self as a class reference on
> > the brandnew object. Now in the rest of my constructor I do some Querys on a
> > MySQL database to get information about the authenticated user (.htaccess with
> > AuthenDBI). Afterwards I store the user-data retrieved from the database in a
> > hash-variable and put a reference to this hash in the $self object in the
> > following way:
> > 
> > $self->{'userdata'}->$hashref
> > 
> > Now I can get the different parts of userdata in other instance-methods with
> > code like the following ($po is an object of this class):
> > 
> > my $po = new Peseta;
> > print "<p>This desk belongs to: " . $po->{'userdata'}->{'ulname'} . "</p>";
> 
> er ... this may be wrong but ...
> 
> Here you are directly referancing the Objects data structure - which in
> OO is a little naughty (you should repsect an Objects privacy, but perl
> will not enforce it).
> 
> Hence you would need a method call to return said data, in your Peseta
> package put something thus:
> 
> sub get_desk_owner {
> 
> 	my $self = shift;
> 	my $name = shift;
> 	return $self->{'userdata'}->{$name};
> 
> }
> 
here something we have been quite successfull using.  It allows us to
retrieve nested data from all of our object without directly poking insid
e the object.  Very similar to $r->param();

sub getAttr{
  my $self = shift;
  my $req = shift;

  # we generally tend to store data in a second level hash
  $return $self->{'attr'}->{$req} || '';

}

for setting instance data ...

sub setAttr {

  my $self = shift;

  my $i = {@_};

  foreach my $key (keys %{$i}){
    $self->{'attr'}->{$key} = $i->{$key};
  }

  return;

}

hope this helps

Sean Cook
Systems Analyst
Edutest.com

Phone: 804.673.2253    1.888.335.8378
email: sdc@edutest.com
__________________________________________________
Save the whales.  Collect the whole set.

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic