如何根据可用模块动态包含模块?
我有一个使用 CGI::Session::Drive::memcached 的 perl 脚本,但我希望能够回退到默认会话驱动程序或其他驱动程序(如果系统上可用)...
这就是我开始的方式使用 Memcache,但这不一定能解决 Cache::Memecached 和/或 CGI::Session::Driver::memcached 不可用时的问题...
package MySession;
use Moose::Role;
use Moose::Util::TypeConstraints;
use namespace::autoclean;
use CGI::Session ('-ip_match');
use CGI::Session::Driver::memcached;
use Cache::Memcached::Fast;
#would be nice to create this conditionally, or use a delegate maybe
has 'memeCached' => (
is => 'rw',
isa => 'Maybe[Cache::Memcached::Fast]',
default => sub{ return Cache::Memcached::Fast->new( {'servers' => [ '10.x.x.x.:10001' ],'compress_threshold' => '100000','nowait' => 1,'utf8' => 1} ) },
);
sub buildSession{
my($this,$cgi,$sessionDir) = @_;
$cgi = $cgi || $this->getCGI();
my $sid = $this->SID();
my $mem = $this->memeCached();
my $sss;
if(!$mem){
$sss = CGI::Session->load(undef, $cgi, {Directory=>$sessionDir}) or die CGI::Session->errstr();
}else{
$sss = CGI::Session->load( "driver:memcached", $cgi, { Memcached => $mem }) or die CGI::Session->errstr();
}
...
然后这让我思考,我该怎么做——在一般意义上?或者最好的方法是什么(特别是使用 Moose)?
I have a perl script that uses the CGI::Session::Drive::memcached, but I want to be able to fallback on the default Session driver or another driver if it's available on the system...
This is how I started off using Memcache, but this doesnt necessarily solve the problem of the case when Cache::Memecached and/or CGI::Session::Driver::memcached are not available...
package MySession;
use Moose::Role;
use Moose::Util::TypeConstraints;
use namespace::autoclean;
use CGI::Session ('-ip_match');
use CGI::Session::Driver::memcached;
use Cache::Memcached::Fast;
#would be nice to create this conditionally, or use a delegate maybe
has 'memeCached' => (
is => 'rw',
isa => 'Maybe[Cache::Memcached::Fast]',
default => sub{ return Cache::Memcached::Fast->new( {'servers' => [ '10.x.x.x.:10001' ],'compress_threshold' => '100000','nowait' => 1,'utf8' => 1} ) },
);
sub buildSession{
my($this,$cgi,$sessionDir) = @_;
$cgi = $cgi || $this->getCGI();
my $sid = $this->SID();
my $mem = $this->memeCached();
my $sss;
if(!$mem){
$sss = CGI::Session->load(undef, $cgi, {Directory=>$sessionDir}) or die CGI::Session->errstr();
}else{
$sss = CGI::Session->load( "driver:memcached", $cgi, { Memcached => $mem }) or die CGI::Session->errstr();
}
...
Then this got me thinking, how do I do this -- in a general sense? or what's the best way to do this (especially using Moose)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我也有类似的情况。我们使用 Windows 域,我可以将其连接到 Net::LDAP。在我的程序中,我希望能够获取用户 ID
jsmith
,而不是打印用户 ID,我希望能够打印出名字John Smith
>。我公司的很多人都使用我的程序,但并非所有人都是 Perl 专家,而且大多数人不知道如何安装 Perl 模块。而且,由于 Net::LDAP 不是标准模块,因此许多人没有它。
相反,我想要一个后备例程。如果我可以使用
Net::LDAP
查找名称,我会打印该名称,如果我无法加载Net::LDAP
,我会回退并仅打印用户 ID。我使用以下内容来测试
Net::LDAP
是否已安装,并在可能的情况下加载它:您必须了解的是:
与:
它在编译时加载到模块中 时间。通过用
eval
包围require
,我可以测试语句是否成功(并且模块已加载)或失败(模块未加载,但程序未加载)也不会崩溃。)然后我可以检查$@
以查看模块是否已加载。$@
是 eval 设置的错误信息。如果$@
为空,则模块存在并且加载成功。我需要使用包变量(
our $Net_Ldap_Status
而不是my $Net_Ldap_Status
),否则该变量将在程序运行时丢失。 (我什至不确定我的 $Net_Ldap_Status 是否可以在 BEGIN 语句中工作)。现在,事情变得很奇怪......
当我需要检查 $Net_Ldap_Status 时,我需要重新声明它:
否则我往往会遇到未声明的变量错误。有趣的是,重新声明它并不会失去它以前的价值。因此,我的代码中的某处是:
I had a similar situation. We use Windows domains, which I can connect to Net::LDAP. In my program, I want to be able to take the user ID
jsmith
, and instead of printing on the user ID, I want to be able to print out the nameJohn Smith
.Many people at my company use my program, but not all are Perl experts and most wouldn't know how to install a Perl module. And, since Net::LDAP is not a standard module, many people don't have it.
Instead, I wanted a fallback routine. If I could look up the name with
Net::LDAP
, I would print the name, if I couldn't loadNet::LDAP
, I would fallback and just print the user ID.I used the following for testing if
Net::LDAP
was installed, and load it if possible:What you have to understand is that:
is the same as:
It loads in the module at compile time. By surrounding the
require
with aneval
I can test whether the statement succeeds (and the module is loaded) or fails (the module doesn't load, but the program doesn't crash either.) I can then check$@
to see if the module loaded or not.$@
is the error message that eval sets. If$@
is null, then the module exists and was loaded successfully.I need to use a package variable (the
our $Net_Ldap_Status
instead ofmy $Net_Ldap_Status
) or the variable will be lost when the program runs. (I'm not even sure ifmy $Net_Ldap_Status
would work in aBEGIN
statement).Now, here's where things get funky...
When I need to check
$Net_Ldap_Status
, I need to redeclare it:or I tend to get that non-declared variable error. The funny thing is that it doesn't lose its previous value by redeclaring it. Thus, somewhere in my code is: