Keep has asked for the wisdom of the Perl Monks concerning the following question:
monks,
for 10 years Ive been able to find any answer I ave needed here, or on Stack Overflow. Today, neither have helped, much.
My need is to Manhandle Apache::Session::* Similar to the way MasonX::Request::WithApacheSession used to. However Im using Apache2, mod_perl2 and well, MasonX is pretty broken there. I'm still using HTML::Mason, but with my owe Session Handler. This gives me the flexability to switch Data Sources from say Postgres to MySQL (Not that I would, but you never know). Also, If I can get this code functioning well enough, I'd happily submit it to CPAN to have a working workaround for MasonX breakage. (MasonX::Request::WithApache2Session and MasonX::Request::WithApacheSession2 do not work at all). The code follows.
M42C::WebM42C::Web::Session.pmpackage M42C::Web; use 5.008009; use strict; use warnings; use M42C::Web::Session; our @EXPORT = qw(); our $VERSION = '2.0.01'; sub new { my $this = shift; my $class = ref $this || $this; my $self = {}; my $r = shift; bless($self, $class); $self->{_r} = $r; return $self; }; sub session { my $self = shift; return M42C::Web::Session->new( ${$self->{_r}} )->session(); }; 1; __END__
And Finally the autohandlerpackage M42C::Web::Session; use strict; use warnings; use CGI::Cookie; use Carp; use DBI; sub new { my $this = shift; my $class = ref $this || $this; my $self = {}; my $r = shift; bless($self, $class); my %params = @_; $self->{_r} = $r; $self->{_commit} = $params{AutoCommit}; $self->getConnection(); if($self->getCookie) { $class->getSession(); } else { $class->createSession(); }; return $class; } sub getConnection { my $self = shift; return if( $self->{_dbi} ); my $sessionClass = $self->{_r}->dir_config->get("M42CSessionCl +ass"); eval { require $sessionClass; $sessionClass->import(); 1; } or do { my $err = $@; die($err); }; my $dbh = DBI->connect( $self->{_r}->dir_config->get("M42CSessionDataSource"), $self->{_r}->dir_config->get("M42CSessionDataUsername" +), $self->{_r}->dir_config->get("M42CSessionDataPassword" +)); $self->{_dbh} = $dbh; } sub session { my $self = shift; my %cookies = CGI::Cookie->fetch(); if($cookies{'sessionid'}) { $self->{_session} = $self->getSession( $cookies{'sessi +onid'}->value() ); } else { $self->{_session} = $self->createSession(); }; return $self->{_session}; }; sub getCookie { my $self = shift; my %cookies = CGI::Cookie->fetch(); if($cookies{"sessionid"}) { return $cookies{"sessionid"}; }; return undef; } sub setCookie { my $self = shift; my $sid = shift; my $cookie = CGI::Cookie->new( '-value' => $sid, '-name' => $self->{_r}->dir_config->get("M42CCookieNam +e") || 'sessionid', '-expires' => $self->{_r}->dir_config->get("M42CCookie +Expires") || 0, '-domain' => $self->{_r}->dir_config->get("M42CCookieD +omain"), '-path' => $self->{_r}->dir_config->get("M42CCookiePat +h") || '/', '-secure' => $self->{_r}->dir_config->get("M42CCookieS +ecure") || undef, '-httponly' => $self->{_r}->dir_config->get("M42CCooki +eHttp") || undef ); $self->{_r}->headers_out->add("Set-Cookie", $cookie); } sub getSession { my $self = shift; my $cookie = $self->getCookie(); return $self->createSession() if (! $cookie); my %session; tie(%session, $self->{_r}->dir_config->get("M42CSessionClass") +, $cookie->value(), { Handle=>$self->{_dbh}, Commit=>$self->{_r}->dir_config->get("M42CSessionAutoC +ommit") }); return \%session; }; sub createSession { my $self = shift; my %session; tie(%session, $self->{_r}->dir_config->get("M42CSessionClass") +, undef, { Handle=>$self->{_dbh}, Commit=>$self->{_r}->dir_config->get("M42CSessionAutoC +ommit") }); if($self->{_r}->dir_config->get("M42CSessionUseCookie")) { $self->setCookie(\%session); }; return \%session; }; 1; __END__
The Error I receive is:<%init> # $m->comp('/comps/session.comp'); use Data::Dumper; use M42C::Web::Session; my $eng = M42C::Web->new(\$r); </%init> <pre> <% Dumper($eng->session) %> </pre>
Can't locate Apache::Session::Postgres in @INC (@INC contains: /opt/m4 +2c/managed/opt/perl/5.8.9/lib/5.8.9/x86_64-linux /opt/m42c/managed/op +t/perl/5.8.9/lib/5.8.9 /opt/m42c/managed/opt/perl/5.8.9/lib/site_perl +/5.8.9/x86_64-linux /opt/m42c/managed/opt/perl/5.8.9/lib/site_perl/5. +8.9 . /opt/m42c/managed/opt/httpd) at /opt/m42c/managed/opt/perl/5.8. +9/lib/site_perl/5.8.9/M42C/Web/Session.pm line 39. ... 35: return if( $self->{_dbi} ); 36: 37: my $sessionClass = $self->{_r}->dir_config->get("M42CSessionCl +ass"); 38: eval { 39: require $sessionClass; <-- Offending line 40: $sessionClass->import(); 41: 1; 42: } or do { 43: my $err = $@;
Obviously my perl istallation is frameworked, and I have confirmed that Apache::Session::Postgres is in fact installed with
Any and all guidance is much appreciated.perl -MApache::Session::Postgres. Just for kicks I have tried "using" +the module as well to no effect. As well, if the require line is fail +ing, WHY is blowing through the eval. Should it not be blowing at the + die line below it? <code> [shell]$ locate Postgres.pm |grep "Apache/Session" /opt/m42c/managed/opt/perl/5.8.9/lib/site_perl/5.8.9/Apache/Session/Po +stgres.pm /opt/m42c/managed/opt/perl/5.8.9/lib/site_perl/5.8.9/Apache/Session/St +ore/Postgres.pm
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: failure to require an installed module + eval fails to trap? (!bareword)
by tye (Sage) on Jul 26, 2015 at 05:56 UTC | |
|
Re: failure to require an installed module + eval fails to trap?
by syphilis (Archbishop) on Jul 26, 2015 at 04:51 UTC | |
by Keep (Initiate) on Jul 26, 2015 at 07:03 UTC | |
by syphilis (Archbishop) on Jul 26, 2015 at 08:25 UTC | |
by Keep (Initiate) on Jul 26, 2015 at 08:56 UTC | |
by Keep (Initiate) on Jul 28, 2015 at 04:54 UTC |