You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Andreas Grupp <gr...@elektronikschule.de> on 2000/09/28 19:46:31 UTC

OOP and mod_perl question

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>";

My question is now: Can I be sure that there are no conflicts when several 
users are requesting pages that work with this module? Can I be sure that the 
object data is not shared between different requests and the object has really 
only data corresponding to the actual request when I follow the general rules 
for OOP under perl?

Thanks a lot for your answers

  Andreas

--
____________________________________________________________________

Elektronikschule Tettnang   http://www.elektronikschule.de/~grupp
Oberhofer Str. 25           mailto:grupp@elektronikschule.de
88069 Tettnang              PGP-Key available via mail. Use subject
Tel.: +49 7542 9372-33              Use subject: send pgp-public-key
Fax.: +49 7542 9372-40

Re: OOP and mod_perl question

Posted by "Sean D. Cook" <sd...@edutest.com>.

> 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.



Re: OOP and mod_perl question

Posted by Greg Cope <gj...@rubberplant.freeserve.co.uk>.
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};

}

and the secound line becomes:

print "<p>This desk belongs to: " . $po->get_desk_owner('ulname'} .
"</p>";

Why bother - well you may change the internal objects data structure and
hence your method will break all code that uses it, yet in mine all I
need is to change the object implemetation.

> My question is now: Can I be sure that there are no conflicts when several
> users are requesting pages that work with this module? Can I be sure that the
> object data is not shared between different requests and the object has really
> only data corresponding to the actual request when I follow the general rules
> for OOP under perl?

The objects instance data will not be shared between requests if you are
carfull (and useing strict, and -w will help alot here) to initialise
and scope variables properly.

In your example you use 'my' and hence this will be fine.

The way to make something global (BEWARE) is to use a package global
(via use vars), and not to reinitaialise it after the first request -
then the variable WILL have the same value as it had after its last use
IN THAT CHILD (as apache is a multi-process model - the var will have
different states in different children).

The mod_perl guide covers these issues very well.

Hope that helps.

Greg Cope

> 
> Thanks a lot for your answers
> 
>   Andreas
> 
> --
> ____________________________________________________________________
> 
> Elektronikschule Tettnang   http://www.elektronikschule.de/~grupp
> Oberhofer Str. 25           mailto:grupp@elektronikschule.de
> 88069 Tettnang              PGP-Key available via mail. Use subject
> Tel.: +49 7542 9372-33              Use subject: send pgp-public-key
> Fax.: +49 7542 9372-40