returning one instance of an object per request

2001-07-06 Thread Jay Buffington

Hi,

I'm building a web application that has a User perl module.  I have several other perl 
modules that need to know the user id of the current logged in user (or 0 for a guest 
user).  I was thinking that I could write the User class in such a way that every time 
(except the first) a constructor was called the same instance of the user object would 
be returned for each apache request.  

Is this the best way to go about solving my problem?  If so what's the best way to 
implement this?  Or maybe I should just pass around the user id to every class?  I'd 
perfer to avoid this if possible. 

Thanks,
Jay 



Re: returning one instance of an object per request

2001-07-06 Thread Perrin Harkins

 I'm building a web application that has a User perl module.  I have
several other perl modules that need to know the user id of the current
logged in user (or 0 for a guest user).  I was thinking that I could write
the User class in such a way that every time (except the first) a
constructor was called the same instance of the user object would be
returned for each apache request.

 Is this the best way to go about solving my problem?  If so what's the
best way to implement this?  Or maybe I should just pass around the user id
to every class?  I'd perfer to avoid this if possible.

Try this:

sub get_user {
my $user = Apache-request()-pnotes('USER');
if (!$user) {
# first time for this request
$user = MySite::User-new(); # or whatever
Apache-request()-pnotes('USER', $user);
}
return $user;
}

Put it in some utility class that your other classes all use.
Alternatively, you could make your User class cache itself in pnotes and
just have everyone call new(), but that assumes you'll never want to use it
outside of mod_perl.  Also see Class::Singleton.

- Perrin




Re: returning one instance of an object per request

2001-07-06 Thread darren chamberlain

Jay Buffington [EMAIL PROTECTED] said something to this effect on 07/06/2001:
 I'm building a web application that has a User perl module.  I
 have several other perl modules that need to know the user id
 of the current logged in user (or 0 for a guest user).  I was
 thinking that I could write the User class in such a way that
 every time (except the first) a constructor was called the same
 instance of the user object would be returned for each apache
 request.  
 
 Is this the best way to go about solving my problem?  If so
 what's the best way to implement this?  Or maybe I should just
 pass around the user id to every class?  I'd perfer to avoid
 this if possible. 

Take a look at Class::Singleton, available on CPAN.  From the
docs:

  A Singleton describes an object class that can have only one
  instance in any system.  An example of a Singleton might be a
  print spooler or system registry. This module implements a
  Singleton class from which other classes can be derived. By
  itself, the Class::Singleton module does very little other than
  manage the instantiation of a single object. In deriving a class
  from Class::Singleton, your module will inherit the Singleton
  instantiation method and can implement whatever specific
  functionality is required.

(darren)

-- 
Unix is an operating system, OS/2 is half an operating system, Windows
is a shell, and DOS is a boot partition virus.
-- Peter H. Coffin