免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: hyoryeo
打印 上一主题 下一主题

[解决]请教各位大大判断库的方法 [复制链接]

论坛徽章:
78
双子座
日期:2013-10-15 08:50:09天秤座
日期:2013-10-16 18:02:08白羊座
日期:2013-10-18 13:35:33天蝎座
日期:2013-10-18 13:37:06狮子座
日期:2013-10-18 13:40:31双子座
日期:2013-10-22 13:58:42戌狗
日期:2013-10-22 18:50:04CU十二周年纪念徽章
日期:2013-10-24 15:41:34巨蟹座
日期:2013-10-24 17:14:56处女座
日期:2013-10-24 17:15:30双子座
日期:2013-10-25 13:49:39午马
日期:2013-10-28 15:02:15
11 [报告]
发表于 2010-09-10 15:18 |只看该作者
require也应该是可以的

论坛徽章:
0
12 [报告]
发表于 2010-09-10 15:18 |只看该作者
if (exists $ARGV[0]) {
        use lib "/usr/lib/system/";
        no header qw( :ui :log :validation );
        no backup qw( :standard );  
        print "bac";
} else {
        use lib "/usr/lib/system/";
        require header;
        require backup;
}

exit;

[root@-s admin]# ./backup.img 55
Status: 302 Moved
Location: /cgi-bin/login.cgi?

论坛徽章:
0
13 [报告]
发表于 2010-09-10 15:19 |只看该作者
回复 1# hyoryeo


perl cookbook
   

   

12.2. Trapping Errors in require or use
12.2.1. Problem
You need to load in a module that might not be present on your system. This normally results in a fatal exception. You want to detect and trap these failures.

12.2.2. Solution
Wrap the require or use in an eval, and wrap the eval in a BEGIN block:

# no import
BEGIN {
    unless (eval "require $mod; 1") {
        warn "couldn't require $mod: $@";
    }
}

# imports into current package
BEGIN {
    unless (eval "use $mod; 1") {
        warn "couldn't use $mod: $@";
    }
}
12.2.3. Discussion
You usually want a program to fail if it tries to load a module that is missing or doesn't compile. Sometimes, though, you'd like to recover from that error, perhaps trying an alternative module instead. As with any other exception, you insulate yourself from compilation errors with an eval.

You don't want to use eval { BLOCK }, because this traps only runtime exceptions, and use is a compile-time event. Instead, you must use eval "string" to catch compile-time problems as well. Remember, require on a bareword has a slightly different meaning than require on a variable. It adds a ".pm" and translates double-colons into your operating system's path separators, canonically / (as in URLs), but sometimes \, :, or even . on some systems.

If you need to try several modules in succession, stopping at the first one that works, you could do something like this:

BEGIN {
    my($found, @DBs, $mod);
    $found = 0;
    @DBs = qw(Giant::Eenie Giant::Meanie Mouse::Mynie Moe);
    for $mod (@DBs) {
        if (eval "require $mod") {
            $mod->import( );         # if needed
            $found = 1;
            last;
        }
    }
    die "None of @DBs loaded" unless $found;
}
We wrap the eval in a BEGIN block to ensure the module-loading happens at compile time instead of runtime.

12.2.4. See Also
The eval, die, use, and require functions in Chapter 32 of Programming Perl and in perlfunc(1); Recipe 10.12; Recipe 12.3


--------------------------------------------------------------------------------

   
12.1. Defining a Module's Interface  12.3. Delaying use Until Runtime


--------------------------------------------------------------------------------


Copyright © 2003 O'Reilly & Associates. All rights reserved.

论坛徽章:
0
14 [报告]
发表于 2010-09-10 15:20 |只看该作者
那这段代码的结果呢?
#!/usr/bin/perl
use lib "/usr/lib/system/";

if (exists $ARGV[0]) {
        print "bac";
} else {
        require header;
        require backup;
        import header qw( :ui :log :validation );
        import backup qw( :standard );
}

exit;

论坛徽章:
0
15 [报告]
发表于 2010-09-10 15:22 |只看该作者
回复 12# hyoryeo


    你没有  import

论坛徽章:
0
16 [报告]
发表于 2010-09-10 15:39 |只看该作者
那这段代码的结果呢?
dugu072_cu 发表于 2010-09-10 15:20



    呃..其实早上试过..不晓得为什么不行..现在却OK了..也许是因为没有BEGIN...

论坛徽章:
0
17 [报告]
发表于 2010-09-10 15:58 |只看该作者
回复 16# hyoryeo


    与 BEGIN 不会有什么关系,否则,require跑到 BEGIN 去,就和 use 一样了…… 我觉得更大的可能是你没有 import

论坛徽章:
1
未羊
日期:2014-09-08 22:47:27
18 [报告]
发表于 2010-09-10 19:04 |只看该作者
应该用BEGIN

论坛徽章:
0
19 [报告]
发表于 2010-09-14 11:13 |只看该作者
谢谢大家....失败的原因估计找到了...是因为用户权限问题..
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP