You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Simon Dassow <ja...@area319.de> on 2003/10/16 22:42:47 UTC

Shared data in module / sharing %session

Hi Pe(rlers|ople),
I want to have %session to be shared in a module.
With ``use vars qw(%session);'' it doesnt seem to work (Apache is blocking
after a few requests).
My current solution is to store the reference via
``$r->pnotes("SESSION_DATA"=>\%session);'' and save $r into $self so the other methods
have access. But i think i could be made better, but i've no idea how, maybe
someone of you can help me with that.

Perhaps i'm totally wrong, but i didn't find any comments on that and the
mod_perl-book needs a few weeks until it arrives :(.
The main thing i dont understand is why i cant use vars...

Thanks in advance,
Regards,

Simon


The code im using:

package My::Home;
use strict;
use Data::Dumper;
use Apache::Session::Flex;
use Apache::Request;
use Apache::Constants qw(:common);
$My::Home=bless({});

sub handler {
	my($self,$r)=@_;
	my $apr=new Apache::Request($r);
	$self->{$r}=$r;
	my $sid=$apr->param("SID")||undef;
	my %session=();
	
	tie %session, 'Apache::Session::Flex',$sid {..... and so on
	$r->pnotes("SESSION_DATA"=>\%session);

	$r->content_type("text/html");
	$r->send_http_header();
	$self->debug_session();
	return(OK);
}

sub debug_session {
	my $self=shift;
	my %session=$self->{r}->pnotes("SESSION_DATA");
	print "<pre>";
	print Dumper(\%session);
	print "</pre>";
}

Re: Shared data in module / sharing %session

Posted by Simon Dassow <ja...@area319.de>.
On 16 Oct 2003 17:43:14 -0400
Perrin Harkins <pe...@elem.com> wrote:
> On Thu, 2003-10-16 at 16:42, Simon Dassow wrote:
> This is probably because Apache::Session locks until it gets
> destroyed, and globals never get destroyed.

Doh... ok, now i know, thanks :)

> > My current solution is to store the reference via
> > ``$r->pnotes("SESSION_DATA"=>\%session);'' and save $r into $self so
> > the other methods have access. But i think i could be made better,
> > but i've no idea how, maybe someone of you can help me with that.
> Are you just trying to share it with other code during the same
> request?  What you're doing is fine.  You can also always gets the
> current request object by calling Apache->request().

Ok, than i dont need $self anymore i think.

> > $My::Home=bless({});
> That last part is strange.  It's assigning a hash, blessed into
> My::Home, to a global called $Home in the package My.  I doubt that's
> really what you want to do.

I think that's ok if i want $self. In the httpd.conf i have
PerlHandler $My::Home

> > sub handler {
> > 	my($self,$r)=@_;
> 
> There is only a $self here if you've set this handler up as a method
> handler and prototyped it correctly.  See
> http://perl.apache.org/docs/1.0/guide/config.html#Perl_Method_Handlers
> 
> Have you checked to see what you're getting in $self and $r?

Yes, it seems to work :)


Thanks for the reply,
now its clear to me why it didnt work.

Best regards,
Simon

Re: Shared data in module / sharing %session

Posted by Perrin Harkins <pe...@elem.com>.
On Thu, 2003-10-16 at 16:42, Simon Dassow wrote:
> I want to have %session to be shared in a module.
> With ``use vars qw(%session);'' it doesnt seem to work (Apache is blocking
> after a few requests).

This is probably because Apache::Session locks until it gets destroyed,
and globals never get destroyed.

> My current solution is to store the reference via
> ``$r->pnotes("SESSION_DATA"=>\%session);'' and save $r into $self so the other methods
> have access. But i think i could be made better, but i've no idea how, maybe
> someone of you can help me with that.

Are you just trying to share it with other code during the same
request?  What you're doing is fine.  You can also always gets the
current request object by calling Apache->request().

> The main thing i dont understand is why i cant use vars...

You can, but you would have to manually clear the global at the end of
the request so that the session object gets DESTROYed, saves your
changes, and releases locks.

> package My::Home;
> use strict;
> use Data::Dumper;
> use Apache::Session::Flex;
> use Apache::Request;
> use Apache::Constants qw(:common);
> $My::Home=bless({});

That last part is strange.  It's assigning a hash, blessed into
My::Home, to a global called $Home in the package My.  I doubt that's
really what you want to do.

> sub handler {
> 	my($self,$r)=@_;

There is only a $self here if you've set this handler up as a method
handler and prototyped it correctly.  See
http://perl.apache.org/docs/1.0/guide/config.html#Perl_Method_Handlers

Have you checked to see what you're getting in $self and $r?

- Perrin