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:[EMAIL PROTECTED]
> 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